GLNode.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.GLNode = function( type ) {
  5. this.uuid = THREE.Math.generateUUID();
  6. this.allows = {};
  7. this.requestUpdate = false;
  8. this.type = type;
  9. };
  10. THREE.GLNode.prototype.parse = function( builder, context ) {
  11. context = context || {};
  12. builder.parsing = true;
  13. var material = builder.material;
  14. this.build( builder.addCache( context.cache, context.requires ).addSlot( context.slot ), 'v4' );
  15. material.clearVertexNode();
  16. material.clearFragmentNode();
  17. builder.removeCache().removeSlot();
  18. builder.parsing = false;
  19. };
  20. THREE.GLNode.prototype.parseAndBuildCode = function( builder, output, context ) {
  21. context = context || {};
  22. this.parse( builder, context );
  23. return this.buildCode( builder, output, context );
  24. };
  25. THREE.GLNode.prototype.buildCode = function( builder, output, context ) {
  26. context = context || {};
  27. var material = builder.material;
  28. var data = { result : this.build( builder.addCache( context.cache, context.requires ).addSlot( context.slot ), output ) };
  29. if ( builder.isShader( 'vertex' ) ) data.code = material.clearVertexNode();
  30. else data.code = material.clearFragmentNode();
  31. builder.removeCache().removeSlot();
  32. return data;
  33. };
  34. THREE.GLNode.prototype.build = function( builder, output, uuid ) {
  35. output = output || this.getType( builder, output );
  36. var material = builder.material, data = material.getDataNode( uuid || this.uuid );
  37. if ( builder.parsing ) this.appendDepsNode( builder, data, output );
  38. if ( this.allows[ builder.shader ] === false ) {
  39. throw new Error( 'Shader ' + shader + ' is not compatible with this node.' );
  40. }
  41. if ( this.requestUpdate && material.requestUpdate.indexOf( this ) === - 1 ) {
  42. material.requestUpdate.push( this );
  43. }
  44. return this.generate( builder, output, uuid );
  45. };
  46. THREE.GLNode.prototype.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. THREE.GLNode.prototype.getType = function( builder, output ) {
  55. return output === 'sampler2D' || output === 'samplerCube' ? output : this.type;
  56. };