Node.js 5.3 KB

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