RoughnessToBlinnExponentNode.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. import { FunctionNode } from '../core/FunctionNode.js';
  6. import { MaxMIPLevelNode } from '../utils/MaxMIPLevelNode.js';
  7. import { BlinnShininessExponentNode } from './BlinnShininessExponentNode.js';
  8. function RoughnessToBlinnExponentNode( texture ) {
  9. TempNode.call( this, 'fv1' );
  10. this.texture = texture;
  11. this.maxMIPLevel = new MaxMIPLevelNode( texture );
  12. this.blinnShininessExponent = new BlinnShininessExponentNode();
  13. };
  14. RoughnessToBlinnExponentNode.Nodes = (function() {
  15. var getSpecularMIPLevel = new FunctionNode( [
  16. // taken from here: http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
  17. "float getSpecularMIPLevel( const in float blinnShininessExponent, const in float maxMIPLevelScalar ) {",
  18. // float envMapWidth = pow( 2.0, maxMIPLevelScalar );
  19. // float desiredMIPLevel = log2( envMapWidth * sqrt( 3.0 ) ) - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
  20. " float desiredMIPLevel = maxMIPLevelScalar - 0.79248 - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );",
  21. // clamp to allowable LOD ranges.
  22. " return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );",
  23. "}"
  24. ].join( "\n" ) );
  25. return {
  26. getSpecularMIPLevel: getSpecularMIPLevel
  27. };
  28. })();
  29. RoughnessToBlinnExponentNode.prototype = Object.create( TempNode.prototype );
  30. RoughnessToBlinnExponentNode.prototype.constructor = RoughnessToBlinnExponentNode;
  31. RoughnessToBlinnExponentNode.prototype.nodeType = "RoughnessToBlinnExponent";
  32. RoughnessToBlinnExponentNode.prototype.generate = function ( builder, output ) {
  33. if ( builder.isShader( 'fragment' ) ) {
  34. this.maxMIPLevel.texture = this.texture;
  35. var getSpecularMIPLevel = builder.include( RoughnessToBlinnExponentNode.Nodes.getSpecularMIPLevel );
  36. return builder.format( getSpecularMIPLevel + '( ' + this.blinnShininessExponent.build( builder, 'fv1' ) + ', ' + this.maxMIPLevel.build( builder, 'fv1' ) + ' )', this.type, output );
  37. } else {
  38. console.warn( "THREE.RoughnessToBlinnExponentNode is not compatible with " + builder.shader + " shader." );
  39. return builder.format( '0.0', this.type, output );
  40. }
  41. };
  42. RoughnessToBlinnExponentNode.prototype.copy = function ( source ) {
  43. TempNode.prototype.copy.call( this, source );
  44. this.texture = source.texture;
  45. };
  46. RoughnessToBlinnExponentNode.prototype.toJSON = function ( meta ) {
  47. var data = this.getJSONNode( meta );
  48. if ( ! data ) {
  49. data = this.createJSONNode( meta );
  50. data.texture = this.texture;
  51. }
  52. return data;
  53. };
  54. export { RoughnessToBlinnExponentNode };