ToonLightingModel.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import LightingModel from '../core/LightingModel.js';
  2. import BRDF_Lambert from './BSDF/BRDF_Lambert.js';
  3. import { diffuseColor } from '../core/PropertyNode.js';
  4. import { normalGeometry } from '../accessors/NormalNode.js';
  5. import { tslFn, float, vec2, vec3 } from '../shadernode/ShaderNode.js';
  6. import { mix, smoothstep } from '../math/MathNode.js';
  7. import { materialReference } from '../accessors/MaterialReferenceNode.js';
  8. const getGradientIrradiance = tslFn( ( { normal, lightDirection, builder } ) => {
  9. // dotNL will be from -1.0 to 1.0
  10. const dotNL = normal.dot( lightDirection );
  11. const coord = vec2( dotNL.mul( 0.5 ).add( 0.5 ), 0.0 );
  12. if ( builder.material.gradientMap ) {
  13. const gradientMap = materialReference( 'gradientMap', 'texture' ).context( { getUV: () => coord } );
  14. return vec3( gradientMap.r );
  15. } else {
  16. const fw = coord.fwidth().mul( 0.5 );
  17. return mix( vec3( 0.7 ), vec3( 1.0 ), smoothstep( float( 0.7 ).sub( fw.x ), float( 0.7 ).add( fw.x ), coord.x ) );
  18. }
  19. } );
  20. class ToonLightingModel extends LightingModel {
  21. direct( { lightDirection, lightColor, reflectedLight }, stack, builder ) {
  22. const irradiance = getGradientIrradiance( { normal: normalGeometry, lightDirection, builder } ).mul( lightColor );
  23. reflectedLight.directDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor: diffuseColor.rgb } ) ) );
  24. }
  25. indirectDiffuse( { irradiance, reflectedLight } ) {
  26. reflectedLight.indirectDiffuse.addAssign( irradiance.mul( BRDF_Lambert( { diffuseColor } ) ) );
  27. }
  28. }
  29. export default ToonLightingModel;