Explorar o código

added getMeshMaterials, prevent material duplication in getMaterials

Nicolas Cannasse %!s(int64=8) %!d(string=hai) anos
pai
achega
c6a955bf52
Modificáronse 2 ficheiros con 12 adicións e 4 borrados
  1. 5 1
      h3d/scene/Mesh.hx
  2. 7 3
      h3d/scene/MultiMaterial.hx

+ 5 - 1
h3d/scene/Mesh.hx

@@ -12,6 +12,10 @@ class Mesh extends Object {
 		this.material = mat;
 		this.material = mat;
 	}
 	}
 
 
+	public function getMeshMaterials() {
+		return [material];
+	}
+
 	override function getBounds( ?b : h3d.col.Bounds, rec = false ) {
 	override function getBounds( ?b : h3d.col.Bounds, rec = false ) {
 		b = super.getBounds(b, rec);
 		b = super.getBounds(b, rec);
 		if( primitive == null || flags.has(FIgnoreBounds) )
 		if( primitive == null || flags.has(FIgnoreBounds) )
@@ -50,7 +54,7 @@ class Mesh extends Object {
 
 
 	override function getMaterials( ?a : Array<h3d.mat.Material> ) {
 	override function getMaterials( ?a : Array<h3d.mat.Material> ) {
 		if( a == null ) a = [];
 		if( a == null ) a = [];
-		if( material != null ) a.push(material);
+		if( material != null && a.indexOf(material) < 0 ) a.push(material);
 		return super.getMaterials(a);
 		return super.getMaterials(a);
 	}
 	}
 
 

+ 7 - 3
h3d/scene/MultiMaterial.hx

@@ -9,9 +9,13 @@ class MultiMaterial extends Mesh {
 		this.materials = mats == null ? [material] : mats;
 		this.materials = mats == null ? [material] : mats;
 	}
 	}
 
 
+	override function getMeshMaterials() {
+		return materials.copy();
+	}
+
 	override function clone( ?o : Object ) {
 	override function clone( ?o : Object ) {
 		var m = o == null ? new MultiMaterial(null,materials) : cast o;
 		var m = o == null ? new MultiMaterial(null,materials) : cast o;
-		m.materials = [for( m in materials ) cast m.clone()];
+		m.materials = [for( m in materials ) if( m == null ) null else cast m.clone()];
 		super.clone(m);
 		super.clone(m);
 		m.material = m.materials[0];
 		m.material = m.materials[0];
 		return m;
 		return m;
@@ -27,7 +31,7 @@ class MultiMaterial extends Mesh {
 
 
 	override function getMaterialByName( name : String ) : h3d.mat.Material {
 	override function getMaterialByName( name : String ) : h3d.mat.Material {
 		for( m in materials )
 		for( m in materials )
-			if( m.name == name )
+			if( m != null && m.name == name )
 				return m;
 				return m;
 		return super.getMaterialByName(name);
 		return super.getMaterialByName(name);
 	}
 	}
@@ -35,7 +39,7 @@ class MultiMaterial extends Mesh {
 	override function getMaterials( ?a : Array<h3d.mat.Material> ) {
 	override function getMaterials( ?a : Array<h3d.mat.Material> ) {
 		if( a == null ) a = [];
 		if( a == null ) a = [];
 		for( m in materials )
 		for( m in materials )
-			if( m != null )
+			if( m != null && a.indexOf(m) < 0 )
 				a.push(m);
 				a.push(m);
 		for( o in children )
 		for( o in children )
 			o.getMaterials(a);
 			o.getMaterials(a);