Node.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 ) {
  71. const { outputNode } = builder.getNodeProperties( this );
  72. if ( outputNode?.isNode === true ) {
  73. const type = this.getNodeType( builder );
  74. return outputNode.build( builder, type );
  75. }
  76. }
  77. update( /*frame*/ ) {
  78. console.warn( 'Abstract function.' );
  79. }
  80. build( builder, output = null ) {
  81. const refNode = this.getReference( builder );
  82. if ( this !== refNode ) {
  83. return refNode.build( builder, output );
  84. }
  85. builder.addNode( this );
  86. builder.addStack( this );
  87. /* expected return:
  88. - "construct" -> Node
  89. - "analyze" -> null
  90. - "generat" -> String
  91. */
  92. let result = null;
  93. const buildStage = builder.getBuildStage();
  94. if ( buildStage === 'construct' ) {
  95. const properties = builder.getNodeProperties( this );
  96. const nodeData = builder.getDataFromNode( this );
  97. if ( properties.initied !== true ) {
  98. nodeData.initied = true;
  99. properties.outputNode = this.construct( builder );
  100. for ( const childNode of Object.values( properties ) ) {
  101. if ( childNode?.isNode === true ) {
  102. childNode.build( builder );
  103. }
  104. }
  105. }
  106. } else if ( buildStage === 'analyze' ) {
  107. this.analyze( builder );
  108. } else if ( buildStage === 'generate' ) {
  109. const isGenerateOnce = this.generate.length === 1;
  110. if ( isGenerateOnce ) {
  111. const type = this.getNodeType( builder );
  112. const nodeData = builder.getDataFromNode( this );
  113. result = nodeData.snippet;
  114. if ( result === undefined ) {
  115. result = this.generate( builder ) || '';
  116. nodeData.snippet = result;
  117. }
  118. result = builder.format( result, type, output );
  119. } else {
  120. result = this.generate( builder, output ) || '';
  121. }
  122. }
  123. builder.removeStack( this );
  124. return result;
  125. }
  126. serialize( json ) {
  127. const nodeKeys = getNodesKeys( this );
  128. if ( nodeKeys.length > 0 ) {
  129. const inputNodes = {};
  130. for ( const property of nodeKeys ) {
  131. inputNodes[ property ] = this[ property ].toJSON( json.meta ).uuid;
  132. }
  133. json.inputNodes = inputNodes;
  134. }
  135. }
  136. deserialize( json ) {
  137. if ( json.inputNodes !== undefined ) {
  138. const nodes = json.meta.nodes;
  139. for ( const property in json.inputNodes ) {
  140. const uuid = json.inputNodes[ property ];
  141. this[ property ] = nodes[ uuid ];
  142. }
  143. }
  144. }
  145. toJSON( meta ) {
  146. const { uuid, type } = this;
  147. const isRoot = ( meta === undefined || typeof meta === 'string' );
  148. if ( isRoot ) {
  149. meta = {
  150. textures: {},
  151. images: {},
  152. nodes: {}
  153. };
  154. }
  155. // serialize
  156. let data = meta.nodes[ uuid ];
  157. if ( data === undefined ) {
  158. data = {
  159. uuid,
  160. type,
  161. meta,
  162. metadata: {
  163. version: 4.5,
  164. type: 'Node',
  165. generator: 'Node.toJSON'
  166. }
  167. };
  168. meta.nodes[ data.uuid ] = data;
  169. this.serialize( data );
  170. delete data.meta;
  171. }
  172. // TODO: Copied from Object3D.toJSON
  173. function extractFromCache( cache ) {
  174. const values = [];
  175. for ( const key in cache ) {
  176. const data = cache[ key ];
  177. delete data.metadata;
  178. values.push( data );
  179. }
  180. return values;
  181. }
  182. if ( isRoot ) {
  183. const textures = extractFromCache( meta.textures );
  184. const images = extractFromCache( meta.images );
  185. const nodes = extractFromCache( meta.nodes );
  186. if ( textures.length > 0 ) data.textures = textures;
  187. if ( images.length > 0 ) data.images = images;
  188. if ( nodes.length > 0 ) data.nodes = nodes;
  189. }
  190. return data;
  191. }
  192. }
  193. export default Node;