Selaa lähdekoodia

fixed ColorMatrix alpha handling

Nicolas Cannasse 8 vuotta sitten
vanhempi
commit
1e7503b702
2 muutettua tiedostoa jossa 18 lisäystä ja 6 poistoa
  1. 8 3
      h3d/pass/ColorMatrix.hx
  2. 10 3
      samples/Filters.hx

+ 8 - 3
h3d/pass/ColorMatrix.hx

@@ -7,6 +7,7 @@ class ColorMatrixShader extends h3d.shader.ScreenShader {
 		@param var texture : Sampler2D;
 		@param var matrix : Mat4;
 
+		@const var useAlpha : Bool;
 		@const var useMask : Bool;
 		@const var maskInvert : Bool;
 		@const var hasSecondMatrix : Bool;
@@ -17,15 +18,19 @@ class ColorMatrixShader extends h3d.shader.ScreenShader {
 		@param var maskPower : Float;
 		@param var maskChannel : Vec4;
 
+		function apply( color : Vec4, mat : Mat4 ) : Vec4 {
+			return useAlpha ? color * mat : vec4(color.rgb * matrix.mat3x4(), (color * matrix).a);
+		}
+
 		function fragment() {
 			if( useMask ) {
 				var color = texture.get(input.uv);
 				var uv = vec3(input.uv, 1);
 				var k = pow(mask.get( vec2(uv.dot(maskMatA), uv.dot(maskMatB)) ).dot(maskChannel), maskPower);
-				var color2 = hasSecondMatrix ? color * matrix2 : color;
-				output.color = maskInvert ? mix(color2, color * matrix, k) : mix(color * matrix, color2, k);
+				var color2 = hasSecondMatrix ? apply(color,matrix2) : color;
+				output.color = maskInvert ? mix(color2, apply(color,matrix), k) : mix(apply(color,matrix), color2, k);
 			} else
-				output.color = texture.get(input.uv) * matrix;
+				output.color = apply(texture.get(input.uv),matrix);
 		}
 
 	};

+ 10 - 3
samples/Filters.hx

@@ -22,11 +22,11 @@ class Filters extends hxd.App {
 
 		var help = new h2d.Text(hxd.Res.customFont.toFont(), s2d);
 		help.x = help.y = 5;
-		help.text = "0:Disable 1:Blur 2:Glow 3:DropShadow 4:Displacement 5:Glow(Knockout) 6:Mix +/-:Scale";
+		help.text = "0:Disable 1:Blur 2:Glow 3:DropShadow 4:Displacement 5:Glow(Knockout) 6:Mix 7:ColorMatrix +/-:Scale";
 	}
 
 	override function update(dt:Float) {
-		for( i in 0...7 )
+		for( i in 0...8 )
 			if( K.isPressed(K.NUMBER_0 + i) || K.isPressed(K.NUMPAD_0+i) )
 				setFilters(i);
 		if( K.isPressed(K.NUMPAD_ADD) ) {
@@ -65,7 +65,14 @@ class Filters extends hxd.App {
 		case 6:
 			var g = new h2d.filter.Glow(0xFFA500, 50, 2, 2);
 			g.knockout = true;
-			spr.filters = [g, new h2d.filter.Displacement(disp,3,3), new h2d.filter.Blur(3,2,0.8), new h2d.filter.DropShadow(8, Math.PI/4,0,1,3,3,0.5)];
+			spr.filters = [g, new h2d.filter.Displacement(disp, 3, 3), new h2d.filter.Blur(3, 2, 0.8), new h2d.filter.DropShadow(8, Math.PI / 4, 0, 1, 3, 3, 0.5)];
+		case 7:
+			var m = new h3d.Matrix();
+			m.identity();
+			m.colorContrast(0.5);
+			m.colorHue(Math.PI / 4);
+			m.colorSaturation(-0.5);
+			spr.filters = [new h2d.filter.ColorMatrix(m)];
 		}
 	}