Node.js 7.6 KB

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