Node.js 3.1 KB

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