RoughnessToBlinnExponentNode.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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, 'f' );
  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, 'f' ) + ', ' + this.maxMIPLevel.build( builder, 'f' ) + ' )', 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 };