Node.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. build( builder, output = null ) {
  29. const hash = this.getHash( builder );
  30. const sharedNode = builder.getNodeFromHash( hash );
  31. if ( sharedNode !== undefined && this !== sharedNode ) {
  32. return sharedNode.build( builder, output );
  33. }
  34. builder.addNode( this );
  35. builder.addStack( this );
  36. const isGenerateOnce = this.generate.length === 1;
  37. let snippet = null;
  38. if ( isGenerateOnce ) {
  39. const type = this.getNodeType( builder );
  40. const nodeData = builder.getDataFromNode( this );
  41. snippet = nodeData.snippet;
  42. if ( snippet === undefined ) {
  43. snippet = this.generate( builder ) || '';
  44. nodeData.snippet = snippet;
  45. }
  46. snippet = builder.format( snippet, type, output );
  47. } else {
  48. snippet = this.generate( builder, output ) || '';
  49. }
  50. builder.removeStack( this );
  51. return snippet;
  52. }
  53. serialize( json ) {
  54. const nodeKeys = getNodesKeys( this );
  55. if ( nodeKeys.length > 0 ) {
  56. const inputNodes = {};
  57. for ( const property of nodeKeys ) {
  58. inputNodes[ property ] = this[ property ].toJSON( json.meta ).uuid;
  59. }
  60. json.inputNodes = inputNodes;
  61. }
  62. }
  63. deserialize( json ) {
  64. if ( json.inputNodes !== undefined ) {
  65. const nodes = json.meta.nodes;
  66. for ( const property in json.inputNodes ) {
  67. const uuid = json.inputNodes[ property ];
  68. this[ property ] = nodes[ uuid ];
  69. }
  70. }
  71. }
  72. toJSON( meta ) {
  73. const { uuid, type } = this;
  74. const isRoot = ( meta === undefined || typeof meta === 'string' );
  75. if ( isRoot ) {
  76. meta = {
  77. textures: {},
  78. images: {},
  79. nodes: {}
  80. };
  81. }
  82. // serialize
  83. let data = meta.nodes[ uuid ];
  84. if ( data === undefined ) {
  85. data = {
  86. uuid,
  87. type,
  88. meta,
  89. metadata: {
  90. version: 4.5,
  91. type: 'Node',
  92. generator: 'Node.toJSON'
  93. }
  94. };
  95. meta.nodes[ data.uuid ] = data;
  96. this.serialize( data );
  97. delete data.meta;
  98. }
  99. // TODO: Copied from Object3D.toJSON
  100. function extractFromCache( cache ) {
  101. const values = [];
  102. for ( const key in cache ) {
  103. const data = cache[ key ];
  104. delete data.metadata;
  105. values.push( data );
  106. }
  107. return values;
  108. }
  109. if ( isRoot ) {
  110. const textures = extractFromCache( meta.textures );
  111. const images = extractFromCache( meta.images );
  112. const nodes = extractFromCache( meta.nodes );
  113. if ( textures.length > 0 ) data.textures = textures;
  114. if ( images.length > 0 ) data.images = images;
  115. if ( nodes.length > 0 ) data.nodes = nodes;
  116. }
  117. return data;
  118. }
  119. }
  120. Node.prototype.isNode = true;
  121. export default Node;