Node.js 7.3 KB

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