Browse Source

- Fix indirect lighting

ShiroSmith 7 years ago
parent
commit
e123fe8073
3 changed files with 31 additions and 14 deletions
  1. 14 3
      h3d/scene/pbr/Renderer.hx
  2. 11 9
      h3d/scene/pbr/VolumetricLightmap.hx
  3. 6 2
      h3d/shader/pbr/Lighting.hx

+ 14 - 3
h3d/scene/pbr/Renderer.hx

@@ -94,7 +94,6 @@ class Renderer extends h3d.scene.Renderer {
 		pbrOutIndirect.addShader(pbrProps);
 		pbrOutIndirect.pass.setBlendMode(Add);
 		pbrOutIndirect.pass.stencil = new h3d.mat.Stencil();
-		pbrOutIndirect.pass.stencil.setFunc(NotEqual, 0x80, 0x80, 0x80);
 		pbrOutIndirect.pass.stencil.setOp(Keep, Keep, Keep);
 		allPasses.push(output);
 		allPasses.push(defaultPass);
@@ -165,6 +164,7 @@ class Renderer extends h3d.scene.Renderer {
 		var pbr = allocTarget("pbr",false,1.);
 		var other = allocTarget("other",false,1.,RGBA32F);
 
+		ctx.setGlobal("albedoMap",{ texture : albedo, 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 });
@@ -241,11 +241,13 @@ class Renderer extends h3d.scene.Renderer {
 		pbrIndirect.cameraInvViewProj.load(ctx.camera.getInverseViewProj());
 		switch( renderMode ) {
 		case Default:
-			pbrIndirect.drawIndirect = true;
+			pbrIndirect.drawIndirectDiffuse = true;
+			pbrIndirect.drawIndirectSpecular= true;
 			pbrIndirect.showSky = skyMode != Hide;
 			pbrIndirect.skyMap = skyMode == Irrad ? env.diffuse : env.env;
 		case LightProbe:
-			pbrIndirect.drawIndirect = false;
+			pbrIndirect.drawIndirectDiffuse = false;
+			pbrIndirect.drawIndirectSpecular= false;
 			pbrIndirect.showSky = true;
 			pbrIndirect.skyMap = env.env;
 		}
@@ -282,6 +284,15 @@ class Renderer extends h3d.scene.Renderer {
 		ctx.extraShaders = null;
 
 		pbrProps.isScreen = true;
+
+		pbrIndirect.drawIndirectDiffuse = true;
+		pbrIndirect.drawIndirectSpecular = false;
+		pbrOutIndirect.pass.stencil.setFunc(NotEqual, 0x80, 0x80, 0x80);
+		pbrOutIndirect.render();
+
+		pbrIndirect.drawIndirectDiffuse = false;
+		pbrIndirect.drawIndirectSpecular = true;
+		pbrOutIndirect.pass.stencil.setFunc(Always);
 		pbrOutIndirect.render();
 
 		apply(AfterHdr);

+ 11 - 9
h3d/scene/pbr/VolumetricLightmap.hx

@@ -13,6 +13,12 @@ class VolumetricLightmap extends h3d.scene.Mesh {
 
 	var shader : h3d.shader.pbr.VolumetricLightmap;
 
+	function set_voxelSize(newSize) :h3d.Vector {
+		voxelSize = newSize;
+		updateProbeCount();
+		return voxelSize;
+	}
+
 	public function new(?parent) {
 		super(new h3d.prim.Cube(1,1,1,false), null, parent);
 		shader = new h3d.shader.pbr.VolumetricLightmap();
@@ -55,7 +61,7 @@ class VolumetricLightmap extends h3d.scene.Mesh {
 		return sh;
 	}
 
-	public function getCoefCount() : Int{
+	public inline function getCoefCount() : Int{
 		return shOrder * shOrder;
 	}
 
@@ -85,12 +91,6 @@ class VolumetricLightmap extends h3d.scene.Mesh {
 		}
 	}
 
-	function set_voxelSize(newSize) :h3d.Vector {
-		voxelSize = newSize;
-		updateProbeCount();
-		return voxelSize;
-	}
-
 	public function updateProbeCount(){
 		syncPos();
 		var scale = absPos.getScale();
@@ -100,13 +100,15 @@ class VolumetricLightmap extends h3d.scene.Mesh {
 	}
 
 	public function load( bytes : haxe.io.Bytes ) {
+		if( bytes.length == 0 )
+			return false;
 		bytes = haxe.zip.Uncompress.run(bytes);
 		var count = getProbeCount();
-		if( bytes.length != count * shOrder * shOrder * 4 * 4 )
+		if( bytes.length != count * getCoefCount() * 4 * 4 )
 			return false;
 		lastBakedProbeIndex = count;
 		if( lightProbeTexture != null ) lightProbeTexture.dispose();
-		lightProbeTexture = new h3d.mat.Texture(probeCount.x * shOrder * shOrder, probeCount.y * probeCount.z, [Dynamic], RGBA32F);
+		lightProbeTexture = new h3d.mat.Texture(probeCount.x * getCoefCount(), probeCount.y * probeCount.z, [Dynamic], RGBA32F);
 		lightProbeTexture.filter = Nearest;
 		lightProbeTexture.uploadPixels(new hxd.Pixels(lightProbeTexture.width, lightProbeTexture.height, bytes, RGBA32F));
 		return true;

+ 6 - 2
h3d/shader/pbr/Lighting.hx

@@ -11,7 +11,8 @@ class Indirect extends PropsDefinition {
 		@param var irrPower : Float;
 
 		@const var showSky : Bool;
-		@const var drawIndirect : Bool;
+		@const var drawIndirectDiffuse : Bool;
+		@const var drawIndirectSpecular : Bool;
 		@param var skyMap : SamplerCube;
 		@param var cameraInvViewProj : Mat4;
 		@param var emissivePower : Float;
@@ -35,8 +36,11 @@ class Indirect extends PropsDefinition {
 				var envBRDF = irrLut.get(vec2(roughness, NdV));
 				var specular = envSpec * (F * envBRDF.x + envBRDF.y);
 
+				if( !drawIndirectDiffuse ) diffuse = vec3(0.);
+				if( !drawIndirectSpecular ) specular = vec3(0.);
+
 				var indirect = (diffuse * (1 - metalness) * (1 - F) + specular) * irrPower;
-				if( !drawIndirect ) indirect = vec3(0.);
+
 				pixelColor.rgb += indirect * occlusion + albedo * emissive * emissivePower;
 			}
 		}