RoughnessToBlinnExponentNode.js 2.3 KB

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