apply-light-color.glsl.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Copyright (c) 2017-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. *
  6. * adapted from three.js (https://github.com/mrdoob/three.js/)
  7. * which under the MIT License, Copyright © 2010-2021 three.js authors
  8. */
  9. export const apply_light_color = `
  10. #ifdef dIgnoreLight
  11. #ifdef bumpEnabled
  12. if (uBumpFrequency > 0.0 && uBumpAmplitude > 0.0 && bumpiness > 0.0) {
  13. material.rgb += fbm(vModelPosition * uBumpFrequency) * (uBumpAmplitude * bumpiness) / uBumpFrequency;
  14. material.rgb -= bumpiness / (2.0 * uBumpFrequency);
  15. }
  16. #endif
  17. gl_FragColor = material;
  18. #else
  19. #ifdef bumpEnabled
  20. if (uBumpFrequency > 0.0 && uBumpAmplitude > 0.0 && bumpiness > 0.0) {
  21. normal = perturbNormal(-vViewPosition, normal, fbm(vModelPosition * uBumpFrequency), (uBumpAmplitude * bumpiness) / uBumpFrequency);
  22. }
  23. #endif
  24. vec4 color = material;
  25. ReflectedLight reflectedLight = ReflectedLight(vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
  26. PhysicalMaterial physicalMaterial;
  27. physicalMaterial.diffuseColor = color.rgb * (1.0 - metalness);
  28. #ifdef enabledFragDepth
  29. physicalMaterial.roughness = min(max(roughness, 0.0525), 1.0);
  30. #else
  31. vec3 dxy = max(abs(dFdx(normal)), abs(dFdy(normal)));
  32. float geometryRoughness = max(max(dxy.x, dxy.y), dxy.z);
  33. physicalMaterial.roughness = min(max(roughness, 0.0525) + geometryRoughness, 1.0);
  34. #endif
  35. physicalMaterial.specularColor = mix(vec3(0.04), color.rgb, metalness);
  36. physicalMaterial.specularF90 = 1.0;
  37. GeometricContext geometry;
  38. geometry.position = -vViewPosition;
  39. geometry.normal = normal;
  40. geometry.viewDir = normalize(vViewPosition);
  41. IncidentLight directLight;
  42. #pragma unroll_loop_start
  43. for (int i = 0; i < dLightCount; ++i) {
  44. directLight.direction = uLightDirection[i];
  45. directLight.color = uLightColor[i] * PI; // * PI for punctual light
  46. RE_Direct_Physical(directLight, geometry, physicalMaterial, reflectedLight);
  47. }
  48. #pragma unroll_loop_end
  49. vec3 irradiance = uAmbientColor * PI; // * PI for punctual light
  50. RE_IndirectDiffuse_Physical(irradiance, geometry, physicalMaterial, reflectedLight);
  51. // indirect specular only metals
  52. vec3 radiance = uAmbientColor * metalness;
  53. vec3 iblIrradiance = uAmbientColor * metalness;
  54. vec3 clearcoatRadiance = vec3(0.0);
  55. RE_IndirectSpecular_Physical(radiance, iblIrradiance, clearcoatRadiance, geometry, physicalMaterial, reflectedLight);
  56. vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;
  57. outgoingLight = clamp(outgoingLight, 0.01, 0.99); // prevents black artifacts on specular highlight with transparent background
  58. gl_FragColor = vec4(outgoingLight, color.a);
  59. #endif
  60. #if defined(dXrayShaded_on)
  61. gl_FragColor.a *= 1.0 - pow(abs(dot(normal, vec3(0.0, 0.0, 1.0))), uXrayEdgeFalloff);
  62. #elif defined(dXrayShaded_inverted)
  63. gl_FragColor.a *= pow(abs(dot(normal, vec3(0.0, 0.0, 1.0))), uXrayEdgeFalloff);
  64. #endif
  65. gl_FragColor.rgb *= uExposure;
  66. `;