Przeglądaj źródła

[material] Fix material library preview interactive and update

Clément Espeute 1 rok temu
rodzic
commit
1d6fe7f693
1 zmienionych plików z 41 dodań i 30 usunięć
  1. 41 30
      hrt/prefab/Material.hx

+ 41 - 30
hrt/prefab/Material.hx

@@ -115,12 +115,43 @@ class Material extends Prefab {
 			mat.mainPass.setPassName(mainPassName);
 	}
 
+	override function makeInstance() {
+		#if editor
+		var mats = getMaterials();
+
+		if ( mats == null || mats.length == 0 ) {
+			if (previewSphere != null)
+				throw "what";
+
+			var parent = findFirstLocal3d();
+
+			var sphere = new h3d.prim.Sphere(1., 64, 48);
+			sphere.addUVs();
+			sphere.addNormals();
+			sphere.addTangents();
+			sphere.colors = sphere.points;
+
+			var m = new h3d.scene.Mesh(sphere);
+			m.name = "previewSphereObjName";
+			@:privateAccess m.material.name = "previewMat";
+			previewSphere = m;
+			parent.addChild(previewSphere);
+
+			var previewCount = findFirstLocal3d().getScene().findAll(o -> o.name == "previewSphereObjName" ? true : null).length;
+			previewSphere.x = ( previewCount - 1) * 5.0;
+		}
+		#end
+
+
+		updateInstance();
+	}
+
 	override function updateInstance(?propName ) {
 		var local3d = findFirstLocal3d();
 		if( local3d == null )
 			return;
 
-		var mats = getMaterials();
+		var mats = getMaterials(true);
 
 		if (this.refMatLib != null && this.refMatLib != "") {
 			// We want to save some infos to reapply them after loading datas from the choosen mat
@@ -153,31 +184,6 @@ class Material extends Prefab {
 
 		var props = renderProps();
 
-		#if editor
-		if ( mats == null || mats.length == 0 ) {
-			if (previewSphere != null)
-				previewSphere.remove();
-
-			var parent = findFirstLocal3d();
-
-			var sphere = new h3d.prim.Sphere(1., 64, 48);
-			sphere.addUVs();
-			sphere.addNormals();
-			sphere.addTangents();
-			sphere.colors = sphere.points;
-
-			var m = new h3d.scene.Mesh(sphere);
-			m.name = "previewSphereObjName";
-			@:privateAccess m.material.name = "previewMat";
-			previewSphere = m;
-			parent.addChild(previewSphere);
-
-			var previewCount = findFirstLocal3d().getScene().findAll(o -> o.name == "previewSphereObjName" ? true : null).length;
-			previewSphere.x = ( previewCount - 1) * 5.0;
-
-			mats = [ @:privateAccess m.material ];
-		}
-		#end
 		function loadTextureCb( path : String ) : h3d.mat.Texture {
 			return shared.loadTexture(path, false);
 		}
@@ -185,10 +191,6 @@ class Material extends Prefab {
 			update(m, props, loadTextureCb);
 	}
 
-	override function makeInstance() {
-		updateInstance();
-	}
-
 	function applyOverrides() {
 		if (this.overrides == null || this.overrides.length == 0)
 			return;
@@ -225,6 +227,15 @@ class Material extends Prefab {
 	}
 
 	#if editor
+	override function makeInteractive() : hxd.SceneEvents.Interactive {
+		if (previewSphere != null) {
+			var col = new h3d.col.Sphere(0,0,0,1.);
+			var int = new h3d.scene.Interactive(col, previewSphere);
+			return int;
+		}
+		return null;
+	}
+
 	override function edit( ctx : hide.prefab.EditContext ) {
 		super.edit(ctx);