Node.js 8.8 KB

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