123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- import { NodeUpdateType } from './constants.js';
- import { getNodesKeys } from './NodeUtils.js';
- import { MathUtils } from 'three';
- let _nodeId = 0;
- class Node {
- constructor( nodeType = null ) {
- this.nodeType = nodeType;
- this.updateType = NodeUpdateType.None;
- this.uuid = MathUtils.generateUUID();
- Object.defineProperty( this, 'id', { value: _nodeId ++ } );
- }
- get type() {
- return this.constructor.name;
- }
- getHash( /*builder*/ ) {
- return this.uuid;
- }
- getUpdateType( /*builder*/ ) {
- return this.updateType;
- }
- getNodeType( /*builder*/ ) {
- return this.nodeType;
- }
- getReference( builder ) {
- const hash = this.getHash( builder );
- const nodeFromHash = builder.getNodeFromHash( hash );
- return nodeFromHash || this;
- }
- update( /*frame*/ ) {
- console.warn( 'Abstract function.' );
- }
- generate( /*builder, output*/ ) {
- console.warn( 'Abstract function.' );
- }
- analyze( builder ) {
- const refNode = this.getReference( builder );
- if ( this !== refNode ) {
- return refNode.analyze( builder );
- }
- const nodeData = builder.getDataFromNode( this );
- nodeData.dependenciesCount = nodeData.dependenciesCount === undefined ? 1 : nodeData.dependenciesCount + 1;
- const nodeKeys = getNodesKeys( this );
- for ( const property of nodeKeys ) {
- this[ property ].analyze( builder );
- }
- }
- build( builder, output = null ) {
- const refNode = this.getReference( builder );
- if ( this !== refNode ) {
- return refNode.build( builder, output );
- }
- builder.addNode( this );
- builder.addStack( this );
- const nodeData = builder.getDataFromNode( this );
- const isGenerateOnce = this.generate.length === 1;
- let snippet = null;
- if ( isGenerateOnce ) {
- const type = this.getNodeType( builder );
- snippet = nodeData.snippet;
- if ( snippet === undefined ) {
- snippet = this.generate( builder ) || '';
- nodeData.snippet = snippet;
- }
- snippet = builder.format( snippet, type, output );
- } else {
- snippet = this.generate( builder, output ) || '';
- }
- builder.removeStack( this );
- return snippet;
- }
- serialize( json ) {
- const nodeKeys = getNodesKeys( this );
- if ( nodeKeys.length > 0 ) {
- const inputNodes = {};
- for ( const property of nodeKeys ) {
- inputNodes[ property ] = this[ property ].toJSON( json.meta ).uuid;
- }
- json.inputNodes = inputNodes;
- }
- }
- deserialize( json ) {
- if ( json.inputNodes !== undefined ) {
- const nodes = json.meta.nodes;
- for ( const property in json.inputNodes ) {
- const uuid = json.inputNodes[ property ];
- this[ property ] = nodes[ uuid ];
- }
- }
- }
- toJSON( meta ) {
- const { uuid, type } = this;
- const isRoot = ( meta === undefined || typeof meta === 'string' );
- if ( isRoot ) {
- meta = {
- textures: {},
- images: {},
- nodes: {}
- };
- }
- // serialize
- let data = meta.nodes[ uuid ];
- if ( data === undefined ) {
- data = {
- uuid,
- type,
- meta,
- metadata: {
- version: 4.5,
- type: 'Node',
- generator: 'Node.toJSON'
- }
- };
- meta.nodes[ data.uuid ] = data;
- this.serialize( data );
- delete data.meta;
- }
- // TODO: Copied from Object3D.toJSON
- function extractFromCache( cache ) {
- const values = [];
- for ( const key in cache ) {
- const data = cache[ key ];
- delete data.metadata;
- values.push( data );
- }
- return values;
- }
- if ( isRoot ) {
- const textures = extractFromCache( meta.textures );
- const images = extractFromCache( meta.images );
- const nodes = extractFromCache( meta.nodes );
- if ( textures.length > 0 ) data.textures = textures;
- if ( images.length > 0 ) data.images = images;
- if ( nodes.length > 0 ) data.nodes = nodes;
- }
- return data;
- }
- }
- Node.prototype.isNode = true;
- export default Node;
|