Browse Source

Update decals

ShiroSmith 6 years ago
parent
commit
fba350dc90
2 changed files with 110 additions and 9 deletions
  1. 26 8
      h3d/scene/pbr/Renderer.hx
  2. 84 1
      h3d/shader/pbr/VolumeDecal.hx

+ 26 - 8
h3d/scene/pbr/Renderer.hx

@@ -42,10 +42,21 @@ typedef RenderProps = {
 	var occlusion : Float;
 }
 
+class DepthCopy extends h3d.shader.ScreenShader {
+
+	static var SRC = {
+		@ignore @param var depthTexture : Channel;
+		function fragment() {
+			pixelColor = vec4(depthTexture.get(calculatedUV));
+		}
+	}
+}
+
 class Renderer extends h3d.scene.Renderer {
 	var slides = new h3d.pass.ScreenFx(new h3d.shader.pbr.Slides());
 	var pbrOut = new h3d.pass.ScreenFx(new h3d.shader.ScreenShader());
 	var tonemap = new h3d.pass.ScreenFx(new h3d.shader.pbr.ToneMapping());
+	var depthCopy = new h3d.pass.ScreenFx(new DepthCopy());
 	var pbrLightPass : h3d.mat.Pass;
 	var screenLightPass : h3d.pass.ScreenFx<h3d.shader.ScreenShader>;
 	var fxaa = new h3d.pass.FXAA();
@@ -66,8 +77,7 @@ class Renderer extends h3d.scene.Renderer {
 		Value("output.color"),
 		Vec4([Value("output.normal",3),ALPHA]),
 		Vec4([Value("output.metalness"), Value("output.roughness"), Value("output.occlusion"), ALPHA]),
-		Vec4([Value("output.emissive"),Const(0),Const(0), ALPHA /* ? */]),
-		Vec4([Value("output.depth"), Const(0),Const(0),Const(0)])
+		Vec4([Value("output.emissive"),Value("output.depth"),Const(0), ALPHA /* ? */])
 	]);
 	var decalsOutput = new h3d.pass.Output("decals",[
 		Vec4([Swiz(Value("output.color"),[X,Y,Z]), Value("output.albedoStrength",1)]),
@@ -76,6 +86,7 @@ class Renderer extends h3d.scene.Renderer {
 		Vec4([Value("output.emissive"), Const(0), Const(0), Const(1)]),
 	]);
 
+
 	public function new(env) {
 		super();
 		this.env = env;
@@ -113,7 +124,7 @@ class Renderer extends h3d.scene.Renderer {
 				to be discarded when the camera is inside its volume.
 			*/
 			pbrLightPass.culling = Front;
-			pbrLightPass.depth(false, Greater);
+			pbrLightPass.depth(false, GreaterEqual);
 			pbrLightPass.enableLights = true;
 		}
 		ctx.pbrLightPass = pbrLightPass;
@@ -167,22 +178,29 @@ class Renderer extends h3d.scene.Renderer {
 
 		drawShadows();
 
-		var albedo = allocTarget("albedo");
-		var normal = allocTarget("normalDepth",false,1.,RGBA16F);
+		var albedo = allocTarget("albedo", true, 1.);
+		var normal = allocTarget("normal",false,1.,RGBA16F);
 		var pbr = allocTarget("pbr",false,1.);
 		var other = allocTarget("other",false,1.,RGBA32F);
-		var depth = allocTarget("depth",false,1.,R32F);
 
 		ctx.setGlobal("albedoMap",{ texture : albedo, channel : hxsl.Channel.R });
-		ctx.setGlobal("depthMap",{ texture : depth, channel : hxsl.Channel.R });
+		ctx.setGlobal("depthMap",{ texture : other, channel : hxsl.Channel.G });
 		ctx.setGlobal("normalMap",{ texture : normal, channel : hxsl.Channel.R });
 		ctx.setGlobal("occlusionMap",{ texture : pbr, channel : hxsl.Channel.B });
 		ctx.setGlobal("bloom",null);
 
-		setTargets([albedo,normal,pbr,other,depth]);
+		setTargets([albedo,normal,pbr,other]);
 		clear(0, 1, 0);
 		mainDraw();
 
+		var depth = allocTarget("depth",false,1.,R32F);
+		var depthMap = ctx.getGlobal("depthMap");
+		depthCopy.shader.depthTexture = depthMap.texture;
+		depthCopy.shader.depthTextureChannel = depthMap.channel;
+		setTargets([depth]);
+		depthCopy.render();
+		ctx.setGlobal("depthMap",{ texture : depth, channel : hxsl.Channel.R });
+
 		setTargets([albedo,normal,pbr,other]);
 		decalsOutput.draw(get("decal"));
 

+ 84 - 1
h3d/shader/pbr/VolumeDecal.hx

@@ -1,6 +1,89 @@
 package h3d.shader.pbr;
 
-class VolumeDecal extends hxsl.Shader {
+class DecalOverlay extends hxsl.Shader {
+	static var SRC = {
+
+		@global var global : {
+			var time : Float;
+			var pixelSize : Vec2;
+			@perObject var modelView : Mat4;
+			@perObject var modelViewInverse : Mat4;
+		};
+
+		@global var camera : {
+			var view : Mat4;
+			var proj : Mat4;
+			var position : Vec3;
+			var projFlip : Float;
+			var projDiag : Vec3;
+			var viewProj : Mat4;
+			var inverseViewProj : Mat4;
+			var zNear : Float;
+			var zFar : Float;
+			@var var dir : Vec3;
+		};
+
+		var output : {
+			color : Vec4
+		};
+
+		@const var CENTERED : Bool;
+
+		@global var depthMap : Channel;
+
+		@param var scale : Vec3;
+		@param var minBound : Vec3;
+		@param var maxBound : Vec3;
+		@param var maxAngle : Float;
+		@param var fadePower : Float;
+		@param var fadeStart : Float;
+		@param var fadeEnd : Float;
+		@param var emissive : Float;
+
+		@param var colorTexture : Sampler2D;
+
+		var calculatedUV : Vec2;
+		var pixelColor : Vec4;
+		var pixelTransformedPosition : Vec3;
+		var projectedPosition : Vec4;
+
+		function outsideBounds() : Bool {
+			return (pixelTransformedPosition.x < minBound.x || pixelTransformedPosition.x > maxBound.x ||
+					pixelTransformedPosition.y < minBound.y || pixelTransformedPosition.y > maxBound.y ||
+			 		pixelTransformedPosition.z < minBound.z || pixelTransformedPosition.z > maxBound.z );
+		}
+
+		function fragment() {
+
+			var matrix = camera.inverseViewProj * global.modelViewInverse;
+			var screenPos = projectedPosition.xy / projectedPosition.w;
+			var depth = depthMap.get(screenToUv(screenPos));
+			var ruv = vec4( screenPos, depth, 1 );
+			var wpos = ruv * matrix;
+			var ppos = ruv * camera.inverseViewProj;
+
+			pixelTransformedPosition = ppos.xyz / ppos.w;
+			var pos = (wpos.xyz / wpos.w);
+			calculatedUV = pos.xy;
+			var fadeFactor = 1 - clamp( pow( max( 0.0, abs(pos.z) - fadeStart) / (fadeEnd - fadeStart), fadePower), 0, 1);
+
+			if( CENTERED )
+				calculatedUV += 0.5;
+
+			if(	outsideBounds() )
+				discard;
+
+			var color = colorTexture.get(calculatedUV);
+			pixelColor = color + color * emissive;
+		}
+	}
+
+	public function new( ) {
+		super();
+	}
+}
+
+class DecalPBR extends hxsl.Shader {
 
 	static var SRC = {