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