PhongLightingModel.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import LightingModel from '../core/LightingModel.js';
  2. import F_Schlick from './BSDF/F_Schlick.js';
  3. import BRDF_Lambert from './BSDF/BRDF_Lambert.js';
  4. import { diffuseColor } from '../core/PropertyNode.js';
  5. import { transformedNormalView } from '../accessors/NormalNode.js';
  6. import { materialSpecularStrength } from '../accessors/MaterialNode.js';
  7. import { shininess, specularColor } from '../core/PropertyNode.js';
  8. import { positionViewDirection } from '../accessors/PositionNode.js';
  9. import { tslFn, float } from '../shadernode/ShaderNode.js';
  10. const G_BlinnPhong_Implicit = () => float( 0.25 );
  11. const D_BlinnPhong = tslFn( ( { dotNH } ) => {
  12. return shininess.mul( float( 0.5 ) ).add( 1.0 ).mul( float( 1 / Math.PI ) ).mul( dotNH.pow( shininess ) );
  13. } );
  14. const BRDF_BlinnPhong = tslFn( ( { lightDirection } ) => {
  15. const halfDir = lightDirection.add( positionViewDirection ).normalize();
  16. const dotNH = transformedNormalView.dot( halfDir ).clamp();
  17. const dotVH = positionViewDirection.dot( halfDir ).clamp();
  18. const F = F_Schlick( { f0: specularColor, f90: 1.0, dotVH } );
  19. const G = G_BlinnPhong_Implicit();
  20. const D = D_BlinnPhong( { dotNH } );
  21. return F.mul( G ).mul( D );
  22. } );
  23. class PhongLightingModel extends LightingModel {
  24. constructor( specular = true ) {
  25. super();
  26. this.specular = specular;
  27. }
  28. direct( { lightDirection, lightColor, reflectedLight } ) {
  29. const dotNL = transformedNormalView.dot( lightDirection ).clamp();
  30. const irradiance = dotNL.mul( lightColor );
  31. reflectedLight.directDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseColor.rgb } ) ) );
  32. if ( this.specular === true ) {
  33. reflectedLight.directSpecular.addAssign( irradiance.mul( BRDF_BlinnPhong( { lightDirection } ) ).mul( materialSpecularStrength ) );
  34. }
  35. }
  36. indirectDiffuse( { irradiance, reflectedLight } ) {
  37. reflectedLight.indirectDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor } ) ) );
  38. }
  39. }
  40. export default PhongLightingModel;