Node.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. if ( index !== undefined ) {
  25. yield { childNode, replaceNode( node ) {
  26. self[ property ][ index ] = node;
  27. } };
  28. } else {
  29. yield { childNode, replaceNode( node ) {
  30. self[ property ] = node;
  31. } };
  32. }
  33. }
  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. properties.initialized = true;
  112. properties.outputNode = this.construct( builder );
  113. for ( const childNode of Object.values( properties ) ) {
  114. if ( childNode && childNode.isNode === true ) {
  115. childNode.build( builder );
  116. }
  117. }
  118. }
  119. } else if ( buildStage === 'analyze' ) {
  120. this.analyze( builder );
  121. } else if ( buildStage === 'generate' ) {
  122. const isGenerateOnce = this.generate.length === 1;
  123. if ( isGenerateOnce ) {
  124. const type = this.getNodeType( builder );
  125. const nodeData = builder.getDataFromNode( this );
  126. result = nodeData.snippet;
  127. if ( result === undefined /*|| builder.context.tempRead === false*/ ) {
  128. result = this.generate( builder ) || '';
  129. nodeData.snippet = result;
  130. }
  131. result = builder.format( result, type, output );
  132. } else {
  133. result = this.generate( builder, output ) || '';
  134. }
  135. }
  136. builder.removeChain( this );
  137. return result;
  138. }
  139. getSerializeChildren() {
  140. return getNodeChildren( this );
  141. }
  142. serialize( json ) {
  143. const nodeChildren = this.getSerializeChildren();
  144. const inputNodes = {};
  145. for ( const { property, index, childNode } of nodeChildren ) {
  146. if ( index !== undefined ) {
  147. if ( inputNodes[ property ] === undefined ) {
  148. inputNodes[ property ] = Number.isInteger( index ) ? [] : {};
  149. }
  150. inputNodes[ property ][ index ] = childNode.toJSON( json.meta ).uuid;
  151. } else {
  152. inputNodes[ property ] = childNode.toJSON( json.meta ).uuid;
  153. }
  154. }
  155. if ( Object.keys( inputNodes ).length > 0 ) {
  156. json.inputNodes = inputNodes;
  157. }
  158. }
  159. deserialize( json ) {
  160. if ( json.inputNodes !== undefined ) {
  161. const nodes = json.meta.nodes;
  162. for ( const property in json.inputNodes ) {
  163. if ( Array.isArray( json.inputNodes[ property ] ) ) {
  164. const inputArray = [];
  165. for ( const uuid of json.inputNodes[ property ] ) {
  166. inputArray.push( nodes[ uuid ] );
  167. }
  168. this[ property ] = inputArray;
  169. } else if ( typeof json.inputNodes[ property ] === 'object' ) {
  170. const inputObject = {};
  171. for ( const subProperty in json.inputNodes[ property ] ) {
  172. const uuid = json.inputNodes[ property ][ subProperty ];
  173. inputObject[ subProperty ] = nodes[ uuid ];
  174. }
  175. this[ property ] = inputObject;
  176. } else {
  177. const uuid = json.inputNodes[ property ];
  178. this[ property ] = nodes[ uuid ];
  179. }
  180. }
  181. }
  182. }
  183. toJSON( meta ) {
  184. const { uuid, type } = this;
  185. const isRoot = ( meta === undefined || typeof meta === 'string' );
  186. if ( isRoot ) {
  187. meta = {
  188. textures: {},
  189. images: {},
  190. nodes: {}
  191. };
  192. }
  193. // serialize
  194. let data = meta.nodes[ uuid ];
  195. if ( data === undefined ) {
  196. data = {
  197. uuid,
  198. type,
  199. meta,
  200. metadata: {
  201. version: 4.5,
  202. type: 'Node',
  203. generator: 'Node.toJSON'
  204. }
  205. };
  206. if ( isRoot !== true ) meta.nodes[ data.uuid ] = data;
  207. this.serialize( data );
  208. delete data.meta;
  209. }
  210. // TODO: Copied from Object3D.toJSON
  211. function extractFromCache( cache ) {
  212. const values = [];
  213. for ( const key in cache ) {
  214. const data = cache[ key ];
  215. delete data.metadata;
  216. values.push( data );
  217. }
  218. return values;
  219. }
  220. if ( isRoot ) {
  221. const textures = extractFromCache( meta.textures );
  222. const images = extractFromCache( meta.images );
  223. const nodes = extractFromCache( meta.nodes );
  224. if ( textures.length > 0 ) data.textures = textures;
  225. if ( images.length > 0 ) data.images = images;
  226. if ( nodes.length > 0 ) data.nodes = nodes;
  227. }
  228. return data;
  229. }
  230. }
  231. export default Node;
  232. export function addNodeClass( nodeClass ) {
  233. if ( typeof nodeClass !== 'function' || ! nodeClass.name ) throw new Error( `Node class ${ nodeClass.name } is not a class` );
  234. if ( NodeClasses.has( nodeClass.name ) ) throw new Error( `Redefinition of node class ${ nodeClass.name }` );
  235. NodeClasses.set( nodeClass.name, nodeClass );
  236. }
  237. export function createNodeFromType( type ) {
  238. const Class = NodeClasses.get( type );
  239. if ( Class !== undefined ) {
  240. return new Class();
  241. }
  242. }