ソースを参照

Improve emitter

trethaller 7 年 前
コミット
0079285ca9
2 ファイル変更40 行追加14 行削除
  1. 6 4
      hide/comp/PropsEditor.hx
  2. 34 10
      hide/prefab/fx/Emitter.hx

+ 6 - 4
hide/comp/PropsEditor.hx

@@ -3,7 +3,7 @@ package hide.comp;
 enum PropType {
 	PInt( ?min : Int, ?max : Int );
 	PFloat( ?min : Float, ?max : Float );
-	PVec( n : Int );
+	PVec( n : Int, ?min : Float, ?max : Float );
 	PBool;
 	PTexture;
 	PChoice( choices : Array<String> );
@@ -67,15 +67,17 @@ class PropsEditor extends Component {
 			new Element('<input type="texturepath" field="${p.name}">').appendTo(parent);
 		case PUnsupported(text):
 			new Element('<font color="red">' + StringTools.htmlEscape(text) + '</font>').appendTo(parent);
-		case PVec(n):
+		case PVec(n, min, max):
 			var isColor = p.name.toLowerCase().indexOf("color") >= 0;
 			var names = isColor ? ["r", "g", "b", "a"] : ["x", "y", "z", "w"];
 			for( i in 0...n ) {
 				var div = new Element('<div>').appendTo(parent);
 				new Element('<span>${names[i]} </span>').appendTo(div);
 				var e = new Element('<input type="range" class="small" field="${p.name}.$i">').appendTo(div);
-				e.attr("min", isColor ? "0" : "-1");
-				e.attr("max", "1");
+				if(min == null) min = isColor ? 0.0 : -1.0;
+				if(max == null)	max = 1.0;
+				e.attr("min", "" + min);
+				e.attr("max", "" + max);
 			}
 		case PChoice(choices):
 			var e = new Element('<select field="${p.name}"></select>');

+ 34 - 10
hide/prefab/fx/Emitter.hx

@@ -20,6 +20,7 @@ typedef InstanceDef = {
 	localSpeed: Value,
 	localOffset: Value,
 	scale: Value,
+	rotation: Value,
 	?alignVec: h3d.Vector
 }
 
@@ -46,8 +47,13 @@ private class ParticleInstance extends h3d.scene.Object {
 	}
 
 	public function update(dt : Float) {
+		var child = getChildAt(0);
+		if(child == null)
+			return;
+
+		var t = hxd.Math.clamp(life / emitter.lifeTime, 0.0, 1.0);
 
-		var localSpeed = evaluator.getVector(def.localSpeed, life);
+		var localSpeed = evaluator.getVector(def.localSpeed, t);
 		if(localSpeed.length() > 0.001) {
 			localSpeed.transform3x3(orientation.toMatrix());
 			curVelocity = localSpeed;
@@ -57,13 +63,20 @@ private class ParticleInstance extends h3d.scene.Object {
 		y += curVelocity.y * dt;
 		z += curVelocity.z * dt;
 
-		var scaleVec = evaluator.getVector(def.scale, life);
-		scaleX = scaleVec.x;
-		scaleY = scaleVec.y;
-		scaleZ = scaleVec.z;
+		var rot = evaluator.getVector(def.rotation, t);
+		rot.scale3(Math.PI / 180.0);
+		child.setRotation(rot.x, rot.y, rot.z);
+
+		var offset = evaluator.getVector(def.localOffset, t);
+		child.setPosition(offset.x, offset.y, offset.z);
+
+		var scaleVec = evaluator.getVector(def.scale, t);
+		child.scaleX = scaleVec.x;
+		child.scaleY = scaleVec.y;
+		child.scaleZ = scaleVec.z;
 
 		for(anim in shaderAnims) {
-			anim.setTime(life);  // TODO: Scale by lifetime
+			anim.setTime(t);
 		}
 
 		life += dt;
@@ -262,13 +275,23 @@ class Emitter extends Object3D {
 	static var instanceParams : Array<ParamDef> = [
 		{
 			name: "speed",
-			t: PVec(3),
+			t: PVec(3, -10, 10),
 			def: [5.,0.,0.]
 		},
 		{
 			name: "scale",
 			t: PVec(3),
 			def: [1.,1.,1.]
+		},
+		{
+			name: "rotation",
+			t: PVec(3, 0, 360),
+			def: [0.,0.,0.]
+		},
+		{
+			name: "offset",
+			t: PVec(3, -10, 10),
+			def: [0.,0.,0.]
 		}
 	];
 
@@ -374,9 +397,10 @@ class Emitter extends Object3D {
 
 		emitterObj.instDef = {
 			localSpeed: makeParam(template, "speed"),
-			localOffset: VConst(0.0),
-			scale: VConst(1.0),
-			alignVec: new h3d.Vector(1,0,0)
+			localOffset: makeParam(template, "offset"),
+			scale: makeParam(template, "scale"),
+			rotation: makeParam(template, "rotation"),
+			alignVec: null, //new h3d.Vector(1,0,0)
 		};
 
 		emitterObj.particleTemplate = template;