Node.js 7.0 KB

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