Browse Source

added material.color persistence to meshbatch (allow both per-instance and global material color)

Nicolas Cannasse 6 years ago
parent
commit
0ecf70553d
2 changed files with 41 additions and 0 deletions
  1. 25 0
      h3d/scene/MeshBatch.hx
  2. 16 0
      h3d/shader/ColorMult.hx

+ 25 - 0
h3d/scene/MeshBatch.hx

@@ -31,6 +31,13 @@ class MeshBatch extends Mesh {
 	var indexCount : Int;
 	var indexCount : Int;
 	var modelViewID = hxsl.Globals.allocID("global.modelView");
 	var modelViewID = hxsl.Globals.allocID("global.modelView");
 	var modelViewInverseID = hxsl.Globals.allocID("global.modelViewInverse");
 	var modelViewInverseID = hxsl.Globals.allocID("global.modelViewInverse");
+	var colorSave = new h3d.Vector();
+	var colorMult : h3d.shader.ColorMult;
+	
+	/**
+		Tells if we can use material.color as a global multiply over each instance color (default: true)
+	**/
+	public var allowGlobalMaterialColor : Bool = true;
 
 
 	/**
 	/**
 	 * 	If set, use this position in emitInstance() instead MeshBatch absolute position
 	 * 	If set, use this position in emitInstance() instead MeshBatch absolute position
@@ -135,11 +142,27 @@ class MeshBatch extends Mesh {
 	public function begin( maxCount : Int ) {
 	public function begin( maxCount : Int ) {
 		if( maxCount > shaderInstances )
 		if( maxCount > shaderInstances )
 			shadersChanged = true;
 			shadersChanged = true;
+		colorSave.load(material.color);
 		curInstances = 0;
 		curInstances = 0;
 		maxInstances = maxCount;
 		maxInstances = maxCount;
 		if( shadersChanged ) {
 		if( shadersChanged ) {
+			if( colorMult != null ) {
+				material.mainPass.removeShader(colorMult);
+				colorMult = null;
+			}
 			initShadersMapping();
 			initShadersMapping();
 			shadersChanged = false;
 			shadersChanged = false;
+			if( allowGlobalMaterialColor ) {
+				if( colorMult == null ) {
+					colorMult = new h3d.shader.ColorMult();
+					material.mainPass.addShader(colorMult);
+				}
+			} else {
+				if( colorMult != null ) {
+					material.mainPass.removeShader(colorMult);
+					colorMult = null;
+				}
+			}
 		}
 		}
 	}
 	}
 
 
@@ -226,6 +249,7 @@ class MeshBatch extends Mesh {
 			syncData(p);
 			syncData(p);
 			p = p.next;
 			p = p.next;
 		}
 		}
+		if( allowGlobalMaterialColor ) material.color.load(colorSave);
 		curInstances++;
 		curInstances++;
 	}
 	}
 
 
@@ -238,6 +262,7 @@ class MeshBatch extends Mesh {
 			p = p.next;
 			p = p.next;
 		}
 		}
 		instanced.commands.setCommand(curInstances,indexCount);
 		instanced.commands.setCommand(curInstances,indexCount);
+		if( colorMult != null ) colorMult.color.load(material.color);
 	}
 	}
 
 
 	override function emit(ctx:RenderContext) {
 	override function emit(ctx:RenderContext) {

+ 16 - 0
h3d/shader/ColorMult.hx

@@ -0,0 +1,16 @@
+package h3d.shader;
+
+class ColorMult extends hxsl.Shader {
+
+	static var SRC = {
+		var pixelColor : Vec4;
+
+		@param var color : Vec4;
+
+		function fragment() {
+			pixelColor *= color;
+		}
+
+	};
+
+}