GLNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.parse = function( builder, cache, requires ) {
  11. builder.parsing = true;
  12. var material = builder.material;
  13. this.build( builder.addCache( cache, requires ), 'v4' );
  14. material.clearVertexNode();
  15. material.clearFragmentNode();
  16. builder.removeCache();
  17. builder.parsing = false;
  18. };
  19. THREE.GLNode.prototype.parseAndBuildCode = function( builder, output, cache, requires ) {
  20. this.parse( builder, cache, requires );
  21. return this.buildCode( builder, output, cache, requires );
  22. };
  23. THREE.GLNode.prototype.buildCode = function( builder, output, cache, requires ) {
  24. var material = builder.material;
  25. var data = { result : this.build( builder.addCache( cache, requires ), output ) };
  26. if ( builder.isShader( 'vertex' ) ) data.code = material.clearVertexNode();
  27. else data.code = material.clearFragmentNode();
  28. builder.removeCache();
  29. return data;
  30. };
  31. THREE.GLNode.prototype.build = function( builder, output, uuid ) {
  32. var material = builder.material;
  33. var data = material.getDataNode( uuid || this.uuid );
  34. if ( builder.parsing ) this.appendDepsNode( builder, data, output );
  35. if ( this.allow[ builder.shader ] === false ) {
  36. throw new Error( 'Shader ' + shader + ' is not compatible with this node.' );
  37. }
  38. if ( this.requestUpdate && ! data.requestUpdate ) {
  39. material.requestUpdate.push( this );
  40. data.requestUpdate = true;
  41. }
  42. return this.generate( builder, output, uuid );
  43. };
  44. THREE.GLNode.prototype.appendDepsNode = function( builder, data, output ) {
  45. data.deps = ( data.deps || 0 ) + 1;
  46. var outputLen = builder.getFormatLength( output );
  47. if ( outputLen > ( data.outputMax || 0 ) || this.getType( builder ) ) {
  48. data.outputMax = outputLen;
  49. data.output = output;
  50. }
  51. };
  52. THREE.GLNode.prototype.getType = function( builder ) {
  53. return this.type;
  54. };