Sfoglia il codice sorgente

fixed Glow filter with alpha premult

ncannasse 6 anni fa
parent
commit
586c4f78c9
2 ha cambiato i file con 15 aggiunte e 11 eliminazioni
  1. 9 5
      h2d/filter/Glow.hx
  2. 6 6
      h3d/shader/Blur.hx

+ 9 - 5
h2d/filter/Glow.hx

@@ -17,19 +17,23 @@ class Glow extends Blur {
 
 	function setParams() {
 		pass.shader.fixedColor.setColor(color);
-		pass.shader.fixedColor.w = alpha;
+		pass.shader.fixedColor.w = smoothColor ? alpha * 1.5 /* more accurate ramp */ : alpha;
 		pass.shader.smoothFixedColor = smoothColor;
 	}
 
 	override function draw( ctx : RenderContext, t : h2d.Tile ) {
 		setParams();
+		var tex = t.getTexture();
+		var old = tex.filter;
 		var save = ctx.textures.allocTarget("glowSave", t.width, t.height, false);
-		h3d.pass.Copy.run(t.getTexture(), save, None);
-		pass.apply(ctx, t.getTexture());
+		h3d.pass.Copy.run(tex, save, None);
+		tex.filter = Linear;
+		pass.apply(ctx, tex);
+		tex.filter = old;
 		if( knockout )
-			h3d.pass.Copy.run(save, t.getTexture(), Erase);
+			h3d.pass.Copy.run(save, tex, Erase);
 		else
-			h3d.pass.Copy.run(save, t.getTexture(), Alpha);
+			h3d.pass.Copy.run(save, tex, Alpha);
 		return t;
 	}
 

+ 6 - 6
h3d/shader/Blur.hx

@@ -43,7 +43,7 @@ class Blur extends ScreenShader {
 					c = mix(c, ccur, ((d - 0.001).max(0.) * 100000).min(1.));
 					color += c * values[i < 0 ? -i : i];
 				}
-				output.color = color;
+				pixelColor = color;
 			}
 			else if( isDepth ) {
 				var val = 0.;
@@ -51,21 +51,21 @@ class Blur extends ScreenShader {
 					if( isCube ) val += unpack(cubeTexture.get(vec3((input.uv + pixel * offsets[i < 0 ? -i : i] * i )* 2.0 - 1.0, 1) * cubeDir)) * values[i < 0 ? -i : i];
 					else val += unpack(texture.get(input.uv + pixel * offsets[i < 0 ? -i : i] * i)) * values[i < 0 ? -i : i];
 				}
-				output.color = pack(val.min(0.9999999));
+				pixelColor = pack(val.min(0.9999999));
 			} else {
 				var color = vec4(0, 0, 0, 0);
 				@unroll for( i in -Quality + 1...Quality ){
 					if( isCube ) color += cubeTexture.get(vec3((input.uv + pixel * offsets[i < 0 ? -i : i] * i )* 2.0 - 1.0, 1) * cubeDir) * values[i < 0 ? -i : i];
 					else color += texture.get(input.uv + pixel * offsets[i < 0 ? -i : i] * i) * values[i < 0 ? -i : i];
 				}
-				output.color = color;
+				pixelColor = color;
 			}
 			if( hasFixedColor ) {
-				output.color.rgb = fixedColor.rgb;
 				if( smoothFixedColor )
-					output.color.a *= fixedColor.a;
+					pixelColor.a *= fixedColor.a;
 				else
-					output.color.a = fixedColor.a * float(output.color.a > 0);
+					pixelColor.a = fixedColor.a * float(pixelColor.a > 0);
+				pixelColor.rgb = fixedColor.rgb * pixelColor.a; // premult required for 2D filters
 			}
 		}