Node.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { MathUtils } from '../../../../build/three.module.js';
  2. class Node {
  3. constructor( type ) {
  4. this.uuid = MathUtils.generateUUID();
  5. this.name = '';
  6. this.type = type;
  7. this.userData = {};
  8. }
  9. analyze( builder, settings = {} ) {
  10. builder.analyzing = true;
  11. this.build( builder.addFlow( settings.slot, settings.cache, settings.context ), 'v4' );
  12. builder.clearVertexNodeCode();
  13. builder.clearFragmentNodeCode();
  14. builder.removeFlow();
  15. builder.analyzing = false;
  16. }
  17. analyzeAndFlow( builder, output, settings = {} ) {
  18. this.analyze( builder, settings );
  19. return this.flow( builder, output, settings );
  20. }
  21. flow( builder, output, settings = {} ) {
  22. builder.addFlow( settings.slot, settings.cache, settings.context );
  23. const flow = {};
  24. flow.result = this.build( builder, output );
  25. flow.code = builder.clearNodeCode();
  26. flow.extra = builder.context.extra;
  27. builder.removeFlow();
  28. return flow;
  29. }
  30. build( builder, output, uuid ) {
  31. output = output || this.getType( builder, output );
  32. const data = builder.getNodeData( uuid || this );
  33. if ( builder.analyzing ) {
  34. this.appendDepsNode( builder, data, output );
  35. }
  36. if ( builder.nodes.indexOf( this ) === - 1 ) {
  37. builder.nodes.push( this );
  38. }
  39. if ( this.updateFrame !== undefined && builder.updaters.indexOf( this ) === - 1 ) {
  40. builder.updaters.push( this );
  41. }
  42. return this.generate( builder, output, uuid );
  43. }
  44. generate( /* builder, output, uuid, type, ns */ ) {
  45. // This method needs to be implemented in subclasses
  46. }
  47. getHash() {
  48. let hash = '{';
  49. let prop, obj;
  50. for ( prop in this ) {
  51. obj = this[ prop ];
  52. if ( obj instanceof Node ) {
  53. hash += '"' + prop + '":' + obj.getHash() + ',';
  54. }
  55. }
  56. if ( this.hashProperties ) {
  57. for ( let i = 0; i < this.hashProperties.length; i ++ ) {
  58. prop = this.hashProperties[ i ];
  59. obj = this[ prop ];
  60. hash += '"' + prop + '":"' + String( obj ) + '",';
  61. }
  62. }
  63. hash += '"id":"' + this.uuid + '"}';
  64. return hash;
  65. }
  66. appendDepsNode( builder, data, output ) {
  67. data.deps = ( data.deps || 0 ) + 1;
  68. const outputLen = builder.getTypeLength( output );
  69. if ( outputLen > ( data.outputMax || 0 ) || this.getType( builder, output ) ) {
  70. data.outputMax = outputLen;
  71. data.output = output;
  72. }
  73. }
  74. setName( name ) {
  75. this.name = name;
  76. return this;
  77. }
  78. getName( /* builder */ ) {
  79. return this.name;
  80. }
  81. getType( builder, output ) {
  82. return output === 'sampler2D' || output === 'samplerCube' ? output : this.type;
  83. }
  84. getJSONNode( meta ) {
  85. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  86. if ( ! isRootObject && meta.nodes[ this.uuid ] !== undefined ) {
  87. return meta.nodes[ this.uuid ];
  88. }
  89. }
  90. copy( source ) {
  91. if ( source.name !== undefined ) this.name = source.name;
  92. if ( source.userData !== undefined ) this.userData = JSON.parse( JSON.stringify( source.userData ) );
  93. return this;
  94. }
  95. createJSONNode( meta ) {
  96. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  97. const data = {};
  98. if ( typeof this.nodeType !== 'string' ) throw new Error( 'Node does not allow serialization.' );
  99. data.uuid = this.uuid;
  100. data.nodeType = this.nodeType;
  101. if ( this.name !== '' ) data.name = this.name;
  102. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  103. if ( ! isRootObject ) {
  104. meta.nodes[ this.uuid ] = data;
  105. }
  106. return data;
  107. }
  108. toJSON( meta ) {
  109. return this.getJSONNode( meta ) || this.createJSONNode( meta );
  110. }
  111. }
  112. Node.prototype.isNode = true;
  113. Node.prototype.hashProperties = undefined;
  114. export { Node };