Browse Source

[MESHSPRAY] Fix set to ground redraw. Fix set to ground with binary meshes.

clandrin 3 years ago
parent
commit
d20bc89e5e
1 changed files with 44 additions and 13 deletions
  1. 44 13
      hrt/prefab/l3d/MeshSpray.hx

+ 44 - 13
hrt/prefab/l3d/MeshSpray.hx

@@ -448,17 +448,29 @@ class MeshSpray extends Object3D {
 		sys.io.File.saveContent(MESH_SPRAY_CONFIG_PATH, hide.Ide.inst.toJSON(allSetGroups));
 	}
 
-	function setGroundPos( ectx : EditContext, obj : Object3D ) {
-		var pos = obj.getAbsPos();
+	function setGroundPos( ectx : EditContext, obj : Object3D = null, absPos : h3d.col.Point = null ) : { mz : Float, rotX : Float, rotY : Float, rotZ : Float } {
+		if (absPos == null && obj == null)
+			throw "setGroundPos should use either object or absPos";
+		var tx : Float; var ty : Float; var tz : Float;
+		if ( absPos != null ) {
+			tx = absPos.x;
+			ty = absPos.y;
+			tz = absPos.z;
+		} else { // obj != null
+			tx = obj.getAbsPos().tx;
+			ty = obj.getAbsPos().ty;
+			tz = obj.getAbsPos().tz;
+		}
 		var config = currentConfig;
-		var tz = ectx.positionToGroundZ(pos.tx, pos.ty);
-		var mz = config.zOffset + tz - pos.tz;
-		obj.z += mz;
+		var groundZ = ectx.positionToGroundZ(tx, ty);
+		var mz = config.zOffset + groundZ - tz;
+		if ( obj != null )
+			obj.z += mz;
 		var orient = config.orientTerrain;
 		var tilt = config.tiltAmount;
 
 		inline function getPoint(dx,dy) {
-			var dz = ectx.positionToGroundZ(pos.tx + 0.1 * dx, pos.ty + 0.1 * dy) - tz;
+			var dz = ectx.positionToGroundZ(tx + 0.1 * dx, ty + 0.1 * dy) - groundZ;
 			return new h3d.col.Point(dx*0.1, dy*0.1, dz * orient);
 		}
 
@@ -470,9 +482,15 @@ class MeshSpray extends Object3D {
 		var m = q.toMatrix();
 		m.prependRotation(Math.random()*tilt*Math.PI/8,0,  (config.rotation + (Std.random(2) == 0 ? -1 : 1) * Math.round(Math.random() * config.rotationOffset)) * Math.PI / 180);
 		var a = m.getEulerAngles();
-		obj.rotationX = hxd.Math.fmt(a.x * 180 / Math.PI);
-		obj.rotationY = hxd.Math.fmt(a.y * 180 / Math.PI);
-		obj.rotationZ = hxd.Math.fmt(a.z * 180 / Math.PI);
+		var rotX = hxd.Math.fmt(a.x * 180 / Math.PI);
+		var rotY = hxd.Math.fmt(a.y * 180 / Math.PI);
+		var rotZ = hxd.Math.fmt(a.z * 180 / Math.PI);
+		if ( obj != null ) {
+			obj.rotationX = rotX;
+			obj.rotationY = rotY;
+			obj.rotationZ = rotZ;	
+		}
+		return { mz : mz, rotX : rotX, rotY : rotY, rotZ : rotZ };
 	}
 
 	var wasEdited = false;
@@ -709,16 +727,29 @@ class MeshSpray extends Object3D {
 		});
 
 		options.find("#toground").click(function(_) {
-			var mso = null;
-			for( c in children ) {
+			var ctx = ectx.getContext(this);
+			var mso = cast(ctx.local3d,MeshSprayObject);
+			for( c in this.children ) {
 				var obj = c.to(Object3D);
 				if( obj == null ) continue;
 				setGroundPos(ectx, obj);
 				var ctx = ectx.getContext(obj);
 				if( ctx != null ) obj.applyTransform(ctx.local3d);
-				if ( ctx.local3d.parent != null && Std.is(ctx.local3d.parent, MeshSprayObject))
-					mso = cast(ctx.local3d.parent,MeshSprayObject);
 			}
+			if ( this.binaryMeshes != null ) {
+				var pos = new h3d.col.Point(0,0,0);
+				for ( bm in this.binaryMeshes ) {
+					pos.x = bm.x;
+					pos.y = bm.y;
+					pos.z = bm.z;
+					var ground = setGroundPos(ectx, null, pos);
+					bm.z += ground.mz;
+					bm.rotX = ground.rotX;
+					bm.rotY = ground.rotY;
+					bm.rotZ = ground.rotZ;
+				}
+			}
+			
 			mso.redraw();
 		});