apply-light-color.glsl.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Copyright (c) 2017-2021 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 bumpEnabled
  11. if (uBumpFrequency > 0.0 && uBumpAmplitude > 0.0) {
  12. vec3 bumpNormal = perturbNormal(-vViewPosition, normal, fbm(vModelPosition * uBumpFrequency), (uBumpAmplitude * bumpiness) / uBumpFrequency);
  13. #ifdef enabledFragDepth
  14. if (!isNaN(bumpNormal.x) && !isNaN(bumpNormal.y) && !isNaN(bumpNormal.z)) {
  15. normal = bumpNormal;
  16. }
  17. #else
  18. normal = bumpNormal;
  19. #endif
  20. }
  21. #endif
  22. vec4 color = material;
  23. ReflectedLight reflectedLight = ReflectedLight(vec3(0.0), vec3(0.0), vec3(0.0), vec3(0.0));
  24. PhysicalMaterial physicalMaterial;
  25. physicalMaterial.diffuseColor = color.rgb * (1.0 - metalness);
  26. #ifdef enabledFragDepth
  27. physicalMaterial.roughness = min(max(roughness, 0.0525), 1.0);
  28. #else
  29. vec3 dxy = max(abs(dFdx(normal)), abs(dFdy(normal)));
  30. float geometryRoughness = max(max(dxy.x, dxy.y), dxy.z);
  31. physicalMaterial.roughness = min(max(roughness, 0.0525) + geometryRoughness, 1.0);
  32. #endif
  33. physicalMaterial.specularColor = mix(vec3(0.04), color.rgb, metalness);
  34. physicalMaterial.specularF90 = 1.0;
  35. GeometricContext geometry;
  36. geometry.position = -vViewPosition;
  37. geometry.normal = normal;
  38. geometry.viewDir = normalize(vViewPosition);
  39. IncidentLight directLight;
  40. #pragma unroll_loop_start
  41. for (int i = 0; i < dLightCount; ++i) {
  42. directLight.direction = uLightDirection[i];
  43. directLight.color = uLightColor[i] * PI; // * PI for punctual light
  44. RE_Direct_Physical(directLight, geometry, physicalMaterial, reflectedLight);
  45. }
  46. #pragma unroll_loop_end
  47. vec3 irradiance = uAmbientColor * PI; // * PI for punctual light
  48. RE_IndirectDiffuse_Physical(irradiance, geometry, physicalMaterial, reflectedLight);
  49. // indirect specular only metals
  50. vec3 radiance = uAmbientColor * metalness;
  51. vec3 iblIrradiance = uAmbientColor * metalness;
  52. vec3 clearcoatRadiance = vec3(0.0);
  53. RE_IndirectSpecular_Physical(radiance, iblIrradiance, clearcoatRadiance, geometry, physicalMaterial, reflectedLight);
  54. vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular;
  55. gl_FragColor = vec4(outgoingLight, color.a);
  56. #ifdef dXrayShaded
  57. gl_FragColor.a *= 1.0 - pow(abs(dot(normal, vec3(0.0, 0.0, 1.0))), uXrayEdgeFalloff);
  58. #endif
  59. `;