RoughnessToBlinnExponentNode.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.RoughnessToBlinnExponentNode = function() {
  5. THREE.TempNode.call( this, 'fv1' );
  6. };
  7. THREE.RoughnessToBlinnExponentNode.getSpecularMIPLevel = new THREE.FunctionNode( [
  8. // taken from here: http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
  9. "float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {",
  10. //float envMapWidth = pow( 2.0, maxMIPLevelScalar );
  11. //float desiredMIPLevel = log2( envMapWidth * sqrt( 3.0 ) ) - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );
  12. "float maxMIPLevelScalar = float( maxMIPLevel );",
  13. "float desiredMIPLevel = maxMIPLevelScalar - 0.79248 - 0.5 * log2( pow2( blinnShininessExponent ) + 1.0 );",
  14. // clamp to allowable LOD ranges.
  15. "return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );",
  16. "}"
  17. ].join( "\n" ) );
  18. THREE.RoughnessToBlinnExponentNode.prototype = Object.create( THREE.TempNode.prototype );
  19. THREE.RoughnessToBlinnExponentNode.prototype.constructor = THREE.RoughnessToBlinnExponentNode;
  20. THREE.RoughnessToBlinnExponentNode.prototype.generate = function( builder, output ) {
  21. var material = builder.material;
  22. if ( builder.isShader( 'fragment' ) ) {
  23. if ( material.isDefined( 'PHYSICAL' ) ) {
  24. builder.include( THREE.RoughnessToBlinnExponentNode.getSpecularMIPLevel );
  25. if ( builder.isCache( 'clearCoat' ) ) {
  26. return builder.format( 'getSpecularMIPLevel( Material_ClearCoat_BlinnShininessExponent( material ), 8 )', this.type, output );
  27. } else {
  28. return builder.format( 'getSpecularMIPLevel( Material_BlinnShininessExponent( material ), 8 )', this.type, output );
  29. }
  30. } else {
  31. console.warn( "THREE.RoughnessToBlinnExponentNode is only compatible with PhysicalMaterial." );
  32. return builder.format( '0.0', this.type, output );
  33. }
  34. } else {
  35. console.warn( "THREE.RoughnessToBlinnExponentNode is not compatible with " + builder.shader + " shader." );
  36. return builder.format( '0.0', this.type, output );
  37. }
  38. };