Skip to content

Commit

Permalink
Nodes: Log warnings instead of throwing on redefinitions of Node types (
Browse files Browse the repository at this point in the history
mrdoob#27357)

This is the same behaviour as three.js itself has when it's imported multiple times.
  • Loading branch information
hybridherbst authored and AdaRoseCannon committed Jan 15, 2024
1 parent 2a5c3a3 commit d0a933a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
7 changes: 6 additions & 1 deletion examples/jsm/nodes/core/Node.js
Expand Up @@ -458,7 +458,12 @@ export default Node;
export function addNodeClass( type, nodeClass ) {

if ( typeof nodeClass !== 'function' || ! type ) throw new Error( `Node class ${ type } is not a class` );
if ( NodeClasses.has( type ) ) throw new Error( `Redefinition of node class ${ type }` );
if ( NodeClasses.has( type ) ) {

console.warn( `Redefinition of node class ${ type }` );
return;

}

NodeClasses.set( type, nodeClass );
nodeClass.type = type;
Expand Down
8 changes: 7 additions & 1 deletion examples/jsm/nodes/lighting/LightsNode.js
Expand Up @@ -173,7 +173,13 @@ export const lightNodes = nodeProxy( LightsNode );

export function addLightNode( lightClass, lightNodeClass ) {

if ( LightNodes.has( lightClass ) ) throw new Error( `Redefinition of light node ${ lightNodeClass.type }` );
if ( LightNodes.has( lightClass ) ) {

console.warn( `Redefinition of light node ${ lightNodeClass.type }` );
return;

}

if ( typeof lightClass !== 'function' ) throw new Error( `Light ${ lightClass.name } is not a class` );
if ( typeof lightNodeClass !== 'function' || ! lightNodeClass.type ) throw new Error( `Light node ${ lightNodeClass.type } is not a class` );

Expand Down
7 changes: 6 additions & 1 deletion examples/jsm/nodes/materials/NodeMaterial.js
Expand Up @@ -541,7 +541,12 @@ export default NodeMaterial;
export function addNodeMaterial( type, nodeMaterial ) {

if ( typeof nodeMaterial !== 'function' || ! type ) throw new Error( `Node material ${ type } is not a class` );
if ( NodeMaterials.has( type ) ) throw new Error( `Redefinition of node material ${ type }` );
if ( NodeMaterials.has( type ) ) {

console.warn( `Redefinition of node material ${ type }` );
return;

}

NodeMaterials.set( type, nodeMaterial );
nodeMaterial.type = type;
Expand Down
8 changes: 7 additions & 1 deletion examples/jsm/nodes/shadernode/ShaderNode.js
Expand Up @@ -15,7 +15,13 @@ const NodeElements = new Map(); // @TODO: Currently only a few nodes are added,

export function addNodeElement( name, nodeElement ) {

if ( NodeElements.has( name ) ) throw new Error( `Redefinition of node element ${ name }` );
if ( NodeElements.has( name ) ) {

console.warn( `Redefinition of node element ${ name }` );
return;

}

if ( typeof nodeElement !== 'function' ) throw new Error( `Node element ${ name } is not a function` );

NodeElements.set( name, nodeElement );
Expand Down

0 comments on commit d0a933a

Please sign in to comment.