소스 검색

Fix material override problems

trethaller 7 년 전
부모
커밋
d42c30b009
4개의 변경된 파일52개의 추가작업 그리고 16개의 파일을 삭제
  1. 39 9
      hide/prefab/Material.hx
  2. 1 1
      hide/prefab/Shader.hx
  3. 9 3
      hide/prefab/l3d/Polygon.hx
  4. 3 3
      hide/view/l3d/Level3D.hx

+ 39 - 9
hide/prefab/Material.hx

@@ -27,19 +27,27 @@ class Material extends Prefab {
 		return r;
 	}
 
-	override function updateInstance(ctx: Context, ?propName) {
-		if(ctx.local3d == null)
-			return;
-
-		inline function update(mat : h3d.mat.Material, props) {
+	function updateObject(ctx: Context, obj: h3d.scene.Object) {
+		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);
+
+			inline function getTex(pname: String) {
+				var p : String = Reflect.field(props, pname);
+				var tex : h3d.mat.Texture = null;
+				if(p != null) {
+					tex = ctx.loadTexture(p);
+					if(tex != null)
+						tex.wrap = Repeat;
+				}
+				return tex;
 			}
+			
+			mat.texture = getTex("diffuseMap");
+			mat.normalMap = getTex("normalMap");
+			mat.specularTexture = getTex("specularMap");
 		}
 
-		var mats = ctx.local3d.getMaterials();
+		var mats = obj.getMaterials();
 		var mat = Lambda.find(mats, m -> m.name == this.name);
 		var props = renderProps();
 		if(mat != null) {
@@ -51,6 +59,20 @@ class Material extends Prefab {
 		}
 	}
 
+	override function updateInstance(ctx: Context, ?propName) {
+		if(ctx.local3d == null)
+			return;
+
+		var obj = ctx.local3d;
+		if(parent != null && Type.getClass(parent) == hide.prefab.Object3D) {
+			for(i in 0...obj.numChildren) {
+				updateObject(ctx, obj.getChildAt(i));
+			}
+		}
+		else
+			updateObject(ctx, obj);
+	}
+
 	override function makeInstance(ctx:Context):Context {
 		if(ctx.local3d == null)
 			return ctx;
@@ -88,5 +110,13 @@ class Material extends Prefab {
 		return { icon : "cog", name : "Material" };
 	}
 
+	public static function hasOverride(p: Prefab) {
+		if(Lambda.exists(p.children, c -> Std.is(c, Material)))
+			return true;
+		if(Type.getClass(p.parent) == hide.prefab.Object3D) //if(Std.is(p.parent, Prefab)) 
+			return Lambda.exists(p.parent.children, c -> Std.is(c, Material));
+		return false;
+	}
+
 	static var _ = Library.register("material", Material);
 }

+ 1 - 1
hide/prefab/Shader.hx

@@ -56,7 +56,7 @@ class Shader extends Prefab {
 			var defVal = evalConst(v.e);
 			shader.hscriptSet(v.v.name, defVal);
 		}
-		for(m in ctx.local3d.getMaterials()) {
+		for(m in ctx.local3d.getMaterials()) { // TODO: Only add to self materials, not all children materials
 			m.mainPass.addShader(shader);
 		}
 		ctx.custom = shader;

+ 9 - 3
hide/prefab/l3d/Polygon.hx

@@ -27,9 +27,14 @@ class Polygon extends Object3D {
 		specularMap = obj.specularMap;
 	}
 
-	public function applyProps(ctx: Context) { // Idea: make common to all Prefabs
+	override function updateInstance( ctx : Context, ?propName : String) {
+		super.updateInstance(ctx, propName);
 		if(ctx.local3d == null)
 			return;
+
+		if(hide.prefab.Material.hasOverride(this))
+			return;
+
 		var mesh : h3d.scene.Mesh = cast ctx.local3d;
 		var mat = mesh.material;
 		mat.mainPass.culling = None;
@@ -91,12 +96,14 @@ class Polygon extends Object3D {
 		ctx.local3d = mesh;
 		ctx.local3d.name = name;
 		applyPos(ctx.local3d);
-		applyProps(ctx);
+		updateInstance(ctx);
 		return ctx;
 	}
 
 	function setColor(ctx: Context, color: Int) {
 		#if editor
+		if(hide.prefab.Material.hasOverride(this))
+			return;
 		if(ctx.local3d == null)
 			return;
 		var mesh = Std.instance(ctx.local3d, h3d.scene.Mesh);
@@ -119,7 +126,6 @@ class Polygon extends Object3D {
 			</div>
 		'),this, function(pname) {
 			ctx.onChange(this, pname);
-			applyProps(ctx.getContext(this));
 		});
 		#end
 	}

+ 3 - 3
hide/view/l3d/Level3D.hx

@@ -152,7 +152,7 @@ private class Level3DSceneEditor extends hide.comp.SceneEditor {
 			allowed = ["renderProps"];
 		}
 
-		if(current != null && (current.type == "model" || current.type == "polygon")) {
+		if(current != null && (current.type == "model" || current.type == "polygon" || current.type == "object")) {
 			allowed.push("material");
 			allowed.push("shader");
 		}
@@ -518,7 +518,7 @@ class Level3D extends FileView {
 		var poly = p.to(hide.prefab.l3d.Polygon);
 		if(poly != null) {
 			var ctx = sceneEditor.getContext(poly);
-			poly.applyProps(ctx);
+			poly.updateInstance(ctx);
 		}
 	}
 
@@ -534,7 +534,7 @@ class Level3D extends FileView {
 		}
 		for(poly in layer.getAll(hide.prefab.l3d.Polygon)) {
 			var ctx = sceneEditor.getContext(poly);
-			poly.applyProps(ctx);
+			poly.updateInstance(ctx);
 		}
 	}