Node.js 3.7 KB

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