Node.js 7.7 KB

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