Node.js 7.8 KB

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