Node.js 8.7 KB

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