2
0

SpecularMIPLevelNode.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 './MaxMIPLevelNode.js';
  7. function SpecularMIPLevelNode( roughness, texture ) {
  8. TempNode.call( this, 'f' );
  9. this.roughness = roughness;
  10. this.texture = texture;
  11. this.maxMIPLevel = undefined;
  12. }
  13. SpecularMIPLevelNode.Nodes = ( function () {
  14. var getSpecularMIPLevel = new FunctionNode( [
  15. // taken from here: http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
  16. "float getSpecularMIPLevel( const in float roughness, const in float maxMIPLevelScalar ) {",
  17. " float sigma = PI * roughness * roughness / ( 1.0 + roughness );",
  18. " float desiredMIPLevel = maxMIPLevelScalar + log2( sigma );",
  19. // clamp to allowable LOD ranges.
  20. " return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );",
  21. "}"
  22. ].join( "\n" ) );
  23. return {
  24. getSpecularMIPLevel: getSpecularMIPLevel
  25. };
  26. } )();
  27. SpecularMIPLevelNode.prototype = Object.create( TempNode.prototype );
  28. SpecularMIPLevelNode.prototype.constructor = SpecularMIPLevelNode;
  29. SpecularMIPLevelNode.prototype.nodeType = "SpecularMIPLevel";
  30. SpecularMIPLevelNode.prototype.setTexture = function ( texture ) {
  31. this.texture = texture;
  32. return this;
  33. };
  34. SpecularMIPLevelNode.prototype.generate = function ( builder, output ) {
  35. if ( builder.isShader( 'fragment' ) ) {
  36. this.maxMIPLevel = this.maxMIPLevel || new MaxMIPLevelNode();
  37. this.maxMIPLevel.texture = this.texture;
  38. var getSpecularMIPLevel = builder.include( SpecularMIPLevelNode.Nodes.getSpecularMIPLevel );
  39. return builder.format( getSpecularMIPLevel + '( ' + this.roughness.build( builder, 'f' ) + ', ' + this.maxMIPLevel.build( builder, 'f' ) + ' )', this.type, output );
  40. } else {
  41. console.warn( "THREE.SpecularMIPLevelNode is not compatible with " + builder.shader + " shader." );
  42. return builder.format( '0.0', this.type, output );
  43. }
  44. };
  45. SpecularMIPLevelNode.prototype.copy = function ( source ) {
  46. TempNode.prototype.copy.call( this, source );
  47. this.texture = source.texture;
  48. this.roughness = source.roughness;
  49. return this;
  50. };
  51. SpecularMIPLevelNode.prototype.toJSON = function ( meta ) {
  52. var data = this.getJSONNode( meta );
  53. if ( ! data ) {
  54. data = this.createJSONNode( meta );
  55. data.texture = this.texture;
  56. data.roughness = this.roughness;
  57. }
  58. return data;
  59. };
  60. export { SpecularMIPLevelNode };