GLNode.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.GLNode = function( type ) {
  5. this.uuid = THREE.Math.generateUUID();
  6. this.allow = {};
  7. this.requestUpdate = false;
  8. this.type = type;
  9. };
  10. THREE.GLNode.prototype.verify = function( builder ) {
  11. builder.isVerify = true;
  12. var material = builder.material;
  13. this.build( builder, 'v4' );
  14. material.clearVertexNode();
  15. material.clearFragmentNode();
  16. builder.setCache(); // reset cache
  17. builder.isVerify = false;
  18. };
  19. THREE.GLNode.prototype.verifyAndBuildCode = function( builder, output, cache ) {
  20. this.verify( builder.setCache( cache ) );
  21. return this.buildCode( builder.setCache( cache ), output );
  22. };
  23. THREE.GLNode.prototype.buildCode = function( builder, output, uuid ) {
  24. var material = builder.material;
  25. var data = { result : this.build( builder, output, uuid ) };
  26. if ( builder.isShader( 'vertex' ) ) data.code = material.clearVertexNode();
  27. else data.code = material.clearFragmentNode();
  28. builder.setCache(); // reset cache
  29. return data;
  30. };
  31. THREE.GLNode.prototype.verifyDepsNode = function( builder, data, output ) {
  32. data.deps = ( data.deps || 0 ) + 1;
  33. var outputLen = builder.getFormatLength( output );
  34. if ( outputLen > data.outputMax || this.getType( builder ) ) {
  35. data.outputMax = outputLen;
  36. data.output = output;
  37. }
  38. };
  39. THREE.GLNode.prototype.build = function( builder, output, uuid ) {
  40. var material = builder.material;
  41. var data = material.getDataNode( uuid || this.uuid );
  42. if ( builder.isShader( 'verify' ) ) this.verifyDepsNode( builder, data, output );
  43. if ( this.allow[ builder.shader ] === false ) {
  44. throw new Error( 'Shader ' + shader + ' is not compatible with this node.' );
  45. }
  46. if ( this.requestUpdate && ! data.requestUpdate ) {
  47. material.requestUpdate.push( this );
  48. data.requestUpdate = true;
  49. }
  50. return this.generate( builder, output, uuid );
  51. };
  52. THREE.GLNode.prototype.getType = function( builder ) {
  53. return this.type;
  54. };