浏览代码

Add support for material prefabs

trethaller 7 年之前
父节点
当前提交
50ae1a77ea
共有 4 个文件被更改,包括 105 次插入3 次删除
  1. 98 0
      hide/prefab/Material.hx
  2. 1 1
      hide/prefab/Shader.hx
  3. 5 1
      hide/prefab/fx/Emitter.hx
  4. 1 1
      hide/view/FXScene.hx

+ 98 - 0
hide/prefab/Material.hx

@@ -0,0 +1,98 @@
+package hide.prefab;
+
+
+class Material extends Prefab {
+
+	public function new(?parent) {
+		super(parent);
+		props = {};
+	}
+	
+	override function load(o:Dynamic) {
+	}
+
+	override function save() {
+		return {
+		};
+	}
+
+	function renderProps() {
+		var cur = h3d.mat.MaterialSetup.current;
+		var setupName = cur.name;
+		var r = Reflect.field(props, setupName);
+		if(r == null) {
+			r = {};
+			Reflect.setField(props, setupName, r);
+		}
+		return r;
+	}
+
+	function updateInstance(ctx: Context) {
+		if(ctx.local3d == null)
+			return;
+
+		inline function update(mat : h3d.mat.Material, props) {
+			mat.props = props;
+			var diff : String = Reflect.field(props, "diffuseMap");
+			if(diff != null) {
+				mat.texture = ctx.loadTexture(diff);
+			}
+		}
+
+		var mats = ctx.local3d.getMaterials();
+		var mat = Lambda.find(mats, m -> m.name == this.name);
+		var props = renderProps();
+		if(mat != null) {
+			update(mat, props);
+		}
+		else {
+			for(m in mats)
+				update(m, props);
+		}
+	}
+
+	override function makeInstance(ctx:Context):Context {
+		if(ctx.local3d == null)
+			return ctx;
+		ctx = ctx.clone(this);
+
+		updateInstance(ctx);
+		return ctx;
+	}
+
+	override function edit( ctx : EditContext ) {
+		#if editor		
+		super.edit(ctx);
+
+		var mat = h3d.mat.Material.create();
+		mat.props = renderProps();
+		var group = ctx.properties.add(new hide.Element('<div class="group" name="Material"></div>'));
+		ctx.properties.addMaterial(mat, group.find('.group > .content'), function(pname) {
+			Reflect.setField(props, h3d.mat.MaterialSetup.current.name, mat.props);
+			ctx.onChange(this, "props");
+			var inst = ctx.getContext(this);
+			if(inst != null)
+				updateInstance(inst);
+		});
+
+		var isPbr = Std.is(ctx.scene.s3d.renderer, h3d.scene.pbr.Renderer);
+		ctx.properties.add(new hide.Element('<div class="group" name="Overrides">
+			<dl>
+				<dt>${isPbr ? "Albedo" : "Diffuse"}</dt><dd><input type="texturepath" field="diffuseMap" style="width:165px"/></dd>
+				<dt>Normal</dt><dd><input type="texturepath" field="normalMap" style="width:165px"/></dd>
+				<dt>Specular</dt><dd><input type="texturepath" field="specularMap" style="width:165px"/></dd>
+			</dl></div>'), renderProps(), function(_) {
+			ctx.onChange(this, "props");
+			var inst = ctx.getContext(this);
+			if(inst != null)
+				updateInstance(inst);
+		});
+		#end
+	}
+
+	override function getHideProps() : HideProps {
+		return { icon : "cog", name : "Material" };
+	}
+
+	static var _ = Library.register("material", Material);
+}

+ 1 - 1
hide/prefab/Shader.hx

@@ -53,7 +53,7 @@ class Shader extends Prefab {
 		};
 		};
 	}
 	}
 
 
-	public function applyVars(ctx: Context, time: Float=0.0) {
+	function applyVars(ctx: Context) {
 		var shader = Std.instance(ctx.custom, ShaderAnimation);
 		var shader = Std.instance(ctx.custom, ShaderAnimation);
 		if(shader == null || shaderDef == null)
 		if(shader == null || shaderDef == null)
 			return;
 			return;

+ 5 - 1
hide/prefab/fx/Emitter.hx

@@ -158,6 +158,11 @@ class EmitterObject extends h3d.scene.Object {
 					part.shaderAnims.push(anim);
 					part.shaderAnims.push(anim);
 				}
 				}
 			}
 			}
+
+			var materials = particleTemplate.getAll(hide.prefab.Material);
+			for(mat in materials) {
+				mat.makeInstance(ctx);
+			}
 		}
 		}
 		context.local3d = this;
 		context.local3d = this;
 		emitCount += count;
 		emitCount += count;
@@ -349,7 +354,6 @@ class Emitter extends Object3D {
 			scale: VConst(1.0),
 			scale: VConst(1.0),
 		};
 		};
 
 
-		trace(emitterObj.instDef.localSpeed);
 		emitterObj.particleTemplate = template;
 		emitterObj.particleTemplate = template;
 		emitterObj.lifeTime = getParamVal("lifeTime");
 		emitterObj.lifeTime = getParamVal("lifeTime");
 		emitterObj.maxCount = getParamVal("maxCount");
 		emitterObj.maxCount = getParamVal("maxCount");

+ 1 - 1
hide/view/FXScene.hx

@@ -46,7 +46,7 @@ private class FXSceneEditor extends hide.comp.SceneEditor {
 			});
 			});
 
 
 			var allRegs = @:privateAccess hide.prefab.Library.registeredElements;
 			var allRegs = @:privateAccess hide.prefab.Library.registeredElements;
-			var allowed = ["model", "object", "shader", "emitter", "constraint", "polygon"];
+			var allowed = ["model", "object", "shader", "emitter", "constraint", "polygon", "material"];
 			for( ptype in allowed ) {
 			for( ptype in allowed ) {
 				var pcl = allRegs.get(ptype);
 				var pcl = allRegs.get(ptype);
 				var props = Type.createEmptyInstance(pcl).getHideProps();
 				var props = Type.createEmptyInstance(pcl).getHideProps();