Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the way LightComponent changes notify the LayerComposition #5592

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/framework/components/light/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class LightComponent extends Component {
const layer = this.system.app.scene.layers.getLayerById(this.layers[i]);
if (layer) {
layer.addLight(this);
this.light.addLayer(layer);
}
}
}
Expand All @@ -192,6 +193,7 @@ class LightComponent extends Component {
const layer = this.system.app.scene.layers.getLayerById(this.layers[i]);
if (layer) {
layer.removeLight(this);
this.light.removeLayer(layer);
}
}
}
Expand All @@ -210,13 +212,15 @@ class LightComponent extends Component {
const index = this.layers.indexOf(layer.id);
if (index >= 0 && this.enabled && this.entity.enabled) {
layer.addLight(this);
this.light.addLayer(layer);
}
}

onLayerRemoved(layer) {
const index = this.layers.indexOf(layer.id);
if (index >= 0) {
layer.removeLight(this);
this.light.removeLayer(layer);
}
}

Expand Down Expand Up @@ -558,12 +562,14 @@ function _defineProps() {
const layer = this.system.app.scene.layers.getLayerById(oldValue[i]);
if (!layer) continue;
layer.removeLight(this);
this.light.removeLayer(layer);
}
for (let i = 0; i < newValue.length; i++) {
const layer = this.system.app.scene.layers.getLayerById(newValue[i]);
if (!layer) continue;
if (this.enabled && this.entity.enabled) {
layer.addLight(this);
this.light.addLayer(layer);
}
}
});
Expand Down
1 change: 0 additions & 1 deletion src/framework/components/light/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class LightComponentSystem extends ComponentSystem {
const light = new Light(this.app.graphicsDevice);
light.type = lightTypes[data.type];
light._node = component.entity;
light._scene = this.app.scene;
component.data.light = light;

super.initializeComponentData(component, data, properties);
Expand Down
4 changes: 4 additions & 0 deletions src/scene/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,10 @@ class Layer {
* Removes all lights from this layer.
*/
clearLights() {

// notify lights
this._lightsSet.forEach(light => light.removeLayer(this));

this._lightsSet.clear();
this._clusteredLightsSet.clear();
this._lights.length = 0;
Expand Down
38 changes: 30 additions & 8 deletions src/scene/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ const directionalCascades = [

let id = 0;

// Class storing shadow rendering related private information
/**
* Class storing shadow rendering related private information
*
* @ignore
*/
class LightRenderData {
constructor(device, camera, face, light) {

Expand Down Expand Up @@ -114,6 +118,13 @@ class LightRenderData {
* @ignore
*/
class Light {
/**
* The Layers the light is on.
*
* @type {Set<import('./layer.js').Layer>}
*/
layers = new Set();

constructor(graphicsDevice) {
this.device = graphicsDevice;
this.id = id++;
Expand Down Expand Up @@ -205,7 +216,6 @@ class Light {
this.atlasSlotIndex = 0; // allocated slot index, used for more persistent slot allocation
this.atlasSlotUpdated = false; // true if the atlas slot was reassigned this frame (and content needs to be updated)

this._scene = null;
this._node = null;

// private rendering data
Expand Down Expand Up @@ -237,6 +247,14 @@ class Light {
}
}

addLayer(layer) {
this.layers.add(layer);
}

removeLayer(layer) {
this.layers.delete(layer);
}

set numCascades(value) {
if (!this.cascades || this.numCascades !== value) {
this.cascades = directionalCascades[value - 1];
Expand Down Expand Up @@ -869,11 +887,16 @@ class Light {
}

layersDirty() {
if (this._scene?.layers) {
this._scene.layers._dirtyLights = true;
}
this.layers.forEach((layer) => {
layer._dirtyLights = true;
});
}

/**
* Updates a integer key for the light. The key is used to identify all shader related features
* of the light, and so needs to have all properties that modify the generated shader encoded.
* Properties without an effect on the shader (color, shadow intensity) should not be encoded.
*/
updateKey() {
// Key definition:
// Bit
Expand Down Expand Up @@ -911,9 +934,8 @@ class Light {
key |= (chanId[this._cookieChannel.charAt(2)] << 14);
}

if (key !== this.key && this._scene !== null) {
// TODO: most of the changes to the key should not invalidate the composition,
// probably only _type and _castShadows
if (key !== this.key) {
// The layer maintains lights split and sorted by the key, notify it when the key changes
this.layersDirty();
}

Expand Down