Node.js 3.8 KB

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