Node.js 7.7 KB

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