getDistanceAttenuation.js 792 B

12345678910111213141516171819202122
  1. import {
  2. ShaderNode, div, max, sub, mul, saturate, pow, pow2, pow4, cond, greaterThan
  3. } from '../../shadernode/ShaderNodeBaseElements.js';
  4. const getDistanceAttenuation = new ShaderNode( ( inputs ) => {
  5. const { lightDistance, cutoffDistance, decayExponent } = inputs;
  6. // based upon Frostbite 3 Moving to Physically-based Rendering
  7. // page 32, equation 26: E[window1]
  8. // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
  9. const distanceFalloff = div( 1.0, max( pow( lightDistance, decayExponent ), 0.01 ) );
  10. return cond(
  11. greaterThan( cutoffDistance, 0 ),
  12. mul( distanceFalloff, pow2( saturate( sub( 1.0, pow4( div( lightDistance, cutoffDistance ) ) ) ) ) ),
  13. distanceFalloff
  14. );
  15. } ); // validated
  16. export default getDistanceAttenuation;