Node.js 7.0 KB

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