Skip to content

Commit

Permalink
Fix an issue with light baking if the clustered lighting has shadows …
Browse files Browse the repository at this point in the history
…disabled (#6843)

Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
  • Loading branch information
mvaligursky and Martin Valigursky committed Jul 23, 2024
1 parent b69b776 commit 25d05be
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/framework/lightmapper/bake-light-ambient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const _tempPoint = new Vec3();

// bake light representing an ambient light (cubemap or constant)
class BakeLightAmbient extends BakeLight {
constructor(scene) {
constructor(lightmapper) {

const scene = lightmapper.scene;
const lightEntity = new Entity('AmbientLight');
lightEntity.addComponent('light', {
type: 'directional',
Expand All @@ -29,7 +30,7 @@ class BakeLightAmbient extends BakeLight {
bakeDir: false
});

super(scene, lightEntity.light.light);
super(scene, lightEntity.light.light, lightmapper.lightingParams);
}

get numVirtualLights() {
Expand Down
4 changes: 4 additions & 0 deletions src/framework/lightmapper/bake-light-simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const _tempPoint = new Vec2();

// a bake light representing a directional, omni or spot type of light
class BakeLightSimple extends BakeLight {
constructor(lightmapper, light) {
super(lightmapper.scene, light, lightmapper.lightingParams);
}

get numVirtualLights() {
// only directional lights support multiple samples
if (this.light.type === LIGHTTYPE_DIRECTIONAL) {
Expand Down
9 changes: 8 additions & 1 deletion src/framework/lightmapper/bake-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const tempSphere = new BoundingSphere();

// helper class to store all lights including their original state
class BakeLight {
constructor(scene, light) {
constructor(scene, light, lightingParams) {

this.scene = scene;

Expand All @@ -19,6 +19,11 @@ class BakeLight {
// don't use cascades
light.numCascades = 1;

if (this.scene.clusteredLightingEnabled) {
// if clustered shadows are disabled, disable them on the light
light.castShadows = light.castShadows && lightingParams.shadowsEnabled;
}

// bounds for non-directional light
if (light.type !== LIGHTTYPE_DIRECTIONAL) {

Expand All @@ -40,6 +45,7 @@ class BakeLight {
this.intensity = this.light.intensity;
this.rotation = this.light._node.getLocalRotation().clone();
this.numCascades = this.light.numCascades;
this.castShadows = this.light.castShadows;
}

restore() {
Expand All @@ -50,6 +56,7 @@ class BakeLight {
light.intensity = this.intensity;
light._node.setLocalRotation(this.rotation);
light.numCascades = this.numCascades;
light.castShadows = this.castShadows;
}

startBake() {
Expand Down
8 changes: 4 additions & 4 deletions src/framework/lightmapper/lightmapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,11 @@ class Lightmapper {
}
}

prepareLightsToBake(layerComposition, allLights, bakeLights) {
prepareLightsToBake(allLights, bakeLights) {

// ambient light
if (this.scene.ambientBake) {
const ambientLight = new BakeLightAmbient(this.scene);
const ambientLight = new BakeLightAmbient(this);
bakeLights.push(ambientLight);
}

Expand All @@ -659,7 +659,7 @@ class Lightmapper {
const light = sceneLights[i];

// store all lights and their original settings we need to temporarily modify
const bakeLight = new BakeLightSimple(this.scene, light);
const bakeLight = new BakeLightSimple(this, light);
allLights.push(bakeLight);

// bake light
Expand Down Expand Up @@ -949,7 +949,7 @@ class Lightmapper {
// Collect bakeable lights, and also keep allLights along with their properties we change to restore them later
this.renderer.collectLights(comp);
const allLights = [], bakeLights = [];
this.prepareLightsToBake(comp, allLights, bakeLights);
this.prepareLightsToBake(allLights, bakeLights);

// update transforms
this.updateTransforms(allNodes);
Expand Down
7 changes: 5 additions & 2 deletions src/scene/renderer/forward-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,11 @@ class ForwardRenderer extends Renderer {
this.lightShadowIntensity[cnt].setValue(directional.shadowIntensity);

const projectionCompensation = (50.0 / lightRenderData.projectionCompensation);
const pixelsPerMeter = directional.penumbraSize / lightRenderData.shadowCamera.renderTarget.width;
this.lightShadowSearchAreaId[cnt].setValue(pixelsPerMeter * projectionCompensation);
const shadowRT = lightRenderData.shadowCamera.renderTarget;
if (shadowRT) {
const pixelsPerMeter = directional.penumbraSize / shadowRT.width;
this.lightShadowSearchAreaId[cnt].setValue(pixelsPerMeter * projectionCompensation);
}

const cameraParams = directional._shadowCameraParams;
cameraParams.length = 4;
Expand Down

0 comments on commit 25d05be

Please sign in to comment.