Node.js 7.0 KB

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