Node.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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() {
  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. getSerializeChildren() {
  129. return getNodeChildren( this );
  130. }
  131. serialize( json ) {
  132. const nodeChildren = this.getSerializeChildren();
  133. const inputNodes = {};
  134. for ( const { property, index, childNode } of nodeChildren ) {
  135. if ( index !== undefined ) {
  136. if ( inputNodes[ property ] === undefined ) {
  137. inputNodes[ property ] = Number.isInteger( index ) ? [] : {};
  138. }
  139. inputNodes[ property ][ index ] = childNode.toJSON( json.meta ).uuid;
  140. } else {
  141. inputNodes[ property ] = childNode.toJSON( json.meta ).uuid;
  142. }
  143. }
  144. if ( Object.keys( inputNodes ).length > 0 ) {
  145. json.inputNodes = inputNodes;
  146. }
  147. }
  148. deserialize( json ) {
  149. if ( json.inputNodes !== undefined ) {
  150. const nodes = json.meta.nodes;
  151. for ( const property in json.inputNodes ) {
  152. if ( Array.isArray( json.inputNodes[ property ] ) ) {
  153. const inputArray = [];
  154. for ( const uuid of json.inputNodes[ property ] ) {
  155. inputArray.push( nodes[ uuid ] );
  156. }
  157. this[ property ] = inputArray;
  158. } else if ( typeof json.inputNodes[ property ] === 'object' ) {
  159. const inputObject = {};
  160. for ( const subProperty in json.inputNodes[ property ] ) {
  161. const uuid = json.inputNodes[ property ][ subProperty ];
  162. inputObject[ subProperty ] = nodes[ uuid ];
  163. }
  164. this[ property ] = inputObject;
  165. } else {
  166. const uuid = json.inputNodes[ property ];
  167. this[ property ] = nodes[ uuid ];
  168. }
  169. }
  170. }
  171. }
  172. toJSON( meta ) {
  173. const { uuid, type } = this;
  174. const isRoot = ( meta === undefined || typeof meta === 'string' );
  175. if ( isRoot ) {
  176. meta = {
  177. textures: {},
  178. images: {},
  179. nodes: {}
  180. };
  181. }
  182. // serialize
  183. let data = meta.nodes[ uuid ];
  184. if ( data === undefined ) {
  185. data = {
  186. uuid,
  187. type,
  188. meta,
  189. metadata: {
  190. version: 4.5,
  191. type: 'Node',
  192. generator: 'Node.toJSON'
  193. }
  194. };
  195. if ( isRoot !== true ) meta.nodes[ data.uuid ] = data;
  196. this.serialize( data );
  197. delete data.meta;
  198. }
  199. // TODO: Copied from Object3D.toJSON
  200. function extractFromCache( cache ) {
  201. const values = [];
  202. for ( const key in cache ) {
  203. const data = cache[ key ];
  204. delete data.metadata;
  205. values.push( data );
  206. }
  207. return values;
  208. }
  209. if ( isRoot ) {
  210. const textures = extractFromCache( meta.textures );
  211. const images = extractFromCache( meta.images );
  212. const nodes = extractFromCache( meta.nodes );
  213. if ( textures.length > 0 ) data.textures = textures;
  214. if ( images.length > 0 ) data.images = images;
  215. if ( nodes.length > 0 ) data.nodes = nodes;
  216. }
  217. return data;
  218. }
  219. }
  220. export default Node;
  221. export function addNodeClass( nodeClass ) {
  222. if ( typeof nodeClass !== 'function' || ! nodeClass.name ) throw new Error( `Node class ${ nodeClass.name } is not a class` );
  223. if ( NodeClasses.has( nodeClass.name ) ) throw new Error( `Redefinition of node class ${ nodeClass.name }` );
  224. NodeClasses.set( nodeClass.name, nodeClass );
  225. }
  226. export function createNodeFromType( type ) {
  227. const Class = NodeClasses.get( type );
  228. if ( Class !== undefined ) {
  229. return new Class();
  230. }
  231. }