SpecularMIPLevelNode.js 834 B

1234567891011121314151617181920212223242526272829303132
  1. import Node from '../core/Node.js';
  2. import { add, mul, div, log2, clamp, maxMipLevel } from '../shadernode/ShaderNodeBaseElements.js';
  3. class SpecularMIPLevelNode extends Node {
  4. constructor( textureNode, roughnessNode = null ) {
  5. super( 'float' );
  6. this.textureNode = textureNode;
  7. this.roughnessNode = roughnessNode;
  8. }
  9. construct() {
  10. const { textureNode, roughnessNode } = this;
  11. // taken from here: http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html
  12. const maxMIPLevelScalar = maxMipLevel( textureNode );
  13. const sigma = div( mul( Math.PI, mul( roughnessNode, roughnessNode ) ), add( 1.0, roughnessNode ) );
  14. const desiredMIPLevel = add( maxMIPLevelScalar, log2( sigma ) );
  15. return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
  16. }
  17. }
  18. export default SpecularMIPLevelNode;