Node.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import { NodeUpdateType } from './constants.js';
  2. import { getNodesKeys, getCacheKey } from './NodeUtils.js';
  3. import { MathUtils } from 'three';
  4. let _nodeId = 0;
  5. class Node {
  6. constructor( nodeType = null ) {
  7. this.isNode = true;
  8. this.nodeType = nodeType;
  9. this.updateType = NodeUpdateType.None;
  10. this.uuid = MathUtils.generateUUID();
  11. Object.defineProperty( this, 'id', { value: _nodeId ++ } );
  12. }
  13. get type() {
  14. return this.constructor.name;
  15. }
  16. getChildren() {
  17. const children = [];
  18. for ( const property in this ) {
  19. const object = this[ property ];
  20. if ( Array.isArray( object ) === true ) {
  21. for ( const child of object ) {
  22. if ( child?.isNode === true ) {
  23. children.push( child );
  24. }
  25. }
  26. } else if ( object?.isNode === true ) {
  27. children.push( object );
  28. }
  29. }
  30. return children;
  31. }
  32. getCacheKey() {
  33. return getCacheKey( this );
  34. }
  35. getHash( /*builder*/ ) {
  36. return this.uuid;
  37. }
  38. getUpdateType( /*builder*/ ) {
  39. return this.updateType;
  40. }
  41. getNodeType( /*builder*/ ) {
  42. return this.nodeType;
  43. }
  44. getConstructHash( /*builder*/ ) {
  45. return this.uuid;
  46. }
  47. getReference( builder ) {
  48. const hash = this.getHash( builder );
  49. const nodeFromHash = builder.getNodeFromHash( hash );
  50. return nodeFromHash || this;
  51. }
  52. construct( builder ) {
  53. const nodeProperties = builder.getNodeProperties( this );
  54. for ( const childNode of this.getChildren() ) {
  55. nodeProperties[ '_node' + childNode.id ] = childNode;
  56. }
  57. // return a outputNode if exists
  58. return null;
  59. }
  60. analyze( builder ) {
  61. const nodeData = builder.getDataFromNode( this );
  62. nodeData.dependenciesCount = nodeData.dependenciesCount === undefined ? 1 : nodeData.dependenciesCount + 1;
  63. if ( nodeData.dependenciesCount === 1 ) {
  64. // node flow children
  65. const nodeProperties = builder.getNodeProperties( this );
  66. for ( const childNode of Object.values( nodeProperties ) ) {
  67. if ( childNode?.isNode === true ) {
  68. childNode.build( builder );
  69. }
  70. }
  71. }
  72. }
  73. generate( builder, output ) {
  74. const { outputNode } = builder.getNodeProperties( this );
  75. if ( outputNode?.isNode === true ) {
  76. return outputNode.build( builder, output );
  77. }
  78. }
  79. update( /*frame*/ ) {
  80. console.warn( 'Abstract function.' );
  81. }
  82. build( builder, output = null ) {
  83. const refNode = this.getReference( builder );
  84. if ( this !== refNode ) {
  85. return refNode.build( builder, output );
  86. }
  87. builder.addNode( this );
  88. builder.addStack( this );
  89. /* expected return:
  90. - "construct" -> Node
  91. - "analyze" -> null
  92. - "generate" -> String
  93. */
  94. let result = null;
  95. const buildStage = builder.getBuildStage();
  96. if ( buildStage === 'construct' ) {
  97. const properties = builder.getNodeProperties( this );
  98. if ( properties.initialized !== true || builder.context.tempRead === false ) {
  99. properties.initialized = true;
  100. properties.outputNode = this.construct( builder );
  101. for ( const childNode of Object.values( properties ) ) {
  102. if ( childNode?.isNode === true ) {
  103. childNode.build( builder );
  104. }
  105. }
  106. }
  107. } else if ( buildStage === 'analyze' ) {
  108. this.analyze( builder );
  109. } else if ( buildStage === 'generate' ) {
  110. const isGenerateOnce = this.generate.length === 1;
  111. if ( isGenerateOnce ) {
  112. const type = this.getNodeType( builder );
  113. const nodeData = builder.getDataFromNode( this );
  114. result = nodeData.snippet;
  115. if ( result === undefined /*|| builder.context.tempRead === false*/ ) {
  116. result = this.generate( builder ) || '';
  117. nodeData.snippet = result;
  118. }
  119. result = builder.format( result, type, output );
  120. } else {
  121. result = this.generate( builder, output ) || '';
  122. }
  123. }
  124. builder.removeStack( this );
  125. return result;
  126. }
  127. serialize( json ) {
  128. const nodeKeys = getNodesKeys( this );
  129. if ( nodeKeys.length > 0 ) {
  130. const inputNodes = {};
  131. for ( const property of nodeKeys ) {
  132. inputNodes[ property ] = this[ property ].toJSON( json.meta ).uuid;
  133. }
  134. json.inputNodes = inputNodes;
  135. }
  136. }
  137. deserialize( json ) {
  138. if ( json.inputNodes !== undefined ) {
  139. const nodes = json.meta.nodes;
  140. for ( const property in json.inputNodes ) {
  141. const uuid = json.inputNodes[ property ];
  142. this[ property ] = nodes[ uuid ];
  143. }
  144. }
  145. }
  146. toJSON( meta ) {
  147. const { uuid, type } = this;
  148. const isRoot = ( meta === undefined || typeof meta === 'string' );
  149. if ( isRoot ) {
  150. meta = {
  151. textures: {},
  152. images: {},
  153. nodes: {}
  154. };
  155. }
  156. // serialize
  157. let data = meta.nodes[ uuid ];
  158. if ( data === undefined ) {
  159. data = {
  160. uuid,
  161. type,
  162. meta,
  163. metadata: {
  164. version: 4.5,
  165. type: 'Node',
  166. generator: 'Node.toJSON'
  167. }
  168. };
  169. meta.nodes[ data.uuid ] = data;
  170. this.serialize( data );
  171. delete data.meta;
  172. }
  173. // TODO: Copied from Object3D.toJSON
  174. function extractFromCache( cache ) {
  175. const values = [];
  176. for ( const key in cache ) {
  177. const data = cache[ key ];
  178. delete data.metadata;
  179. values.push( data );
  180. }
  181. return values;
  182. }
  183. if ( isRoot ) {
  184. const textures = extractFromCache( meta.textures );
  185. const images = extractFromCache( meta.images );
  186. const nodes = extractFromCache( meta.nodes );
  187. if ( textures.length > 0 ) data.textures = textures;
  188. if ( images.length > 0 ) data.images = images;
  189. if ( nodes.length > 0 ) data.nodes = nodes;
  190. }
  191. return data;
  192. }
  193. }
  194. export default Node;