Skip to content

LightsNode: Fix castShadow regression. #31106

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

Merged
merged 2 commits into from
May 15, 2025
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
20 changes: 7 additions & 13 deletions src/nodes/lighting/AnalyticLightNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { NodeUpdateType } from '../core/constants.js';
import { uniform } from '../core/UniformNode.js';
import { Color } from '../../math/Color.js';
import { renderGroup } from '../core/UniformGroupNode.js';
import { hash } from '../core/NodeUtils.js';
import { shadow } from './ShadowNode.js';
import { nodeObject } from '../tsl/TSLCore.js';
import { lightViewPosition } from '../accessors/Lights.js';
Expand Down Expand Up @@ -99,24 +98,19 @@ class AnalyticLightNode extends LightingNode {

}

/**
* Overwrites the default {@link Node#customCacheKey} implementation by including the
* `light.id` and `light.castShadow` into the cache key.
*
* @return {number} The custom cache key.
*/
customCacheKey() {

return hash( this.light.id, this.light.castShadow ? 1 : 0 );

}

getHash() {

return this.light.uuid;

}

/**
* Returns a node representing a direction vector which points from the current
* position in view space to the light's position in view space.
*
* @param {NodeBuilder} builder - The builder object used for setting up the light.
* @return {Node<vec3>} The light vector node.
*/
getLightVector( builder ) {

return lightViewPosition( this.light ).sub( builder.context.positionView || positionView );
Expand Down
13 changes: 9 additions & 4 deletions src/nodes/lighting/LightsNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const getLightNodeById = ( id, lightNodes ) => {
};

const _lightsNodeRef = /*@__PURE__*/ new WeakMap();
const _hashData = [];

/**
* This node represents the scene's lighting and manages the lighting model's life cycle
Expand Down Expand Up @@ -114,27 +115,31 @@ class LightsNode extends Node {
*/
customCacheKey() {

const hashData = [];
const lights = this._lights;

for ( let i = 0; i < lights.length; i ++ ) {

const light = lights[ i ];

hashData.push( light.id );
_hashData.push( light.id );
_hashData.push( light.castShadow ? 1 : 0 );

if ( light.isSpotLight === true ) {

const hashMap = ( light.map !== null ) ? light.map.id : - 1;
const hashColorNode = ( light.colorNode ) ? light.colorNode.getCacheKey() : - 1;

hashData.push( hashMap, hashColorNode );
_hashData.push( hashMap, hashColorNode );

}

}

return hashArray( hashData );
const cacheKey = hashArray( _hashData );

_hashData.length = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any source that .length=0 is better than other alternatives?

I didn't do an extensive beachmark, but I had done some tests between creating a new array and using .length=0 and creating a new array worked better. It seems that both force the GC, using null values ​​after use was what worked best for small arrays.

Copy link
Collaborator Author

@Mugen87 Mugen87 May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to find the original issue/PR where we discussed this but without success so far. It's many years ago but at that time, setting a zero length was the fastest way to clear an array. And it does no create a new array object like the previous approach which invalidates the previous object.

I'm surprised to hear that .length=0 triggers the GC. The array itself is just empty and the reference to it is not set to null or cleared.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can revisit the array management at any point again. Merging for now so the toggling issue is fixed on dev.


return cacheKey;

}

Expand Down