Переглянути джерело

Fix Spline Mesh save/load

ShiroSmith 6 роки тому
батько
коміт
0005a07640
2 змінених файлів з 42 додано та 29 видалено
  1. 22 7
      hrt/prefab/l3d/Spline.hx
  2. 20 22
      hrt/prefab/l3d/SplineMesh.hx

+ 22 - 7
hrt/prefab/l3d/Spline.hx

@@ -107,14 +107,25 @@ class Spline extends Object3D {
 
 	override function load( obj : Dynamic ) {
 		super.load(obj);
-		pointsData = obj.points == null ? [] : obj.points;
+		pointsData = [];
+		if( obj.points != null ) {
+			var points : Array<Dynamic> = obj.points;
+			for( p in points ) {
+				var m = new h3d.Matrix();
+				m._11 = p._11; m._21 = p._21; m._31 = p._31; m._41 = p._41;
+				m._12 = p._12; m._22 = p._22; m._32 = p._32; m._42 = p._42;
+				m._13 = p._13; m._23 = p._23; m._33 = p._33; m._43 = p._43;
+				m._14 = p._14; m._24 = p._24; m._34 = p._34; m._44 = p._44;
+				pointsData.insert(pointsData.length, m);
+			}
+		}
 		shape = obj.shape == null ? Linear : CurveShape.createByIndex(obj.shape);
 		color = obj.color != null ? obj.color : 0xFFFFFFFF;
 		lineThickness = obj.lineThickness == null ? 4 : obj.lineThickness;
 		loop = obj.loop == null ? false : obj.loop;
 		showSpline = obj.showSpline == null ? true : obj.showSpline;
 		step = obj.step == null ? 1.0 : obj.step;
-		obj.threshold = obj.threshold == null ? 0.01 : obj.threshold;
+		threshold = obj.threshold == null ? 0.01 : obj.threshold;
 	}
 
 	override function makeInstance( ctx : hrt.prefab.Context ) : hrt.prefab.Context {
@@ -126,6 +137,7 @@ class Spline extends Object3D {
 		for( pd in pointsData ) {
 			var sp = new SplinePoint(0, 0, 0, ctx.local3d);
 			sp.setTransform(pd);
+			sp.getAbsPos();
 			points.push(sp);
 		}
 		pointsData = [];
@@ -141,12 +153,13 @@ class Spline extends Object3D {
 	override function updateInstance( ctx : hrt.prefab.Context , ?propName : String ) {
 		super.updateInstance(ctx, propName);
 		computeSplineData();
-		
+
 		#if editor
 		if( editor != null )
 			editor.update(ctx, propName);
 		generateSplineGraph(ctx);
 		#end
+		generateSplineGraph(ctx);
 	}
 
 	// Return an interpolation of two samples at length l, 0 <= l <= splineLength
@@ -161,7 +174,7 @@ class Spline extends Object3D {
 		s2 = cast hxd.Math.clamp(s2, 0, data.samples.length - 1);
 
 		// End/Beginning of the curve, just return the point
-		if( s1 == s2 ) 
+		if( s1 == s2 )
 			return data.samples[s1].pos;
 		// Linear interpolation between the two samples
 		else {
@@ -200,7 +213,7 @@ class Spline extends Object3D {
 		var sd = new SplineData();
 		data = sd;
 
-		if( step <= 0 ) 
+		if( step <= 0 )
 			return;
 
 		if( points == null || points.length <= 1 )
@@ -217,7 +230,7 @@ class Spline extends Object3D {
 			var t = (maxT + minT) * 0.5;
 			var p = getPointBetween(t, points[i], points[i+1]);
 			var curSegmentLength = p.distance(samples[samples.length - 1].pos);
-			
+
 			// Point found
 			if( hxd.Math.abs(curSegmentLength - step) <= threshold ) {
 				samples.insert(samples.length, { pos : p, tangent : getTangentBetween(t, points[i], points[i+1]), prev : points[i], next : points[i+1], t : t });
@@ -338,7 +351,7 @@ class Spline extends Object3D {
 		return p1.sub(p0).multiply(3 * (1 - t) * (1 - t)).add(p2.sub(p1).multiply(6 * (1 - t) * t)).add(p3.sub(p2).multiply(3 * t * t)).normalizeFast();
 	}
 
-	#if editor
+
 
 	function generateSplineGraph( ctx : hrt.prefab.Context ) {
 
@@ -368,6 +381,8 @@ class Spline extends Object3D {
 		}
 	}
 
+	#if editor
+
 	override function setSelected( ctx : hrt.prefab.Context , b : Bool ) {
 		super.setSelected(ctx, b);
 

+ 20 - 22
hrt/prefab/l3d/SplineMesh.hx

@@ -1,5 +1,4 @@
 package hrt.prefab.l3d;
-import format.abc.Data.ABCData;
 import h3d.scene.MeshBatch;
 import h3d.scene.Mesh;
 import hrt.prefab.l3d.Spline.SplinePoint;
@@ -41,7 +40,7 @@ class SplineMeshShader extends hxsl.Shader {
 			var worldUp = vec3(0,0,1);
 			var front = -tangent;
 			var right = -front.cross(worldUp).normalize();
-			var up = front.cross(right).normalize();			
+			var up = front.cross(right).normalize();
 
 			var rotation = mat4(	vec4(right.x, front.x, up.x, 0),
 									vec4(right.y, front.y, up.y, 0),
@@ -54,7 +53,6 @@ class SplineMeshShader extends hxsl.Shader {
 									vec4(0,0,0,1));
 
 			var transform = rotation * translation;
-
 			var localPos = (modelPos - vec3(0, offsetY, 0));
 			transformedPosition = localPos * transform.mat3x4();
 			transformedNormal = transformedNormal * modelMat.mat3x4() * rotation.mat3x4();
@@ -64,9 +62,6 @@ class SplineMeshShader extends hxsl.Shader {
 			if( SPLINE_UV_Y )
 				calculatedUV.y = pos;
 		}
-
-		function fragment() {
-		}
 	}
 }
 
@@ -108,14 +103,14 @@ class SplineMesh extends Spline {
 		super.load(obj);
 		meshPath = obj.meshPath;
 		spacing = obj.spacing == null ? 0.0 : obj.spacing;
-		meshScale = obj.meshScale == null ? new h3d.Vector(1,1,1) : obj.meshScale;
-		meshRotation = obj.meshRotation == null ? new h3d.Vector(0,0,0) : obj.meshRotation;
+		meshScale = obj.meshScale == null ? new h3d.Vector(1,1,1) : new h3d.Vector(obj.meshScale.x, obj.meshScale.y, obj.meshScale.z);
+		meshRotation = obj.meshRotation == null ? new h3d.Vector(0,0,0) : new h3d.Vector(obj.meshRotation.x, obj.meshRotation.y, obj.meshRotation.z);
 		splineUVx = obj.splineUVx == null ? false : obj.splineUVx;
 		splineUVy = obj.splineUVy == null ? false : obj.splineUVy;
 	}
 
 	override function make(ctx: Context) {
-		// Don't make children, which are used to setup particles
+		// Don't make children, which are used to setup the material
 		return makeInstance(ctx);
 	}
 
@@ -123,15 +118,16 @@ class SplineMesh extends Spline {
 		var ctx = ctx.clone(this);
 		ctx.local3d = new h3d.scene.Object(ctx.local3d);
 		ctx.local3d.name = name;
-		
+
 		for( pd in pointsData ) {
 			var sp = new SplinePoint(0, 0, 0, ctx.local3d);
 			sp.setTransform(pd);
+			sp.getAbsPos();
 			points.push(sp);
 		}
 		pointsData = [];
 
-		if( points == null || points.length == 0 ) 
+		if( points == null || points.length == 0 )
 			points.push(new SplinePoint(0,0,0, ctx.local3d));
 
 		updateInstance(ctx);
@@ -146,7 +142,7 @@ class SplineMesh extends Spline {
 		var scale = new h3d.Matrix();
 		scale.initScale(meshScale.x, meshScale.y, meshScale.z);
 		modelMat.multiply(scale, rot);
-		
+
 		createMeshPrimitive(ctx);
 		createMeshBatch(ctx);
 		createBatches(ctx);
@@ -158,10 +154,10 @@ class SplineMesh extends Spline {
 			emptyCtx.shared = ctx.shared;
 			for( c in @:privateAccess children ) {
 				var mat = Std.downcast(c, Material);
-				if( mat != null && mat.enabled ) 
+				if( mat != null && mat.enabled )
 					@:privateAccess mat.makeInstance(emptyCtx);
 				var shader = Std.downcast(c, Shader);
-				if( shader != null && shader.enabled ) 
+				if( shader != null && shader.enabled )
 					shader.makeInstance(emptyCtx);
 			}
 		}
@@ -170,7 +166,7 @@ class SplineMesh extends Spline {
 	function createMeshPrimitive( ctx : Context ) {
 		meshPrimitive = null;
 		meshMaterial = null;
-		if( meshPath != null ) {	
+		if( meshPath != null ) {
 			var meshTemplate : h3d.scene.Mesh = ctx.loadModel(meshPath).toMesh();
 			if( meshTemplate != null ) {
 				meshPrimitive = cast meshTemplate.primitive;
@@ -186,7 +182,7 @@ class SplineMesh extends Spline {
 			meshBatch = null;
 		}
 
-		if( meshPrimitive == null || (meshBatch != null && meshBatch.primitive == meshPrimitive) ) 
+		if( meshPrimitive == null || (meshBatch != null && meshBatch.primitive == meshPrimitive) )
 			return;
 
 		if( meshBatch == null ) {
@@ -201,7 +197,7 @@ class SplineMesh extends Spline {
 
 	function createBatches( ctx : Context ) {
 
-		if( meshBatch == null )	
+		if( meshBatch == null )
 			return;
 
 		var localBounds = meshPrimitive.getBounds().clone();
@@ -213,17 +209,18 @@ class SplineMesh extends Spline {
 		if( stepCount > 4096 )
 			return;
 
-		var splinemeshShader = meshBatch.material.mainPass.getShader(SplineMeshShader);
 		meshBatch.begin(stepCount);
+		var splinemeshShader = meshBatch.material.mainPass.getShader(SplineMeshShader);
 		for( i in 0 ... stepCount ) {
-			splinemeshShader.splinePos = i * step + minOffset;
+			if( splinemeshShader != null )
+				splinemeshShader.splinePos = i * step + minOffset;
 			meshBatch.emitInstance();
 		}
-	}	
+	}
 
 	function createMultiMeshes( ctx : Context ) {
 
-		for( m in meshes ) 
+		for( m in meshes )
 			m.remove();
 
 		meshes = [];
@@ -307,6 +304,7 @@ class SplineMesh extends Spline {
 		return { icon : "arrows-v", name : "SplineMesh" };
 	}
 
-	static var _ = hrt.prefab.Library.register("splineMesh", SplineMesh);
 	#end
+
+	static var _ = hrt.prefab.Library.register("splineMesh", SplineMesh);
 }