Node.js 7.6 KB

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