Node.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { Math as _Math } from '../../../../build/three.module.js';
  5. function Node( type ) {
  6. this.uuid = _Math.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. appendDepsNode: function ( builder, data, output ) {
  56. data.deps = ( data.deps || 0 ) + 1;
  57. var outputLen = builder.getTypeLength( output );
  58. if ( outputLen > ( data.outputMax || 0 ) || this.getType( builder, output ) ) {
  59. data.outputMax = outputLen;
  60. data.output = output;
  61. }
  62. },
  63. setName: function ( name ) {
  64. this.name = name;
  65. return this;
  66. },
  67. getName: function ( /* builder */ ) {
  68. return this.name;
  69. },
  70. getType: function ( builder, output ) {
  71. return output === 'sampler2D' || output === 'samplerCube' ? output : this.type;
  72. },
  73. getJSONNode: function ( meta ) {
  74. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  75. if ( ! isRootObject && meta.nodes[ this.uuid ] !== undefined ) {
  76. return meta.nodes[ this.uuid ];
  77. }
  78. },
  79. copy: function ( source ) {
  80. if ( source.name !== undefined ) this.name = source.name;
  81. if ( source.userData !== undefined ) this.userData = JSON.parse( JSON.stringify( source.userData ) );
  82. return this;
  83. },
  84. createJSONNode: function ( meta ) {
  85. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  86. var data = {};
  87. if ( typeof this.nodeType !== "string" ) throw new Error( "Node does not allow serialization." );
  88. data.uuid = this.uuid;
  89. data.nodeType = this.nodeType;
  90. if ( this.name !== "" ) data.name = this.name;
  91. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  92. if ( ! isRootObject ) {
  93. meta.nodes[ this.uuid ] = data;
  94. }
  95. return data;
  96. },
  97. toJSON: function ( meta ) {
  98. return this.getJSONNode( meta ) || this.createJSONNode( meta );
  99. }
  100. };
  101. export { Node };