NodeGL.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.NodeGL = function( type ) {
  5. this.uuid = THREE.Math.generateUUID();
  6. this.allow = {};
  7. this.requestUpdate = false;
  8. this.type = type;
  9. };
  10. THREE.NodeGL.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.NodeGL.prototype.verifyAndBuildCode = function( builder, output, cache ) {
  20. this.verify( builder.setCache(cache) );
  21. return this.buildCode( builder.setCache(cache), output );
  22. };
  23. THREE.NodeGL.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.NodeGL.prototype.verifyNodeDeps = 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.NodeGL.prototype.build = function( builder, output, uuid ) {
  40. var material = builder.material;
  41. var data = material.getNodeData( uuid || this.uuid );
  42. if (builder.isShader('verify')) this.verifyNodeDeps( 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.NodeGL.prototype.getType = function( builder ) {
  53. return this.type;
  54. };