Node.js 7.9 KB

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