Browse Source

glow filter working, use temp target textures

Nicolas Cannasse 10 years ago
parent
commit
fda7196f94
6 changed files with 54 additions and 5 deletions
  1. 1 1
      h2d/Sprite.hx
  2. 1 1
      h2d/filter/Blur.hx
  3. 25 0
      h2d/filter/Glow.hx
  4. 7 0
      h3d/shader/Blur.hx
  5. 20 3
      samples/filters/Main.hx
  6. BIN
      samples/res/hxlogo.png

+ 1 - 1
h2d/Sprite.hx

@@ -426,7 +426,7 @@ class Sprite {
 		var height = Math.ceil(total.yMax - yMin - 1e-10);
 		if( width <= 0 || height <= 0 ) return;
 
-		var t = new h3d.mat.Texture(width, height, [Target]);
+		var t = ctx.textures.allocTarget("filterTemp", ctx, width, height, false);
 		ctx.pushTarget(t, xMin, yMin);
 
 		// reset transform and update childs

+ 1 - 1
h2d/filter/Blur.hx

@@ -36,7 +36,7 @@ class Blur extends Filter {
 	}
 
 	override function draw( ctx : RenderContext, t : h2d.Tile ) {
-		pass.apply(t.getTexture());
+		pass.apply(t.getTexture(), ctx.textures.allocTarget("blurTmp",ctx,t.width,t.height,false));
 		return t;
 	}
 

+ 25 - 0
h2d/filter/Glow.hx

@@ -0,0 +1,25 @@
+package h2d.filter;
+
+class Glow extends Blur {
+
+	public var color : Int;
+	public var alpha : Float;
+
+	public function new( color : Int = 0xFFFFFF, alpha = 1., quality = 1, sigma = 1., passes = 1 ) {
+		super(quality, sigma, passes);
+		this.color = color;
+		this.alpha = alpha;
+		pass.shader.hasFixedColor = true;
+	}
+
+	override function draw( ctx : RenderContext, t : h2d.Tile ) {
+		pass.shader.fixedColor.setColor(color);
+		pass.shader.fixedColor.w = alpha;
+		var save = ctx.textures.allocTarget("glowSave", ctx, t.width, t.height, false);
+		h3d.pass.Copy.run(t.getTexture(), save, None);
+		pass.apply(t.getTexture(), ctx.textures.allocTarget("glowTmp", ctx, t.width, t.height, false));
+		h3d.pass.Copy.run(save, t.getTexture(), Alpha);
+		return t;
+	}
+
+}

+ 7 - 0
h3d/shader/Blur.hx

@@ -10,6 +10,9 @@ class Blur extends ScreenShader {
 		@param var values : Array<Float,Quality>;
 		@param var pixel : Vec2;
 
+		@const var hasFixedColor : Bool;
+		@param var fixedColor : Vec4;
+
 		function fragment() {
 			if( isDepth ) {
 				var val = 0.;
@@ -22,6 +25,10 @@ class Blur extends ScreenShader {
 					color += texture.get(input.uv + pixel * float(i)) * values[i < 0 ? -i : i];
 				output.color = color;
 			}
+			if( hasFixedColor ) {
+				output.color.rgb = fixedColor.rgb;
+				output.color.a *= fixedColor.a;
+			}
 		}
 	}
 

+ 20 - 3
samples/filters/Main.hx

@@ -5,10 +5,9 @@ class Main extends hxd.App {
 	var spr : h2d.Sprite;
 
 	override function init() {
-		engine.debug = true;
-		var scale = 1;
+		engine.backgroundColor = 0x002000;
+		var scale = 4;
 		spr = new h2d.Sprite(s2d);
-		spr.filters = [new h2d.filter.Blur(2,100,3)];
 		spr.x = s2d.width * 0.5;
 		spr.y = s2d.height * 0.5;
 		spr.scale(scale);
@@ -17,10 +16,28 @@ class Main extends hxd.App {
 		bmp.colorKey = 0xFFFFFF;
 		bmp.x = -bmp.tile.width * 0.5 * bmp.scaleX;
 		bmp.y = -bmp.tile.height * 0.5 * bmp.scaleY;
+
+		setFilters(2);
+
+		var help = new h2d.Text(hxd.Res.customFont.toFont(), s2d);
+		help.x = help.y = 5;
+		help.text = "1:Blur 2:Glow";
 	}
 
 	override function update(dt:Float) {
 		spr.rotation += 0.01 * dt;
+		for( i in 1...3 )
+			if( K.isPressed(K.NUMBER_0 + i) )
+				setFilters(i);
+	}
+
+	function setFilters(i) {
+		switch( i ) {
+		case 1:
+			spr.filters = [new h2d.filter.Blur(2, 100, 3)];
+		case 2:
+			spr.filters = [new h2d.filter.Glow(0xFF00FF, 100, 1)];
+		}
 	}
 
 	static function main() {

BIN
samples/res/hxlogo.png