Node.js 3.1 KB

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