3layout(location = 0) in vec3 fragColor;
4layout(location = 1) in vec3 fragPosWorld;
5layout(location = 2) in vec3 fragNormalWorld;
6layout(location = 3) in vec2 fragUv;
8layout(location = 0) out vec4 outColor;
11 vec4 position; // ignore w
12 vec4 color; // w is intensity
16layout(set = 0, binding = 0) uniform GlobalUbo {
20 vec4 ambientLightColor; // w is intensity
21 PointLight pointLights[10];
25layout(set = 1, binding = 1) uniform sampler2D diffuseMap;
27layout(push_constant) uniform Push {
33 vec3 specularLight = vec3(0.0);
34 vec3 surfaceNormal = normalize(gl_FrontFacing ? fragNormalWorld : -fragNormalWorld);
35 vec3 diffuseLight = ubo.ambientLightColor.rgb * ubo.ambientLightColor.a;
37 vec3 cameraPosWorld = ubo.invView[3].xyz;
38 vec3 viewDirection = normalize(cameraPosWorld - fragPosWorld);
40 for (int i = 0; i < ubo.numLights; i++) {
41 PointLight light = ubo.pointLights[i];
42 vec3 directionToLight = light.position.xyz - fragPosWorld;
43 float distanceSquared = dot(directionToLight, directionToLight);
44 float attenuation = distanceSquared > 0.001 ? (light.position.w + 1.0) / distanceSquared : 0.0;
45 directionToLight = normalize(directionToLight);
47 float cosAngIncidence = max(dot(surfaceNormal, directionToLight), 0);
48 vec3 intensity = light.color.rgb * light.color.a * attenuation;
50 if (cosAngIncidence > 0) {
51 vec3 halfVector = normalize(directionToLight + viewDirection);
52 float cosAngHalf = max(dot(surfaceNormal, halfVector), 0);
54 float specular = pow(cosAngHalf, light.shininess);
56 diffuseLight += intensity * cosAngIncidence;
57 specularLight += intensity * specular;
61 vec3 color = texture(diffuseMap, fragUv).xyz;
62 outColor = vec4(diffuseLight * color + specularLight, 1.0);