RoughnessToBlinnExponentNode.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.nodeType = "RoughnessToBlinnExponent";
  21. THREE.RoughnessToBlinnExponentNode.prototype.generate = function ( builder, output ) {
  22. var material = builder.material;
  23. if ( builder.isShader( 'fragment' ) ) {
  24. if ( material.isDefined( 'PHYSICAL' ) ) {
  25. builder.include( THREE.RoughnessToBlinnExponentNode.getSpecularMIPLevel );
  26. if ( builder.isCache( 'clearCoat' ) ) {
  27. return builder.format( 'getSpecularMIPLevel( Material_ClearCoat_BlinnShininessExponent( material ), 8 )', this.type, output );
  28. } else {
  29. return builder.format( 'getSpecularMIPLevel( Material_BlinnShininessExponent( material ), 8 )', this.type, output );
  30. }
  31. } else {
  32. console.warn( "THREE.RoughnessToBlinnExponentNode is only compatible with PhysicalMaterial." );
  33. return builder.format( '0.0', this.type, output );
  34. }
  35. } else {
  36. console.warn( "THREE.RoughnessToBlinnExponentNode is not compatible with " + builder.shader + " shader." );
  37. return builder.format( '0.0', this.type, output );
  38. }
  39. };