BRDF_Sheen.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { transformedNormalView } from '../../accessors/NormalNode.js';
  2. import { positionViewDirection } from '../../accessors/PositionNode.js';
  3. import { sheen, sheenRoughness } from '../../core/PropertyNode.js';
  4. import { tslFn, float } from '../../shadernode/ShaderNode.js';
  5. // https://github.com/google/filament/blob/master/shaders/src/brdf.fs
  6. const D_Charlie = ( roughness, dotNH ) => {
  7. const alpha = roughness.pow2();
  8. // Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF"
  9. const invAlpha = float( 1.0 ).div( alpha );
  10. const cos2h = dotNH.pow2();
  11. const sin2h = cos2h.oneMinus().max( 0.0078125 ); // 2^(-14/2), so sin2h^2 > 0 in fp16
  12. return float( 2.0 ).add( invAlpha ).mul( sin2h.pow( invAlpha.mul( 0.5 ) ) ).div( 2.0 * Math.PI );
  13. };
  14. // https://github.com/google/filament/blob/master/shaders/src/brdf.fs
  15. const V_Neubelt = ( dotNV, dotNL ) => {
  16. // Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
  17. return float( 1.0 ).div( float( 4.0 ).mul( dotNL.add( dotNV ).sub( dotNL.mul( dotNV ) ) ) );
  18. };
  19. const BRDF_Sheen = tslFn( ( { lightDirection } ) => {
  20. const halfDir = lightDirection.add( positionViewDirection ).normalize();
  21. const dotNL = transformedNormalView.dot( lightDirection ).clamp();
  22. const dotNV = transformedNormalView.dot( positionViewDirection ).clamp();
  23. const dotNH = transformedNormalView.dot( halfDir ).clamp();
  24. const D = D_Charlie( sheenRoughness, dotNH );
  25. const V = V_Neubelt( dotNV, dotNL );
  26. return sheen.mul( D ).mul( V );
  27. } );
  28. export default BRDF_Sheen;