Przeglądaj źródła

- Fix frustum culling for pointlights

ShiroSmith 7 lat temu
rodzic
commit
e2151648a4
2 zmienionych plików z 27 dodań i 20 usunięć
  1. 4 10
      h3d/pass/PointShadowMap.hx
  2. 23 10
      h3d/scene/pbr/PointLight.hx

+ 4 - 10
h3d/pass/PointShadowMap.hx

@@ -55,11 +55,13 @@ class PointShadowMap extends Shadows {
 	}
 
 	function syncShader(texture) {
+		var absPos = light.getAbsPos();
+		var pointLight = cast(light, h3d.scene.pbr.PointLight);
 		pshader.shadowMap = texture;
 		pshader.shadowBias = bias;
 		pshader.shadowPower = power;
-		pshader.lightPos = lightCamera.pos;
-		pshader.zFar = lightCamera.zFar;
+		pshader.lightPos = new h3d.Vector(absPos.tx, absPos.ty, absPos.tz);
+		pshader.zFar = pointLight.range;
 	}
 
 	override function saveStaticData() {
@@ -70,9 +72,6 @@ class PointShadowMap extends Shadows {
 
 		var buffer = new haxe.io.BytesBuffer();
 		buffer.addInt32(staticTexture.width);
-		buffer.addFloat(lightCamera.pos.x);
-		buffer.addFloat(lightCamera.pos.y);
-		buffer.addFloat(lightCamera.pos.z);
 
 		for(i in 0 ... 6){
 			var bytes = haxe.zip.Compress.run(staticTexture.capturePixels(i).bytes,9);
@@ -91,11 +90,6 @@ class PointShadowMap extends Shadows {
 		if( size != this.size )
 			return false;
 
-		lightCamera.pos.x = buffer.readFloat();
-		lightCamera.pos.y = buffer.readFloat();
-		lightCamera.pos.z = buffer.readFloat();
-		lightCamera.update();
-
 		if( staticTexture != null ) staticTexture.dispose();
 			staticTexture = new h3d.mat.Texture(size, size, [Target, Cube], format);
 

+ 23 - 10
h3d/scene/pbr/PointLight.hx

@@ -30,10 +30,22 @@ class PointLight extends Light {
 		primitive.render(ctx.engine);
 	}
 
-	override function emit(ctx:RenderContext) {
-		if( ctx.pbrLightPass == null )
-			throw "Rendering a pbr light require a PBR compatible scene renderer";
-		ctx.emitPass(ctx.pbrLightPass, this);
+	override function sync(ctx) {
+
+		if(ctx.computingStatic && (shadows.mode == Static ||  shadows.mode == Mixed)){
+			culled = false; // Always render for baking
+		}
+		else{
+			var sphereVolume = new h3d.col.Sphere();
+			sphereVolume.x = pbr.lightPos.x;
+			sphereVolume.y = pbr.lightPos.y;
+			sphereVolume.z = pbr.lightPos.z;
+			sphereVolume.r = this.range;
+			culled = !ctx.camera.getFrustum().hasSphere(sphereVolume);
+		}
+
+		if(culled) return;
+
 		pbr.lightColor.load(_color);
 		var range = hxd.Math.max(range, 1e-10);
 		var size = hxd.Math.min(size, range);
@@ -42,12 +54,13 @@ class PointLight extends Light {
 		pbr.lightPos.set(absPos.tx, absPos.ty, absPos.tz);
 		pbr.invLightRange4 = 1 / (range * range * range * range);
 		pbr.pointSize = size;
-
-		// Frustum Culling
-		var sphereVolume = new h3d.col.Sphere();
-		sphereVolume.r = this.range;
-		if(ctx.camera.getFrustum().hasSphere( sphereVolume))
-			super.emit(ctx);
 	}
 
+	override function emit(ctx:RenderContext) {
+		if( ctx.pbrLightPass == null )
+			throw "Rendering a pbr light require a PBR compatible scene renderer";
+
+		super.emit(ctx);
+		ctx.emitPass(ctx.pbrLightPass, this);
+	}
 }