Ver Fonte

Create Border primitive only if needed

TothBenoit há 1 ano atrás
pai
commit
58437be25c
2 ficheiros alterados com 19 adições e 3 exclusões
  1. 18 2
      h3d/pass/Border.hx
  2. 1 1
      h3d/shader/AlphaMSDF.hx

+ 18 - 2
h3d/pass/Border.hx

@@ -14,9 +14,19 @@ private class BorderShader extends h3d.shader.ScreenShader {
 
 class Border extends ScreenFx<BorderShader> {
 
+	var width(default, null) : Int;
+	var height(default, null) : Int;
+	var size(default, null) : Int;
+
 	public function new( width : Int, height : Int, size : Int = 1 ) {
 		super(new BorderShader());
+		this.width = width;
+		this.height = height;
+		this.size = height;
+		shader.color.set(1,1,1,1);
+	}
 
+	function createPrimitive() {
 		var bbuf = new hxd.FloatBuffer();
 		inline function add(x, y) {
 			bbuf.push((x / width) * 2 - 1);
@@ -43,12 +53,18 @@ class Border extends ScreenFx<BorderShader> {
 		add(width, height);
 
 		this.primitive = new h3d.prim.RawPrimitive({ vbuf : bbuf, format : hxd.BufferFormat.make([{ name : "position", type : DVec2 }]) }, true);
-		shader.color.set(1,1,1,1);
+	}
+
+	override function render() {
+		if (primitive == null)
+			createPrimitive();
+		super.render();
 	}
 
 	override function dispose() {
+		if (primitive != null)
+			this.primitive.dispose();
 		super.dispose();
-		this.primitive.dispose();
 	}
 
 }

+ 1 - 1
h3d/shader/AlphaMSDF.hx

@@ -20,7 +20,7 @@ class AlphaMSDF extends hxsl.Shader {
 		}
 
 		function fragment() {
-				var sample = texture.get(calculatedUV);
+			var sample = texture.get(calculatedUV);
 			var sd = median(sample.r, sample.g, sample.b);
     		var screenPxDistance = screenPxRange(calculatedUV)*(sd - 0.5);
     		pixelColor.a = clamp(screenPxDistance + 0.5, 0.0, 1.0);