Node.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. import { NodeUpdateType } from './constants.js';
  2. import { getNodeChildren, getCacheKey } from './NodeUtils.js';
  3. import { EventDispatcher } from '../../core/EventDispatcher.js';
  4. import { MathUtils } from '../../math/MathUtils.js';
  5. const NodeClasses = new Map();
  6. let _nodeId = 0;
  7. class Node extends EventDispatcher {
  8. constructor( nodeType = null ) {
  9. super();
  10. this.nodeType = nodeType;
  11. this.updateType = NodeUpdateType.NONE;
  12. this.updateBeforeType = NodeUpdateType.NONE;
  13. this.updateAfterType = NodeUpdateType.NONE;
  14. this.uuid = MathUtils.generateUUID();
  15. this.version = 0;
  16. this._cacheKey = null;
  17. this._cacheKeyVersion = 0;
  18. this.global = false;
  19. this.isNode = true;
  20. Object.defineProperty( this, 'id', { value: _nodeId ++ } );
  21. }
  22. set needsUpdate( value ) {
  23. if ( value === true ) {
  24. this.version ++;
  25. }
  26. }
  27. get type() {
  28. return this.constructor.type;
  29. }
  30. onUpdate( callback, updateType ) {
  31. this.updateType = updateType;
  32. this.update = callback.bind( this.getSelf() );
  33. return this;
  34. }
  35. onFrameUpdate( callback ) {
  36. return this.onUpdate( callback, NodeUpdateType.FRAME );
  37. }
  38. onRenderUpdate( callback ) {
  39. return this.onUpdate( callback, NodeUpdateType.RENDER );
  40. }
  41. onObjectUpdate( callback ) {
  42. return this.onUpdate( callback, NodeUpdateType.OBJECT );
  43. }
  44. onReference( callback ) {
  45. this.updateReference = callback.bind( this.getSelf() );
  46. return this;
  47. }
  48. getSelf() {
  49. // Returns non-node object.
  50. return this.self || this;
  51. }
  52. updateReference( /*state*/ ) {
  53. return this;
  54. }
  55. isGlobal( /*builder*/ ) {
  56. return this.global;
  57. }
  58. * getChildren() {
  59. for ( const { childNode } of getNodeChildren( this ) ) {
  60. yield childNode;
  61. }
  62. }
  63. dispose() {
  64. this.dispatchEvent( { type: 'dispose' } );
  65. }
  66. traverse( callback ) {
  67. callback( this );
  68. for ( const childNode of this.getChildren() ) {
  69. childNode.traverse( callback );
  70. }
  71. }
  72. getCacheKey( force = false ) {
  73. force = force || this.version !== this._cacheKeyVersion;
  74. if ( force === true || this._cacheKey === null ) {
  75. this._cacheKey = getCacheKey( this, force );
  76. this._cacheKeyVersion = this.version;
  77. }
  78. return this._cacheKey;
  79. }
  80. getHash( /*builder*/ ) {
  81. return this.uuid;
  82. }
  83. getUpdateType() {
  84. return this.updateType;
  85. }
  86. getUpdateBeforeType() {
  87. return this.updateBeforeType;
  88. }
  89. getUpdateAfterType() {
  90. return this.updateAfterType;
  91. }
  92. getElementType( builder ) {
  93. const type = this.getNodeType( builder );
  94. const elementType = builder.getElementType( type );
  95. return elementType;
  96. }
  97. getNodeType( builder ) {
  98. const nodeProperties = builder.getNodeProperties( this );
  99. if ( nodeProperties.outputNode ) {
  100. return nodeProperties.outputNode.getNodeType( builder );
  101. }
  102. return this.nodeType;
  103. }
  104. getShared( builder ) {
  105. const hash = this.getHash( builder );
  106. const nodeFromHash = builder.getNodeFromHash( hash );
  107. return nodeFromHash || this;
  108. }
  109. setup( builder ) {
  110. const nodeProperties = builder.getNodeProperties( this );
  111. let index = 0;
  112. for ( const childNode of this.getChildren() ) {
  113. nodeProperties[ 'node' + index ++ ] = childNode;
  114. }
  115. // return a outputNode if exists
  116. return null;
  117. }
  118. increaseUsage( builder ) {
  119. const nodeData = builder.getDataFromNode( this );
  120. nodeData.usageCount = nodeData.usageCount === undefined ? 1 : nodeData.usageCount + 1;
  121. return nodeData.usageCount;
  122. }
  123. analyze( builder ) {
  124. const usageCount = this.increaseUsage( builder );
  125. if ( usageCount === 1 ) {
  126. // node flow children
  127. const nodeProperties = builder.getNodeProperties( this );
  128. for ( const childNode of Object.values( nodeProperties ) ) {
  129. if ( childNode && childNode.isNode === true ) {
  130. childNode.build( builder );
  131. }
  132. }
  133. }
  134. }
  135. generate( builder, output ) {
  136. const { outputNode } = builder.getNodeProperties( this );
  137. if ( outputNode && outputNode.isNode === true ) {
  138. return outputNode.build( builder, output );
  139. }
  140. }
  141. updateBefore( /*frame*/ ) {
  142. console.warn( 'Abstract function.' );
  143. }
  144. updateAfter( /*frame*/ ) {
  145. console.warn( 'Abstract function.' );
  146. }
  147. update( /*frame*/ ) {
  148. console.warn( 'Abstract function.' );
  149. }
  150. build( builder, output = null ) {
  151. const refNode = this.getShared( builder );
  152. if ( this !== refNode ) {
  153. return refNode.build( builder, output );
  154. }
  155. builder.addNode( this );
  156. builder.addChain( this );
  157. /* Build stages expected results:
  158. - "setup" -> Node
  159. - "analyze" -> null
  160. - "generate" -> String
  161. */
  162. let result = null;
  163. const buildStage = builder.getBuildStage();
  164. if ( buildStage === 'setup' ) {
  165. this.updateReference( builder );
  166. const properties = builder.getNodeProperties( this );
  167. if ( properties.initialized !== true ) {
  168. const stackNodesBeforeSetup = builder.stack.nodes.length;
  169. properties.initialized = true;
  170. properties.outputNode = this.setup( builder );
  171. if ( properties.outputNode !== null && builder.stack.nodes.length !== stackNodesBeforeSetup ) {
  172. properties.outputNode = builder.stack;
  173. }
  174. for ( const childNode of Object.values( properties ) ) {
  175. if ( childNode && childNode.isNode === true ) {
  176. childNode.build( builder );
  177. }
  178. }
  179. }
  180. } else if ( buildStage === 'analyze' ) {
  181. this.analyze( builder );
  182. } else if ( buildStage === 'generate' ) {
  183. const isGenerateOnce = this.generate.length === 1;
  184. if ( isGenerateOnce ) {
  185. const type = this.getNodeType( builder );
  186. const nodeData = builder.getDataFromNode( this );
  187. result = nodeData.snippet;
  188. if ( result === undefined ) {
  189. result = this.generate( builder ) || '';
  190. nodeData.snippet = result;
  191. }
  192. result = builder.format( result, type, output );
  193. } else {
  194. result = this.generate( builder, output ) || '';
  195. }
  196. }
  197. builder.removeChain( this );
  198. return result;
  199. }
  200. getSerializeChildren() {
  201. return getNodeChildren( this );
  202. }
  203. serialize( json ) {
  204. const nodeChildren = this.getSerializeChildren();
  205. const inputNodes = {};
  206. for ( const { property, index, childNode } of nodeChildren ) {
  207. if ( index !== undefined ) {
  208. if ( inputNodes[ property ] === undefined ) {
  209. inputNodes[ property ] = Number.isInteger( index ) ? [] : {};
  210. }
  211. inputNodes[ property ][ index ] = childNode.toJSON( json.meta ).uuid;
  212. } else {
  213. inputNodes[ property ] = childNode.toJSON( json.meta ).uuid;
  214. }
  215. }
  216. if ( Object.keys( inputNodes ).length > 0 ) {
  217. json.inputNodes = inputNodes;
  218. }
  219. }
  220. deserialize( json ) {
  221. if ( json.inputNodes !== undefined ) {
  222. const nodes = json.meta.nodes;
  223. for ( const property in json.inputNodes ) {
  224. if ( Array.isArray( json.inputNodes[ property ] ) ) {
  225. const inputArray = [];
  226. for ( const uuid of json.inputNodes[ property ] ) {
  227. inputArray.push( nodes[ uuid ] );
  228. }
  229. this[ property ] = inputArray;
  230. } else if ( typeof json.inputNodes[ property ] === 'object' ) {
  231. const inputObject = {};
  232. for ( const subProperty in json.inputNodes[ property ] ) {
  233. const uuid = json.inputNodes[ property ][ subProperty ];
  234. inputObject[ subProperty ] = nodes[ uuid ];
  235. }
  236. this[ property ] = inputObject;
  237. } else {
  238. const uuid = json.inputNodes[ property ];
  239. this[ property ] = nodes[ uuid ];
  240. }
  241. }
  242. }
  243. }
  244. toJSON( meta ) {
  245. const { uuid, type } = this;
  246. const isRoot = ( meta === undefined || typeof meta === 'string' );
  247. if ( isRoot ) {
  248. meta = {
  249. textures: {},
  250. images: {},
  251. nodes: {}
  252. };
  253. }
  254. // serialize
  255. let data = meta.nodes[ uuid ];
  256. if ( data === undefined ) {
  257. data = {
  258. uuid,
  259. type,
  260. meta,
  261. metadata: {
  262. version: 4.6,
  263. type: 'Node',
  264. generator: 'Node.toJSON'
  265. }
  266. };
  267. if ( isRoot !== true ) meta.nodes[ data.uuid ] = data;
  268. this.serialize( data );
  269. delete data.meta;
  270. }
  271. // TODO: Copied from Object3D.toJSON
  272. function extractFromCache( cache ) {
  273. const values = [];
  274. for ( const key in cache ) {
  275. const data = cache[ key ];
  276. delete data.metadata;
  277. values.push( data );
  278. }
  279. return values;
  280. }
  281. if ( isRoot ) {
  282. const textures = extractFromCache( meta.textures );
  283. const images = extractFromCache( meta.images );
  284. const nodes = extractFromCache( meta.nodes );
  285. if ( textures.length > 0 ) data.textures = textures;
  286. if ( images.length > 0 ) data.images = images;
  287. if ( nodes.length > 0 ) data.nodes = nodes;
  288. }
  289. return data;
  290. }
  291. }
  292. export default Node;
  293. export function addNodeClass( type, nodeClass ) {
  294. if ( typeof nodeClass !== 'function' || ! type ) throw new Error( `Node class ${ type } is not a class` );
  295. if ( NodeClasses.has( type ) ) {
  296. console.warn( `Redefinition of node class ${ type }` );
  297. return;
  298. }
  299. NodeClasses.set( type, nodeClass );
  300. nodeClass.type = type;
  301. }
  302. export function createNodeFromType( type ) {
  303. const Class = NodeClasses.get( type );
  304. if ( Class !== undefined ) {
  305. return new Class();
  306. }
  307. }