Node.js 8.5 KB

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