Node.js 8.9 KB

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