Skip to content

Commit

Permalink
Node: Add .customCacheKey() (mrdoob#30062)
Browse files Browse the repository at this point in the history
* add custom-cachekey

* docs

* doc
  • Loading branch information
sunag authored Dec 8, 2024
1 parent 3a581bf commit 0096caf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
16 changes: 14 additions & 2 deletions src/nodes/core/Node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NodeUpdateType } from './constants.js';
import { getNodeChildren, getCacheKey } from './NodeUtils.js';
import { getNodeChildren, getCacheKey, hash } from './NodeUtils.js';

import { EventDispatcher } from '../../core/EventDispatcher.js';
import { MathUtils } from '../../math/MathUtils.js';
Expand Down Expand Up @@ -320,7 +320,7 @@ class Node extends EventDispatcher {

if ( force === true || this._cacheKey === null ) {

this._cacheKey = getCacheKey( this, force );
this._cacheKey = hash( getCacheKey( this, force ), this.customCacheKey() );
this._cacheKeyVersion = this.version;

}
Expand All @@ -329,6 +329,18 @@ class Node extends EventDispatcher {

}

/**
* Generate a custom cache key for this node.
*
* @return {Number} The cache key of the node.
* @default 0
*/
customCacheKey() {

return 0;

}

/**
* Returns the references to this node which is by default `this`.
*
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/display/ToneMappingNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ class ToneMappingNode extends TempNode {
}

/**
* Overwrites the default `getCacheKey()` implementation by including the tone
* Overwrites the default `customCacheKey()` implementation by including the tone
* mapping type into the cache key.
*
* @return {Number} The hash.
*/
getCacheKey() {
customCacheKey() {

return hash( super.getCacheKey(), this.toneMapping );
return hash( this.toneMapping );

}

Expand Down
12 changes: 9 additions & 3 deletions src/nodes/lighting/AnalyticLightNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ class AnalyticLightNode extends LightingNode {

}

getCacheKey() {

return hash( super.getCacheKey(), this.light.id, this.light.castShadow ? 1 : 0 );
/**
* Overwrites the default `customCacheKey()` implementation by including the
* light.id and light.castShadow into the cache key.
*
* @return {Number} The hash.
*/
customCacheKey() {

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

}

Expand Down
25 changes: 11 additions & 14 deletions src/nodes/lighting/LightsNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,23 @@ class LightsNode extends Node {

}

getCacheKey( force ) {
/**
* Overwrites the default `customCacheKey()` implementation by including the
* light IDs into the cache key.
*
* @return {Number} The hash.
*/
customCacheKey() {

force = force || this.version !== this._cacheKeyVersion;
const lightIDs = [];

if ( force === true || this._cacheKey === null ) {
for ( let i = 0; i < lights.length; i ++ ) {

const lightIDs = [];

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

lightIDs.push( this._lights[ i ].id );

}

this._cacheKey = hashArray( lightIDs );
this._cacheKeyVersion = this.version;
lightIDs.push( lights[ i ].id );

}

return this._cacheKey;
return hashArray( lightIDs );

}

Expand Down

0 comments on commit 0096caf

Please sign in to comment.