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

NodeUtils: getNodesKeys -> getNodeChildren #25581

Merged
merged 16 commits into from
Mar 1, 2023
41 changes: 9 additions & 32 deletions examples/jsm/nodes/core/Node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NodeUpdateType } from './constants.js';
import { getNodesKeys, getCacheKey } from './NodeUtils.js';
import { getNodeChildren, getCacheKey } from './NodeUtils.js';
import { MathUtils } from 'three';

const NodeClasses = new Map();
Expand Down Expand Up @@ -36,42 +36,19 @@ class Node {

* getChildren() {

for ( const property in this ) {
const self = this;

const object = this[ property ];
for ( const { prop, childNode } of getNodeChildren( this ) ) {
LeviPesin marked this conversation as resolved.
Show resolved Hide resolved

if ( Array.isArray( object ) === true ) {
if ( prop.includes( '/' ) ) {

for ( let i = 0; i < object.length; i++ ) {
const prop1 = prop.slice( 0, prop.indexOf( '/' ) );
const prop2 = prop.slice( prop.indexOf( '/' ) + 1 );
yield { childNode, replaceNode( node ) { self[ prop1 ][ prop2 ] = node; } };

const child = object[ i ];

if ( child && child.isNode === true ) {

yield { childNode: child, replaceNode( node ) { object[ i ] = node; } };

}

}

} else if ( object && object.isNode === true ) {

const self = this;
yield { childNode: object, replaceNode( node ) { self[ property ] = node; } };

} else if ( typeof object === 'object' ) {

for ( const property in object ) {

const child = object[ property ];

if ( child && child.isNode === true ) {

yield { childNode: child, replaceNode( node ) { object[ property ] = node; } };

}
} else {

}
yield { childNode, replaceNode( node ) { self[ prop ] = node; } };

}

Expand Down
70 changes: 37 additions & 33 deletions examples/jsm/nodes/core/NodeUtils.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,76 @@
import { Color, Matrix3, Matrix4, Vector2, Vector3, Vector4 } from 'three';

export const getCacheKey = ( object ) => {
export function getCacheKey( object ) {

let cacheKey = '{';

if ( object.isNode === true ) {

cacheKey += `uuid:"${ object.uuid }",`;
cacheKey += `uuid:"${ object.uuid }"`;

}

for ( const property of getNodesKeys( object ) ) {

const node = object[ property ];
for ( const { prop, childNode } of getNodeChildren( object ) ) {

// @TODO: Think about implement NodeArray and NodeObject.

if ( Array.isArray( node ) ) {
let childCacheKey = getCacheKey( childNode );
if ( ! childCacheKey.includes( ',' ) ) childCacheKey = childCacheKey.slice( childCacheKey.indexOf( '"' ), childCacheKey.indexOf( '}' ) );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line just converts childCacheKey to just uuid if it has only it (i.e. something like {uuid:"..."} to "...").

cacheKey += `,${ prop }:${ childCacheKey }`;

for ( const subNode of node ) {
}

cacheKey += `${ property }:${ subNode.getCacheKey() },`;
cacheKey += '}';

}
return cacheKey;

} else {
}

cacheKey += `${ property }:${ node.getCacheKey() },`;
export function* getNodeChildren( node ) {

}
for ( const property in node ) {

}
const object = node[ property ];

cacheKey += '}';
if ( Array.isArray( object ) === true ) {

return cacheKey;
for ( let i = 0; i < object.length; i++ ) {

const child = object[ i ];

};
if ( child && child.isNode === true ) {

export const getNodesKeys = ( object ) => {
yield { prop: property + '/' + i, childNode: child };

const props = [];
}

for ( const name in object ) {
}

const value = object[ name ];
} else if ( object && object.isNode === true ) {

if ( Array.isArray( value ) ) {
yield { prop: property, childNode: object };

if ( value[ 0 ] && value[ 0 ].isNode === true ) {
} else if ( typeof object === 'object' ) {

props.push( name );
for ( const property2 in object ) {

}
const child = object[ property2 ];

} else if ( value && value.isNode === true ) {
if ( child && child.isNode === true ) {

props.push( name );
yield { prop: property + '/' + property2, childNode: child };

}

}

}

}

return props;

};
}

export const getValueType = ( value ) => {
export function getValueType( value ) {

if ( typeof value === 'number' ) {

Expand Down Expand Up @@ -104,9 +108,9 @@ export const getValueType = ( value ) => {

return null;

};
}

export const getValueFromType = ( type, ...params ) => {
export function getValueFromType( type, ...params ) {

const last4 = type ? type.slice( - 4 ) : undefined;

Expand Down Expand Up @@ -146,4 +150,4 @@ export const getValueFromType = ( type, ...params ) => {

return null;

};
}
8 changes: 4 additions & 4 deletions examples/jsm/nodes/materials/NodeMaterial.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Material, ShaderMaterial, NoToneMapping } from 'three';
import { getNodesKeys, getCacheKey } from '../core/NodeUtils.js';
import { getNodeChildren, getCacheKey } from '../core/NodeUtils.js';
import { attribute } from '../core/AttributeNode.js';
import { diffuseColor } from '../core/PropertyNode.js';
import { materialNormal } from '../accessors/ExtendedMaterialNode.js';
Expand Down Expand Up @@ -322,13 +322,13 @@ class NodeMaterial extends ShaderMaterial {
}

const data = Material.prototype.toJSON.call( this, meta );
const nodeKeys = getNodesKeys( this );
const nodeChildren = getNodeChildren( this );

data.inputNodes = {};

for ( const name of nodeKeys ) {
for ( const { prop, childNode } of nodeChildren ) {

data.inputNodes[ name ] = this[ name ].toJSON( meta ).uuid;
data.inputNodes[ prop ] = childNode.toJSON( meta ).uuid;

}

Expand Down