BSDFs.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import ShaderNode from '../shadernode/ShaderNode.js';
  2. import {
  3. add, addTo, sub, mul, div, saturate, dot, pow, pow2, exp2, normalize, max, sqrt, negate,
  4. cond, greaterThan, and,
  5. transformedNormalView, positionViewDirection,
  6. diffuseColor, specularColor, roughness,
  7. EPSILON
  8. } from '../shadernode/ShaderNodeElements.js';
  9. export const F_Schlick = new ShaderNode( ( inputs ) => {
  10. const { f0, f90, dotVH } = inputs;
  11. // Original approximation by Christophe Schlick '94
  12. // float fresnel = pow( 1.0 - dotVH, 5.0 );
  13. // Optimized variant (presented by Epic at SIGGRAPH '13)
  14. // https://cdn2.unrealengine.com/Resources/files/2013SiggraphPresentationsNotes-26915738.pdf
  15. const fresnel = exp2( mul( sub( mul( - 5.55473, dotVH ), 6.98316 ), dotVH ) );
  16. return add( mul( f0, sub( 1.0, fresnel ) ), mul( f90, fresnel ) );
  17. } ); // validated
  18. export const BRDF_Lambert = new ShaderNode( ( inputs ) => {
  19. return mul( 1 / Math.PI, inputs.diffuseColor ); // punctual light
  20. } ); // validated
  21. export const getDistanceAttenuation = new ShaderNode( ( inputs ) => {
  22. const { lightDistance, cutoffDistance, decayExponent } = inputs;
  23. return cond(
  24. and( greaterThan( cutoffDistance, 0 ), greaterThan( decayExponent, 0 ) ),
  25. pow( saturate( add( div( negate( lightDistance ), cutoffDistance ), 1.0 ) ), decayExponent ),
  26. 1.0
  27. );
  28. } ); // validated
  29. //
  30. // STANDARD
  31. //
  32. // Moving Frostbite to Physically Based Rendering 3.0 - page 12, listing 2
  33. // https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf
  34. export const V_GGX_SmithCorrelated = new ShaderNode( ( inputs ) => {
  35. const { alpha, dotNL, dotNV } = inputs;
  36. const a2 = pow2( alpha );
  37. const gv = mul( dotNL, sqrt( add( a2, mul( sub( 1.0, a2 ), pow2( dotNV ) ) ) ) );
  38. const gl = mul( dotNV, sqrt( add( a2, mul( sub( 1.0, a2 ), pow2( dotNL ) ) ) ) );
  39. return div( 0.5, max( add( gv, gl ), EPSILON ) );
  40. } ); // validated
  41. // Microfacet Models for Refraction through Rough Surfaces - equation (33)
  42. // http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html
  43. // alpha is "roughness squared" in Disney’s reparameterization
  44. export const D_GGX = new ShaderNode( ( inputs ) => {
  45. const { alpha, dotNH } = inputs;
  46. const a2 = pow2( alpha );
  47. const denom = add( mul( pow2( dotNH ), sub( a2, 1.0 ) ), 1.0 ); // avoid alpha = 0 with dotNH = 1
  48. return mul( 1 / Math.PI, div( a2, pow2( denom ) ) );
  49. } ); // validated
  50. // GGX Distribution, Schlick Fresnel, GGX_SmithCorrelated Visibility
  51. export const BRDF_GGX = new ShaderNode( ( inputs ) => {
  52. const { lightDirection, f0, f90, roughness } = inputs;
  53. const alpha = pow2( roughness ); // UE4's roughness
  54. const halfDir = normalize( add( lightDirection, positionViewDirection ) );
  55. const dotNL = saturate( dot( transformedNormalView, lightDirection ) );
  56. const dotNV = saturate( dot( transformedNormalView, positionViewDirection ) );
  57. const dotNH = saturate( dot( transformedNormalView, halfDir ) );
  58. const dotVH = saturate( dot( positionViewDirection, halfDir ) );
  59. const F = F_Schlick( { f0, f90, dotVH } );
  60. const V = V_GGX_SmithCorrelated( { alpha, dotNL, dotNV } );
  61. const D = D_GGX( { alpha, dotNH } );
  62. return mul( F, mul( V, D ) );
  63. } ); // validated
  64. export const RE_Direct_Physical = new ShaderNode( ( inputs ) => {
  65. const { lightDirection, lightColor, directDiffuse, directSpecular } = inputs;
  66. const dotNL = saturate( dot( transformedNormalView, lightDirection ) );
  67. let irradiance = mul( dotNL, lightColor );
  68. irradiance = mul( irradiance, Math.PI ); // punctual light
  69. addTo( directDiffuse, mul( irradiance, BRDF_Lambert( { diffuseColor: diffuseColor.rgb } ) ) );
  70. addTo( directSpecular, mul( irradiance, BRDF_GGX( { lightDirection, f0: specularColor, f90: 1, roughness } ) ) );
  71. } );
  72. export const PhysicalLightingModel = new ShaderNode( ( inputs/*, builder*/ ) => {
  73. // PHYSICALLY_CORRECT_LIGHTS <-> builder.renderer.physicallyCorrectLights === true
  74. RE_Direct_Physical( inputs );
  75. } );