소스 검색

add depth/normal dependant mode

bstouls 10 년 전
부모
커밋
65cd18e521
2개의 변경된 파일62개의 추가작업 그리고 4개의 파일을 삭제
  1. 26 1
      h3d/pass/Blur.hx
  2. 36 3
      h3d/shader/Blur.hx

+ 26 - 1
h3d/pass/Blur.hx

@@ -17,6 +17,13 @@ class Blur extends ScreenFx<h3d.shader.Blur> {
 	**/
 	public var passes : Int;
 
+
+	public var depthBlur(default,set) : {
+		depths : h3d.mat.Texture,
+		normals : h3d.mat.Texture,
+		camera : h3d.Camera,
+	};
+
 	var values : Array<Float>;
 
 	public function new(quality = 1, passes = 1, sigma = 1.) {
@@ -36,6 +43,21 @@ class Blur extends ScreenFx<h3d.shader.Blur> {
 		return sigma = s;
 	}
 
+
+	function set_depthBlur(d) {
+		depthBlur = d;
+		if( d == null ) {
+			shader.isDepthDependant = false;
+			shader.depthTexture = null;
+			shader.normalTexture = null;
+		} else {
+			shader.isDepthDependant = true;
+			shader.depthTexture = d.depths;
+			shader.normalTexture = d.normals;
+		}
+		return d;
+	}
+
 	function gauss( x:Int, s:Float ) : Float {
 		if( s <= 0 ) return x == 0 ? 1 : 0;
 		var sq = s * s;
@@ -56,7 +78,7 @@ class Blur extends ScreenFx<h3d.shader.Blur> {
 			values[i] /= tot;
 	}
 
-	public function apply( src : h3d.mat.Texture, ?tmp : h3d.mat.Texture, ?output : h3d.mat.Texture, isDepth = false ) {
+	public function apply( src : h3d.mat.Texture, ?tmp : h3d.mat.Texture, ?output : h3d.mat.Texture, ?isDepth = false ) {
 
 		if( quality <= 0 || passes <= 0 || sigma <= 0 ) return;
 
@@ -72,6 +94,9 @@ class Blur extends ScreenFx<h3d.shader.Blur> {
 		shader.values = values;
 		shader.isDepth = isDepth;
 
+		if( depthBlur != null )
+			shader.cameraInverseViewProj = depthBlur.camera.getInverseViewProj();
+
 		for( i in 0...passes ) {
 			shader.texture = src;
 			shader.pixel.set(1 / src.width, 0);

+ 36 - 3
h3d/shader/Blur.hx

@@ -4,7 +4,10 @@ class Blur extends ScreenShader {
 
 	static var SRC = {
 
+		@param var cameraInverseViewProj : Mat4;
+
 		@param var texture : Sampler2D;
+		@param var depthTexture : Sampler2D;
 		@param @const var Quality : Int;
 		@param @const var isDepth : Bool;
 		@param var values : Array<Float,Quality>;
@@ -14,15 +17,37 @@ class Blur extends ScreenShader {
 		@const var smoothFixedColor : Bool;
 		@param var fixedColor : Vec4;
 
+		@param @const var isDepthDependant : Bool;
+		@param @const var hasNormal : Bool;
+		@param var normalTexture : Sampler2D;
+
 		function fragment() {
-			if( isDepth ) {
+			if( isDepthDependant ) {
+				var pcur = getPosition(input.uv);
+				var ccur = texture.get(input.uv);
+				var color = vec4(0, 0, 0, 0);
+				var ncur = unpackNormal(normalTexture.get(input.uv));
+				for( i in -Quality + 1...Quality ) {
+					var uv = input.uv + pixel * float(i);
+					var c = texture.get(uv);
+					var p = getPosition(uv);
+					var d = (p - pcur).dot(p - pcur);
+					var n = unpackNormal(normalTexture.get(uv));
+
+					c = mix(ccur, c, ncur.dot(n));
+					c = mix(c, ccur, ((d - 0.001).max(0.) * 100000).min(1.));
+					color += c * values[i < 0 ? -i : i];
+				}
+				output.color = color;
+			}
+			else if( isDepth ) {
 				var val = 0.;
-				for( i in -Quality+1...Quality )
+				for( i in -Quality + 1...Quality )
 					val += unpack(texture.get(input.uv + pixel * float(i))) * values[i < 0 ? -i : i];
 				output.color = pack(val.min(0.9999999));
 			} else {
 				var color = vec4(0, 0, 0, 0);
-				for( i in -Quality+1...Quality )
+				for( i in -Quality + 1...Quality )
 					color += texture.get(input.uv + pixel * float(i)) * values[i < 0 ? -i : i];
 				output.color = color;
 			}
@@ -34,6 +59,14 @@ class Blur extends ScreenShader {
 					output.color.a = fixedColor.a * float(output.color.a > 0);
 			}
 		}
+
+		function getPosition( uv : Vec2 ) : Vec3 {
+			var depth = unpack(depthTexture.get(uv));
+			var uv2 = (uv - 0.5) * vec2(2, -2);
+			var temp = vec4(uv2, depth, 1) * cameraInverseViewProj;
+			var originWS = temp.xyz / temp.w;
+			return originWS;
+		}
 	}
 
 }