Преглед на файлове

pbr alpha and decals support

ncannasse преди 7 години
родител
ревизия
302b0000d0
променени са 4 файла, в които са добавени 39 реда и са изтрити 21 реда
  1. 1 1
      h3d/scene/Scene.hx
  2. 20 9
      h3d/scene/pbr/Renderer.hx
  3. 13 6
      h3d/shader/VolumeDecal.hx
  4. 5 5
      h3d/shader/pbr/PropsImport.hx

+ 1 - 1
h3d/scene/Scene.hx

@@ -267,7 +267,7 @@ class Scene extends Object implements h3d.IDrawable implements hxd.SceneEvents.I
 			var p = hardwarePass;
 			var p = hardwarePass;
 			if( p == null )
 			if( p == null )
 				hardwarePass = p = new h3d.pass.HardwarePick();
 				hardwarePass = p = new h3d.pass.HardwarePick();
-			ctx.setGlobal("depthMap", h3d.mat.Texture.fromColor(0xFF00000, 0));
+			ctx.setGlobal("depthMap", { texture : h3d.mat.Texture.fromColor(0xFF00000, 0) });
 			p.pickX = pixelX;
 			p.pickX = pixelX;
 			p.pickY = pixelY;
 			p.pickY = pixelY;
 			p.setContext(ctx);
 			p.setContext(ctx);

+ 20 - 9
h3d/scene/pbr/Renderer.hx

@@ -96,11 +96,17 @@ class Renderer extends h3d.scene.Renderer {
 	public var exposure(get,set) : Float;
 	public var exposure(get,set) : Float;
 	public var debugMode = 0;
 	public var debugMode = 0;
 
 
+	static var ALPHA : hxsl.Output = Swiz(Value("output.color"),[W]);
 	var output = new h3d.pass.Output("mrt",[
 	var output = new h3d.pass.Output("mrt",[
 		Value("output.color"),
 		Value("output.color"),
-		Vec4([Value("output.normal",3),Value("output.depth",1)]),
-		Vec4([Value("output.metalness"), Value("output.roughness"), Value("output.occlusion"), Const(0)]),
-		Vec4([Value("output.emissive"),Const(0),Const(0),Const(0)])
+		Vec4([Value("output.normal",3),ALPHA]),
+		Vec4([Value("output.metalness"), Value("output.roughness"), Value("output.occlusion"), ALPHA]),
+		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)]),
+		Vec4([Value("output.normal",3), Value("output.normalStrength",1)]),
+		Vec4([Value("output.metalness"), Value("output.roughness"), Value("output.occlusion"), Value("output.pbrStrength")]),
 	]);
 	]);
 
 
 	public function new(env) {
 	public function new(env) {
@@ -113,6 +119,7 @@ class Renderer extends h3d.scene.Renderer {
 		allPasses.push(output);
 		allPasses.push(output);
 		allPasses.push(defaultPass);
 		allPasses.push(defaultPass);
 		allPasses.push(shadows);
 		allPasses.push(shadows);
+		allPasses.push(decalsOutput);
 		refreshProps();
 		refreshProps();
 	}
 	}
 
 
@@ -168,13 +175,17 @@ class Renderer extends h3d.scene.Renderer {
 		pbrDirect.enableShadow = props.shadow.enable;
 		pbrDirect.enableShadow = props.shadow.enable;
 
 
 		var albedo = allocTarget("albedo");
 		var albedo = allocTarget("albedo");
-		var normal = allocTarget("normalDepth",false,1.,RGBA32F);
+		var normal = allocTarget("normalDepth",false,1.,RGBA16F);
 		var pbr = allocTarget("pbr",false,1.);
 		var pbr = allocTarget("pbr",false,1.);
-		var emit = allocTarget("emit",false,1.,RGBA16F);
-		setTargets([albedo,normal,pbr,emit]);
+		var other = allocTarget("other",false,1.,RGBA32F);
+		setTargets([albedo,normal,pbr,other]);
 		clear(0, 1, 0);
 		clear(0, 1, 0);
 		mainDraw();
 		mainDraw();
 
 
+		setTargets([albedo,normal,pbr]);
+		ctx.setGlobal("depthMap",{ texture : other, channel : hxsl.Channel.G });
+		decalsOutput.draw(get("decal"));
+
 		setTarget(albedo);
 		setTarget(albedo);
 		draw("albedo");
 		draw("albedo");
 
 
@@ -191,13 +202,13 @@ class Renderer extends h3d.scene.Renderer {
 			var sp = props.sao;
 			var sp = props.sao;
 			var saoTex = allocTarget("sao",false,sp.size);
 			var saoTex = allocTarget("sao",false,sp.size);
 			setTarget(saoTex);
 			setTarget(saoTex);
-			sao.shader.depthTextureChannel = A;
+			sao.shader.depthTextureChannel = B;
 			sao.shader.normalTextureChannel = R;
 			sao.shader.normalTextureChannel = R;
 			sao.shader.numSamples = sp.samples;
 			sao.shader.numSamples = sp.samples;
 			sao.shader.sampleRadius	= sp.radius;
 			sao.shader.sampleRadius	= sp.radius;
 			sao.shader.intensity = sp.intensity;
 			sao.shader.intensity = sp.intensity;
 			sao.shader.bias = sp.bias * sp.bias;
 			sao.shader.bias = sp.bias * sp.bias;
-			sao.apply(normal,normal,ctx.camera);
+			sao.apply(other,normal,ctx.camera);
 			saoBlur.radius = sp.blur;
 			saoBlur.radius = sp.blur;
 			saoBlur.quality = 0.5;
 			saoBlur.quality = 0.5;
 			saoBlur.apply(ctx, saoTex);
 			saoBlur.apply(ctx, saoTex);
@@ -212,7 +223,7 @@ class Renderer extends h3d.scene.Renderer {
 		pbrProps.albedoTex = albedo;
 		pbrProps.albedoTex = albedo;
 		pbrProps.normalTex = normal;
 		pbrProps.normalTex = normal;
 		pbrProps.pbrTex = pbr;
 		pbrProps.pbrTex = pbr;
-		pbrProps.emitTex = emit;
+		pbrProps.otherTex = other;
 		pbrProps.cameraInverseViewProj = ctx.camera.getInverseViewProj();
 		pbrProps.cameraInverseViewProj = ctx.camera.getInverseViewProj();
 
 
 		pbrDirect.cameraPosition.load(ctx.camera.pos);
 		pbrDirect.cameraPosition.load(ctx.camera.pos);

+ 13 - 6
h3d/shader/VolumeDecal.hx

@@ -6,14 +6,18 @@ class VolumeDecal extends hxsl.Shader {
 
 
 		@:import BaseMesh;
 		@:import BaseMesh;
 
 
-		@global var depthMap : Sampler2D;
+		@global var depthMap : Channel;
 
 
 		@param var scale : Vec2;
 		@param var scale : Vec2;
 		@param var normal : Vec3;
 		@param var normal : Vec3;
+		@param var tangent : Vec3;
+		@const var isCentered : Bool = true;
 		var calculatedUV : Vec2;
 		var calculatedUV : Vec2;
+		var transformedTangent : Vec4;
 
 
-		function vertex() {
-			transformedNormal = normal;
+		function __init__vertex() {
+			transformedNormal = (normal * global.modelView.mat3()).normalize();
+			transformedTangent = vec4((tangent * global.modelView.mat3()).normalize(),1.);
 		}
 		}
 
 
 		function fragment() {
 		function fragment() {
@@ -21,14 +25,16 @@ class VolumeDecal extends hxsl.Shader {
 			var screenPos = projectedPosition.xy / projectedPosition.w;
 			var screenPos = projectedPosition.xy / projectedPosition.w;
 			var ruv = vec4(
 			var ruv = vec4(
 				screenPos,
 				screenPos,
-				unpack(depthMap.get(screenToUv(screenPos))),
+				depthMap.get(screenToUv(screenPos)),
 				1
 				1
 			);
 			);
 			var wpos = ruv * matrix;
 			var wpos = ruv * matrix;
 			var ppos = ruv * camera.inverseViewProj;
 			var ppos = ruv * camera.inverseViewProj;
 			pixelTransformedPosition = ppos.xyz / ppos.w;
 			pixelTransformedPosition = ppos.xyz / ppos.w;
-			calculatedUV = scale * (wpos.xy / wpos.w) + 0.5;
-			if( min(min(calculatedUV.x, calculatedUV.y), min(1 - calculatedUV.x, 1 - calculatedUV.y)) < 0 ) discard;
+			calculatedUV = scale * (wpos.xy / wpos.w);
+			if( isCentered ) calculatedUV += 0.5;
+			if( min(min(calculatedUV.x, calculatedUV.y), min(1 - calculatedUV.x, 1 - calculatedUV.y)) < 0 )
+				discard;
 		}
 		}
 
 
 	};
 	};
@@ -36,6 +42,7 @@ class VolumeDecal extends hxsl.Shader {
 	public function new( objectWidth : Float, objectHeight : Float ) {
 	public function new( objectWidth : Float, objectHeight : Float ) {
 		super();
 		super();
 		normal.set(0, 0, 1);
 		normal.set(0, 0, 1);
+		tangent.set(1, 0, 0);
 		scale.set(1/objectWidth, 1/objectHeight);
 		scale.set(1/objectWidth, 1/objectHeight);
 	}
 	}
 
 

+ 5 - 5
h3d/shader/pbr/PropsImport.hx

@@ -6,7 +6,7 @@ class PropsImport extends hxsl.Shader {
 		@param var albedoTex : Sampler2D;
 		@param var albedoTex : Sampler2D;
 		@param var normalTex : Sampler2D;
 		@param var normalTex : Sampler2D;
 		@param var pbrTex : Sampler2D;
 		@param var pbrTex : Sampler2D;
-		@param var emitTex : Sampler2D;
+		@param var otherTex : Sampler2D;
 		@const var isScreen : Bool = true;
 		@const var isScreen : Bool = true;
 
 
 		@param var cameraInverseViewProj : Mat4;
 		@param var cameraInverseViewProj : Mat4;
@@ -28,15 +28,15 @@ class PropsImport extends hxsl.Shader {
 			albedo = albedoTex.get(uv).rgb;
 			albedo = albedoTex.get(uv).rgb;
 			albedo *= albedo; // gamma correct
 			albedo *= albedo; // gamma correct
 
 
-			var normalDepth = normalTex.get(uv);
-			normal = normalDepth.xyz;
-			depth = normalDepth.w;
+			normal = normalTex.get(uv).xyz;
 			var pbr = pbrTex.get(uv);
 			var pbr = pbrTex.get(uv);
 			metalness = pbr.r;
 			metalness = pbr.r;
 			roughness = pbr.g;
 			roughness = pbr.g;
 			occlusion = pbr.b;
 			occlusion = pbr.b;
 
 
-			emissive = emitTex.get(uv).r;
+			var other = otherTex.get(uv);
+			emissive = other.r;
+			depth = other.g;
 
 
 			pbrSpecularColor = mix(vec3(0.04),albedo,metalness);
 			pbrSpecularColor = mix(vec3(0.04),albedo,metalness);