Node.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import { NodeUpdateType } from './constants.js';
  2. import { getNodesKeys, getCacheKey } from './NodeUtils.js';
  3. import { MathUtils } from 'three';
  4. const NodeClasses = new Map();
  5. let _nodeId = 0;
  6. class Node {
  7. constructor( nodeType = null ) {
  8. this.isNode = true;
  9. this.nodeType = nodeType;
  10. this.updateType = NodeUpdateType.NONE;
  11. this.uuid = MathUtils.generateUUID();
  12. Object.defineProperty( this, 'id', { value: _nodeId ++ } );
  13. }
  14. get type() {
  15. return this.constructor.name;
  16. }
  17. isGlobal( /*builder*/ ) {
  18. return false;
  19. }
  20. * getChildren() {
  21. for ( const property in this ) {
  22. const object = this[ property ];
  23. if ( Array.isArray( object ) === true ) {
  24. for ( let i = 0; i < object.length; i++ ) {
  25. const child = object[ i ];
  26. if ( child && child.isNode === true ) {
  27. yield { childNode: child, replaceNode( node ) { object[ i ] = node; } };
  28. }
  29. }
  30. } else if ( object && object.isNode === true ) {
  31. const self = this;
  32. yield { childNode: object, replaceNode( node ) { self[ property ] = node; } };
  33. } else if ( typeof object === 'object' ) {
  34. for ( const property in object ) {
  35. const child = object[ property ];
  36. if ( child && child.isNode === true ) {
  37. yield { childNode: child, replaceNode( node ) { object[ property ] = node; } };
  38. }
  39. }
  40. }
  41. }
  42. }
  43. traverse( callback, replaceNode = null ) {
  44. callback( this, replaceNode );
  45. for ( const { childNode, replaceNode } of this.getChildren() ) {
  46. childNode.traverse( callback, replaceNode );
  47. }
  48. }
  49. getCacheKey() {
  50. return getCacheKey( this );
  51. }
  52. getHash( /*builder*/ ) {
  53. return this.uuid;
  54. }
  55. getUpdateType( /*builder*/ ) {
  56. return this.updateType;
  57. }
  58. getNodeType( /*builder*/ ) {
  59. return this.nodeType;
  60. }
  61. getReference( builder ) {
  62. const hash = this.getHash( builder );
  63. const nodeFromHash = builder.getNodeFromHash( hash );
  64. return nodeFromHash || this;
  65. }
  66. construct( builder ) {
  67. const nodeProperties = builder.getNodeProperties( this );
  68. for ( const { childNode } of this.getChildren() ) {
  69. nodeProperties[ '_node' + childNode.id ] = childNode;
  70. }
  71. // return a outputNode if exists
  72. return null;
  73. }
  74. analyze( builder ) {
  75. const nodeData = builder.getDataFromNode( this );
  76. nodeData.dependenciesCount = nodeData.dependenciesCount === undefined ? 1 : nodeData.dependenciesCount + 1;
  77. if ( nodeData.dependenciesCount === 1 ) {
  78. // node flow children
  79. const nodeProperties = builder.getNodeProperties( this );
  80. for ( const childNode of Object.values( nodeProperties ) ) {
  81. if ( childNode && childNode.isNode === true ) {
  82. childNode.build( builder );
  83. }
  84. }
  85. }
  86. }
  87. generate( builder, output ) {
  88. const { outputNode } = builder.getNodeProperties( this );
  89. if ( outputNode && outputNode.isNode === true ) {
  90. return outputNode.build( builder, output );
  91. }
  92. }
  93. update( /*frame*/ ) {
  94. console.warn( 'Abstract function.' );
  95. }
  96. build( builder, output = null ) {
  97. const refNode = this.getReference( builder );
  98. if ( this !== refNode ) {
  99. return refNode.build( builder, output );
  100. }
  101. builder.addNode( this );
  102. builder.addStack( this );
  103. /* expected return:
  104. - "construct" -> Node
  105. - "analyze" -> null
  106. - "generate" -> String
  107. */
  108. let result = null;
  109. const buildStage = builder.getBuildStage();
  110. if ( buildStage === 'construct' ) {
  111. const properties = builder.getNodeProperties( this );
  112. if ( properties.initialized !== true || builder.context.tempRead === false ) {
  113. properties.initialized = true;
  114. properties.outputNode = this.construct( builder );
  115. for ( const childNode of Object.values( properties ) ) {
  116. if ( childNode && childNode.isNode === true ) {
  117. childNode.build( builder );
  118. }
  119. }
  120. }
  121. } else if ( buildStage === 'analyze' ) {
  122. this.analyze( builder );
  123. } else if ( buildStage === 'generate' ) {
  124. const isGenerateOnce = this.generate.length === 1;
  125. if ( isGenerateOnce ) {
  126. const type = this.getNodeType( builder );
  127. const nodeData = builder.getDataFromNode( this );
  128. result = nodeData.snippet;
  129. if ( result === undefined /*|| builder.context.tempRead === false*/ ) {
  130. result = this.generate( builder ) || '';
  131. nodeData.snippet = result;
  132. }
  133. result = builder.format( result, type, output );
  134. } else {
  135. result = this.generate( builder, output ) || '';
  136. }
  137. }
  138. builder.removeStack( this );
  139. return result;
  140. }
  141. serialize( json ) {
  142. const nodeKeys = getNodesKeys( this );
  143. if ( nodeKeys.length > 0 ) {
  144. const inputNodes = {};
  145. for ( const property of nodeKeys ) {
  146. inputNodes[ property ] = this[ property ].toJSON( json.meta ).uuid;
  147. }
  148. json.inputNodes = inputNodes;
  149. }
  150. }
  151. deserialize( json ) {
  152. if ( json.inputNodes !== undefined ) {
  153. const nodes = json.meta.nodes;
  154. for ( const property in json.inputNodes ) {
  155. const uuid = json.inputNodes[ property ];
  156. this[ property ] = nodes[ uuid ];
  157. }
  158. }
  159. }
  160. toJSON( meta ) {
  161. const { uuid, type } = this;
  162. const isRoot = ( meta === undefined || typeof meta === 'string' );
  163. if ( isRoot ) {
  164. meta = {
  165. textures: {},
  166. images: {},
  167. nodes: {}
  168. };
  169. }
  170. // serialize
  171. let data = meta.nodes[ uuid ];
  172. if ( data === undefined ) {
  173. data = {
  174. uuid,
  175. type,
  176. meta,
  177. metadata: {
  178. version: 4.5,
  179. type: 'Node',
  180. generator: 'Node.toJSON'
  181. }
  182. };
  183. meta.nodes[ data.uuid ] = data;
  184. this.serialize( data );
  185. delete data.meta;
  186. }
  187. // TODO: Copied from Object3D.toJSON
  188. function extractFromCache( cache ) {
  189. const values = [];
  190. for ( const key in cache ) {
  191. const data = cache[ key ];
  192. delete data.metadata;
  193. values.push( data );
  194. }
  195. return values;
  196. }
  197. if ( isRoot ) {
  198. const textures = extractFromCache( meta.textures );
  199. const images = extractFromCache( meta.images );
  200. const nodes = extractFromCache( meta.nodes );
  201. if ( textures.length > 0 ) data.textures = textures;
  202. if ( images.length > 0 ) data.images = images;
  203. if ( nodes.length > 0 ) data.nodes = nodes;
  204. }
  205. return data;
  206. }
  207. }
  208. export default Node;
  209. export function addNodeClass( nodeClass ) {
  210. if ( typeof nodeClass !== 'function' || ! nodeClass.name ) throw new Error( `Node class ${ nodeClass.name } is not a class` );
  211. if ( NodeClasses.has( nodeClass.name ) ) throw new Error( `Redefinition of node class ${ nodeClass.name }` );
  212. NodeClasses.set( nodeClass.name, nodeClass );
  213. }
  214. export function createNodeFromType( type ) {
  215. const Class = NodeClasses.get( type );
  216. if ( Class !== undefined ) {
  217. return new Class();
  218. }
  219. };