Node.js 7.5 KB

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