Node.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. function Node( type ) {
  5. this.uuid = THREE.Math.generateUUID();
  6. this.name = "";
  7. this.type = type;
  8. this.userData = {};
  9. }
  10. Node.prototype = {
  11. constructor: Node,
  12. isNode: true,
  13. analyze: function ( builder, settings ) {
  14. settings = settings || {};
  15. builder.analyzing = true;
  16. this.build( builder.addFlow( settings.slot, settings.cache, settings.context ), 'v4' );
  17. builder.clearVertexNodeCode();
  18. builder.clearFragmentNodeCode();
  19. builder.removeFlow();
  20. builder.analyzing = false;
  21. },
  22. analyzeAndFlow: function ( builder, output, settings ) {
  23. settings = settings || {};
  24. this.analyze( builder, settings );
  25. return this.flow( builder, output, settings );
  26. },
  27. flow: function ( builder, output, settings ) {
  28. settings = settings || {};
  29. builder.addFlow( settings.slot, settings.cache, settings.context )
  30. var flow = {};
  31. flow.result = this.build( builder, output );
  32. flow.code = builder.clearNodeCode();
  33. flow.extra = builder.context.extra;
  34. builder.removeFlow();
  35. return flow;
  36. },
  37. build: function ( builder, output, uuid ) {
  38. output = output || this.getType( builder, output );
  39. var data = builder.getNodeData( uuid || this );
  40. if ( builder.analyzing ) {
  41. this.appendDepsNode( builder, data, output );
  42. }
  43. if ( builder.nodes.indexOf( this ) === - 1 ) {
  44. builder.nodes.push( this );
  45. }
  46. if ( this.updateFrame !== undefined && builder.updaters.indexOf( this ) === - 1 ) {
  47. builder.updaters.push( this );
  48. }
  49. return this.generate( builder, output, uuid );
  50. },
  51. appendDepsNode: function ( builder, data, output ) {
  52. data.deps = ( data.deps || 0 ) + 1;
  53. var outputLen = builder.getTypeLength( output );
  54. if ( outputLen > ( data.outputMax || 0 ) || this.getType( builder, output ) ) {
  55. data.outputMax = outputLen;
  56. data.output = output;
  57. }
  58. },
  59. setName: function ( name ) {
  60. this.name = name;
  61. return this;
  62. },
  63. getName: function ( builder ) {
  64. return this.name;
  65. },
  66. getType: function ( builder, output ) {
  67. return output === 'sampler2D' || output === 'samplerCube' ? output : this.type;
  68. },
  69. getJSONNode: function ( meta ) {
  70. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  71. if ( ! isRootObject && meta.nodes[ this.uuid ] !== undefined ) {
  72. return meta.nodes[ this.uuid ];
  73. }
  74. },
  75. copy: function ( source ) {
  76. if ( source.name !== undefined ) this.name = source.name;
  77. if ( source.userData !== undefined ) this.userData = JSON.parse( JSON.stringify( source.userData ) );
  78. },
  79. createJSONNode: function ( meta ) {
  80. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  81. var data = {};
  82. if ( typeof this.nodeType !== "string" ) throw new Error( "Node does not allow serialization." );
  83. data.uuid = this.uuid;
  84. data.nodeType = this.nodeType;
  85. if ( this.name !== "" ) data.name = this.name;
  86. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  87. if ( ! isRootObject ) {
  88. meta.nodes[ this.uuid ] = data;
  89. }
  90. return data;
  91. },
  92. toJSON: function ( meta ) {
  93. return this.getJSONNode( meta ) || this.createJSONNode( meta );
  94. }
  95. };
  96. export { Node };