Guest User

Untitled

a guest
Jul 21st, 2025
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. for (int i = 0; i < numActiveLights; ++i)
  2. {
  3. vec3 lightPos = lights[i].position.xyz;
  4. float lightType = lights[i].position.w;
  5.  
  6. vec3 L = normalize(lightPos - FragPos_world);
  7. vec3 H = normalize(V + L);
  8. float distance = length(lightPos - FragPos_world);
  9. float NdotL = max(dot(N, L), 0.0);
  10. vec3 radiance = lights[i].color.rgb * lights[i].color.a;
  11. float attenuation = 0.0;
  12. float shadow = 0.0;
  13. if (lightType == 0)
  14. {
  15. float radius = lights[i].params1.x;
  16. float radiusFalloff = pow(1.0 - clamp(distance / radius, 0.0, 1.0), 2.0);
  17. attenuation = radiusFalloff / (distance * distance + 1.0);
  18. if(attenuation > 0.0 && (lights[i].shadowMapHandle.x > 0 || lights[i].shadowMapHandle.y > 0) ) shadow = calculatePointShadow(lights[i].shadowMapHandle, FragPos_world, lightPos, lights[i].params2.x, lights[i].params2.y);
  19. }
  20. else
  21. {
  22. float lightCutOff = lights[i].params1.y;
  23. float lightOuterCutOff = lights[i].params1.z;
  24. vec3 lightDir = lights[i].direction.xyz;
  25.  
  26. float theta = dot(L, -lightDir);
  27. if (theta > lightOuterCutOff) {
  28. float epsilon = lightCutOff - lightOuterCutOff;
  29. float cone_intensity = clamp((theta - lightOuterCutOff) / epsilon, 0.0, 1.0);
  30. float radius = lights[i].params1.x;
  31. float radiusFalloff = pow(1.0 - clamp(distance / radius, 0.0, 1.0), 2.0);
  32. attenuation = cone_intensity * radiusFalloff / (distance * distance + 1.0);
  33. float cookie_attenuation = 1.0;
  34. if (attenuation > 0.0 && (lights[i].shadowMapHandle.x > 0 || lights[i].shadowMapHandle.y > 0)) {
  35. float angle_rad = acos(clamp(lightCutOff, -1.0, 1.0));
  36. if (angle_rad < 0.01) angle_rad = 0.01;
  37. mat4 lightProjection = perspective(angle_rad * 2.0, 1.0, 1.0, lights[i].params2.x);
  38. vec3 up_vector = vec3(0,1,0);
  39. if (abs(dot(lightDir, up_vector)) > 0.99) up_vector = vec3(1,0,0);
  40. mat4 lightView = lookAt(lightPos, lightPos + lightDir, up_vector);
  41. mat4 lightSpaceMatrix = lightProjection * lightView;
  42. vec4 fragPosLightSpace = lightSpaceMatrix * vec4(FragPos_world, 1.0);
  43. if (lights[i].cookieMapHandle[0] > 0u || lights[i].cookieMapHandle[1] > 0u) {
  44. vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
  45. projCoords = projCoords * 0.5 + 0.5;
  46. if (projCoords.x >= 0.0 && projCoords.x <= 1.0 && projCoords.y >= 0.0 && projCoords.y <= 1.0) {
  47. sampler2D cookieSampler = sampler2D(lights[i].cookieMapHandle);
  48. cookie_attenuation = texture(cookieSampler, projCoords.xy).r;
  49. } else {
  50. cookie_attenuation = 0.0;
  51. }
  52. }
  53. shadow = calculateSpotShadow(lights[i].shadowMapHandle, fragPosLightSpace, N, L, lights[i].params2.y);
  54. }
  55. attenuation *= cookie_attenuation;
  56. }
  57. }
  58. if(attenuation > 0.0) {
  59. float NDF = DistributionGGX(N, H, roughness);
  60. float G = GeometrySmith(N, V, L, roughness);
  61. vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
  62. vec3 kS = F;
  63. vec3 kD = vec3(1.0) - kS;
  64. kD *= 1.0 - metallic;
  65. vec3 numerator = NDF * G * F;
  66. float denominator = 4.0 * max(dot(N, V), 0.0) * NdotL + 0.001;
  67. vec3 specular = numerator / denominator;
  68. vec3 diffuseContrib = (kD * albedo / PI) * radiance * NdotL * attenuation * (1.0 - shadow);
  69. totalDirectDiffuse += diffuseContrib;
  70. Lo += (diffuseContrib + specular * radiance * NdotL * attenuation * (1.0 - shadow));
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment