Node.js 8.8 KB

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