Node.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. analyze: function ( builder, settings ) {
  15. settings = settings || {};
  16. builder.analyzing = true;
  17. this.build( builder.addFlow( settings.slot, settings.cache, settings.context ), 'v4' );
  18. builder.clearVertexNodeCode();
  19. builder.clearFragmentNodeCode();
  20. builder.removeFlow();
  21. builder.analyzing = false;
  22. },
  23. analyzeAndFlow: function ( builder, output, settings ) {
  24. settings = settings || {};
  25. this.analyze( builder, settings );
  26. return this.flow( builder, output, settings );
  27. },
  28. flow: function ( builder, output, settings ) {
  29. settings = settings || {};
  30. builder.addFlow( settings.slot, settings.cache, settings.context );
  31. var flow = {};
  32. flow.result = this.build( builder, output );
  33. flow.code = builder.clearNodeCode();
  34. flow.extra = builder.context.extra;
  35. builder.removeFlow();
  36. return flow;
  37. },
  38. build: function ( builder, output, uuid ) {
  39. output = output || this.getType( builder, output );
  40. var data = builder.getNodeData( uuid || this );
  41. if ( builder.analyzing ) {
  42. this.appendDepsNode( builder, data, output );
  43. }
  44. if ( builder.nodes.indexOf( this ) === - 1 ) {
  45. builder.nodes.push( this );
  46. }
  47. if ( this.updateFrame !== undefined && builder.updaters.indexOf( this ) === - 1 ) {
  48. builder.updaters.push( this );
  49. }
  50. return this.generate( builder, output, uuid );
  51. },
  52. generate: function ( /* builder, output, uuid, type, ns */ ) {
  53. // This method needs to be implemented in subclasses
  54. },
  55. getHash: function() {
  56. var hash = '{';
  57. for(var prop in this) {
  58. var obj = this[ prop ];
  59. if (obj instanceof Node) {
  60. hash += '"' + prop + '":' + obj.getHash() + ',';
  61. }
  62. }
  63. hash += '"id":"' + this.uuid + '"}';
  64. return hash;
  65. },
  66. appendDepsNode: function ( builder, data, output ) {
  67. data.deps = ( data.deps || 0 ) + 1;
  68. var 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: function ( name ) {
  75. this.name = name;
  76. return this;
  77. },
  78. getName: function ( /* builder */ ) {
  79. return this.name;
  80. },
  81. getType: function ( builder, output ) {
  82. return output === 'sampler2D' || output === 'samplerCube' ? output : this.type;
  83. },
  84. getJSONNode: function ( meta ) {
  85. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  86. if ( ! isRootObject && meta.nodes[ this.uuid ] !== undefined ) {
  87. return meta.nodes[ this.uuid ];
  88. }
  89. },
  90. copy: function ( 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: function ( meta ) {
  96. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  97. var 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: function ( meta ) {
  109. return this.getJSONNode( meta ) || this.createJSONNode( meta );
  110. }
  111. };
  112. export { Node };