Node.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. import { EventDispatcher } from 'three';
  2. import { NodeUpdateType } from './constants.js';
  3. import { getNodeChildren, getCacheKey } from './NodeUtils.js';
  4. import { MathUtils } from 'three';
  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. construct( builder ) { // @deprecated, r157
  119. console.warn( 'THREE.Node: construct() is deprecated. Use setup() instead.' );
  120. return this.setup( builder );
  121. }
  122. increaseUsage( builder ) {
  123. const nodeData = builder.getDataFromNode( this );
  124. nodeData.usageCount = nodeData.usageCount === undefined ? 1 : nodeData.usageCount + 1;
  125. return nodeData.usageCount;
  126. }
  127. analyze( builder ) {
  128. const usageCount = this.increaseUsage( builder );
  129. if ( usageCount === 1 ) {
  130. // node flow children
  131. const nodeProperties = builder.getNodeProperties( this );
  132. for ( const childNode of Object.values( nodeProperties ) ) {
  133. if ( childNode && childNode.isNode === true ) {
  134. childNode.build( builder );
  135. }
  136. }
  137. }
  138. }
  139. generate( builder, output ) {
  140. const { outputNode } = builder.getNodeProperties( this );
  141. if ( outputNode && outputNode.isNode === true ) {
  142. return outputNode.build( builder, output );
  143. }
  144. }
  145. updateBefore( /*frame*/ ) {
  146. console.warn( 'Abstract function.' );
  147. }
  148. updateAfter( /*frame*/ ) {
  149. console.warn( 'Abstract function.' );
  150. }
  151. update( /*frame*/ ) {
  152. console.warn( 'Abstract function.' );
  153. }
  154. build( builder, output = null ) {
  155. const refNode = this.getShared( builder );
  156. if ( this !== refNode ) {
  157. return refNode.build( builder, output );
  158. }
  159. builder.addNode( this );
  160. builder.addChain( this );
  161. /* Build stages expected results:
  162. - "setup" -> Node
  163. - "analyze" -> null
  164. - "generate" -> String
  165. */
  166. let result = null;
  167. const buildStage = builder.getBuildStage();
  168. if ( buildStage === 'setup' ) {
  169. this.updateReference( builder );
  170. const properties = builder.getNodeProperties( this );
  171. if ( properties.initialized !== true ) {
  172. const stackNodesBeforeSetup = builder.stack.nodes.length;
  173. properties.initialized = true;
  174. properties.outputNode = this.setup( builder );
  175. if ( properties.outputNode !== null && builder.stack.nodes.length !== stackNodesBeforeSetup ) {
  176. properties.outputNode = builder.stack;
  177. }
  178. for ( const childNode of Object.values( properties ) ) {
  179. if ( childNode && childNode.isNode === true ) {
  180. childNode.build( builder );
  181. }
  182. }
  183. }
  184. } else if ( buildStage === 'analyze' ) {
  185. this.analyze( builder );
  186. } else if ( buildStage === 'generate' ) {
  187. const isGenerateOnce = this.generate.length === 1;
  188. if ( isGenerateOnce ) {
  189. const type = this.getNodeType( builder );
  190. const nodeData = builder.getDataFromNode( this );
  191. result = nodeData.snippet;
  192. if ( result === undefined ) {
  193. result = this.generate( builder ) || '';
  194. nodeData.snippet = result;
  195. }
  196. result = builder.format( result, type, output );
  197. } else {
  198. result = this.generate( builder, output ) || '';
  199. }
  200. }
  201. builder.removeChain( this );
  202. return result;
  203. }
  204. getSerializeChildren() {
  205. return getNodeChildren( this );
  206. }
  207. serialize( json ) {
  208. const nodeChildren = this.getSerializeChildren();
  209. const inputNodes = {};
  210. for ( const { property, index, childNode } of nodeChildren ) {
  211. if ( index !== undefined ) {
  212. if ( inputNodes[ property ] === undefined ) {
  213. inputNodes[ property ] = Number.isInteger( index ) ? [] : {};
  214. }
  215. inputNodes[ property ][ index ] = childNode.toJSON( json.meta ).uuid;
  216. } else {
  217. inputNodes[ property ] = childNode.toJSON( json.meta ).uuid;
  218. }
  219. }
  220. if ( Object.keys( inputNodes ).length > 0 ) {
  221. json.inputNodes = inputNodes;
  222. }
  223. }
  224. deserialize( json ) {
  225. if ( json.inputNodes !== undefined ) {
  226. const nodes = json.meta.nodes;
  227. for ( const property in json.inputNodes ) {
  228. if ( Array.isArray( json.inputNodes[ property ] ) ) {
  229. const inputArray = [];
  230. for ( const uuid of json.inputNodes[ property ] ) {
  231. inputArray.push( nodes[ uuid ] );
  232. }
  233. this[ property ] = inputArray;
  234. } else if ( typeof json.inputNodes[ property ] === 'object' ) {
  235. const inputObject = {};
  236. for ( const subProperty in json.inputNodes[ property ] ) {
  237. const uuid = json.inputNodes[ property ][ subProperty ];
  238. inputObject[ subProperty ] = nodes[ uuid ];
  239. }
  240. this[ property ] = inputObject;
  241. } else {
  242. const uuid = json.inputNodes[ property ];
  243. this[ property ] = nodes[ uuid ];
  244. }
  245. }
  246. }
  247. }
  248. toJSON( meta ) {
  249. const { uuid, type } = this;
  250. const isRoot = ( meta === undefined || typeof meta === 'string' );
  251. if ( isRoot ) {
  252. meta = {
  253. textures: {},
  254. images: {},
  255. nodes: {}
  256. };
  257. }
  258. // serialize
  259. let data = meta.nodes[ uuid ];
  260. if ( data === undefined ) {
  261. data = {
  262. uuid,
  263. type,
  264. meta,
  265. metadata: {
  266. version: 4.6,
  267. type: 'Node',
  268. generator: 'Node.toJSON'
  269. }
  270. };
  271. if ( isRoot !== true ) meta.nodes[ data.uuid ] = data;
  272. this.serialize( data );
  273. delete data.meta;
  274. }
  275. // TODO: Copied from Object3D.toJSON
  276. function extractFromCache( cache ) {
  277. const values = [];
  278. for ( const key in cache ) {
  279. const data = cache[ key ];
  280. delete data.metadata;
  281. values.push( data );
  282. }
  283. return values;
  284. }
  285. if ( isRoot ) {
  286. const textures = extractFromCache( meta.textures );
  287. const images = extractFromCache( meta.images );
  288. const nodes = extractFromCache( meta.nodes );
  289. if ( textures.length > 0 ) data.textures = textures;
  290. if ( images.length > 0 ) data.images = images;
  291. if ( nodes.length > 0 ) data.nodes = nodes;
  292. }
  293. return data;
  294. }
  295. }
  296. export default Node;
  297. export function addNodeClass( type, nodeClass ) {
  298. if ( typeof nodeClass !== 'function' || ! type ) throw new Error( `Node class ${ type } is not a class` );
  299. if ( NodeClasses.has( type ) ) {
  300. console.warn( `Redefinition of node class ${ type }` );
  301. return;
  302. }
  303. NodeClasses.set( type, nodeClass );
  304. nodeClass.type = type;
  305. }
  306. export function createNodeFromType( type ) {
  307. const Class = NodeClasses.get( type );
  308. if ( Class !== undefined ) {
  309. return new Class();
  310. }
  311. }