Node.js 3.8 KB

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