SpecularMIPLevelNode.js 2.4 KB

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