Node.js 8.1 KB

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