Node.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { NodeUpdateType } from './constants.js';
  2. import { getNodesKeys } from './NodeUtils.js';
  3. import { MathUtils } from 'three';
  4. let _nodeId = 0;
  5. class Node {
  6. constructor( nodeType = null ) {
  7. this.nodeType = nodeType;
  8. this.updateType = NodeUpdateType.None;
  9. this.uuid = MathUtils.generateUUID();
  10. Object.defineProperty( this, 'id', { value: _nodeId ++ } );
  11. }
  12. get type() {
  13. return this.constructor.name;
  14. }
  15. getHash( /*builder*/ ) {
  16. return this.uuid;
  17. }
  18. getUpdateType( /*builder*/ ) {
  19. return this.updateType;
  20. }
  21. getNodeType( /*builder*/ ) {
  22. return this.nodeType;
  23. }
  24. getReference( builder ) {
  25. const hash = this.getHash( builder );
  26. const nodeFromHash = builder.getNodeFromHash( hash );
  27. return nodeFromHash || this;
  28. }
  29. update( /*frame*/ ) {
  30. console.warn( 'Abstract function.' );
  31. }
  32. generate( /*builder, output*/ ) {
  33. console.warn( 'Abstract function.' );
  34. }
  35. analyze( builder ) {
  36. const refNode = this.getReference( builder );
  37. if ( this !== refNode ) {
  38. return refNode.analyze( builder );
  39. }
  40. const nodeData = builder.getDataFromNode( this );
  41. nodeData.dependenciesCount = nodeData.dependenciesCount === undefined ? 1 : nodeData.dependenciesCount + 1;
  42. const nodeKeys = getNodesKeys( this );
  43. for ( const property of nodeKeys ) {
  44. this[ property ].analyze( builder );
  45. }
  46. }
  47. build( builder, output = null ) {
  48. const refNode = this.getReference( builder );
  49. if ( this !== refNode ) {
  50. return refNode.build( builder, output );
  51. }
  52. builder.addNode( this );
  53. builder.addStack( this );
  54. const nodeData = builder.getDataFromNode( this );
  55. const isGenerateOnce = this.generate.length === 1;
  56. let snippet = null;
  57. if ( isGenerateOnce ) {
  58. const type = this.getNodeType( builder );
  59. snippet = nodeData.snippet;
  60. if ( snippet === undefined ) {
  61. snippet = this.generate( builder ) || '';
  62. nodeData.snippet = snippet;
  63. }
  64. snippet = builder.format( snippet, type, output );
  65. } else {
  66. snippet = this.generate( builder, output ) || '';
  67. }
  68. builder.removeStack( this );
  69. return snippet;
  70. }
  71. serialize( json ) {
  72. const nodeKeys = getNodesKeys( this );
  73. if ( nodeKeys.length > 0 ) {
  74. const inputNodes = {};
  75. for ( const property of nodeKeys ) {
  76. inputNodes[ property ] = this[ property ].toJSON( json.meta ).uuid;
  77. }
  78. json.inputNodes = inputNodes;
  79. }
  80. }
  81. deserialize( json ) {
  82. if ( json.inputNodes !== undefined ) {
  83. const nodes = json.meta.nodes;
  84. for ( const property in json.inputNodes ) {
  85. const uuid = json.inputNodes[ property ];
  86. this[ property ] = nodes[ uuid ];
  87. }
  88. }
  89. }
  90. toJSON( meta ) {
  91. const { uuid, type } = this;
  92. const isRoot = ( meta === undefined || typeof meta === 'string' );
  93. if ( isRoot ) {
  94. meta = {
  95. textures: {},
  96. images: {},
  97. nodes: {}
  98. };
  99. }
  100. // serialize
  101. let data = meta.nodes[ uuid ];
  102. if ( data === undefined ) {
  103. data = {
  104. uuid,
  105. type,
  106. meta,
  107. metadata: {
  108. version: 4.5,
  109. type: 'Node',
  110. generator: 'Node.toJSON'
  111. }
  112. };
  113. meta.nodes[ data.uuid ] = data;
  114. this.serialize( data );
  115. delete data.meta;
  116. }
  117. // TODO: Copied from Object3D.toJSON
  118. function extractFromCache( cache ) {
  119. const values = [];
  120. for ( const key in cache ) {
  121. const data = cache[ key ];
  122. delete data.metadata;
  123. values.push( data );
  124. }
  125. return values;
  126. }
  127. if ( isRoot ) {
  128. const textures = extractFromCache( meta.textures );
  129. const images = extractFromCache( meta.images );
  130. const nodes = extractFromCache( meta.nodes );
  131. if ( textures.length > 0 ) data.textures = textures;
  132. if ( images.length > 0 ) data.images = images;
  133. if ( nodes.length > 0 ) data.nodes = nodes;
  134. }
  135. return data;
  136. }
  137. }
  138. Node.prototype.isNode = true;
  139. export default Node;