GLNode.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. function GLNode( type ) {
  5. this.uuid = THREE.Math.generateUUID();
  6. this.name = "";
  7. this.type = type;
  8. this.userData = {};
  9. };
  10. GLNode.prototype = {
  11. constructor: GLNode,
  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.getFormatLength( output );
  49. if ( outputLen > ( data.outputMax || 0 ) || this.getType( builder, output ) ) {
  50. data.outputMax = outputLen;
  51. data.output = output;
  52. }
  53. },
  54. getType: function ( builder, output ) {
  55. return output === 'sampler2D' || output === 'samplerCube' ? output : this.type;
  56. },
  57. getJSONNode: function ( meta ) {
  58. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  59. if ( ! isRootObject && meta.nodes[ this.uuid ] !== undefined ) {
  60. return meta.nodes[ this.uuid ];
  61. }
  62. },
  63. copy: function ( source ) {
  64. if ( source.name !== undefined ) this.name = source.name;
  65. if ( source.userData !== undefined ) this.userData = JSON.parse( JSON.stringify( source.userData ) );
  66. },
  67. createJSONNode: function ( meta ) {
  68. var isRootObject = ( meta === undefined || typeof meta === 'string' );
  69. var data = {};
  70. if ( typeof this.nodeType !== "string" ) throw new Error( "Node does not allow serialization." );
  71. data.uuid = this.uuid;
  72. data.nodeType = this.nodeType;
  73. if ( this.name !== "" ) data.name = this.name;
  74. if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData;
  75. if ( ! isRootObject ) {
  76. meta.nodes[ this.uuid ] = data;
  77. }
  78. return data;
  79. },
  80. toJSON: function ( meta ) {
  81. return this.getJSONNode( meta ) || this.createJSONNode( meta );
  82. },
  83. };
  84. export { GLNode };