#version 130 #define EGXGL_SOFT_PARTICLES 1 #define EGXGL_MULTISAMPLE 1 in vec3 norm; in vec4 rgba; in vec2 uv; in vec4 eyeView; in vec3 wrlView; uniform sampler2D diffuse; uniform vec2 fogDist; uniform vec3 fogColor; uniform float alphaCutout; #define MAX_LIGHTS 4 uniform int lightCount; uniform vec4 lightColor[MAX_LIGHTS]; uniform vec4 lightPosit[MAX_LIGHTS]; out vec4 FragColor; #if EGXGL_SOFT_PARTICLES out float FragEyeDepth; #endif void main() { vec4 texColor = texture(diffuse, uv); /* swy: this alpha-testing path could be potentially factored out into its own thing. the cutout/threshold value is either 0.5 or 0.0 (for blending) */ if (texColor.a < alphaCutout) discard; float fResult = 0.f; if (fogDist.x > 0) { float z = abs(eyeView.z / eyeView.w); float fStart = fogDist.x, fEnd = fogDist.y; fResult = 1.0 - ((fEnd - z) / (fEnd - fStart)); } vec4 litColor = vec4(0); if (lightCount > 0) { for (int i = 0; i < lightCount; i++) { /* swy: calculate the direction vector of the omni light for this position, calculate the distance between both points to get the attenuation */ vec3 lightDir = normalize(lightPosit[i].xyz - wrlView.xyz); float lightDist = distance(wrlView.xyz, lightPosit[i].xyz); vec3 lightTint = lightColor[i].rgb; float lightRadius = lightColor[i].a; /* swy: calculate the attenuation at this point */ float distAttenuation = smoothstep(lightRadius, 0, lightDist); /* swy: calculate the amount of light received depending on where is this point facing to: opposing the light direction => full strength / looking in the same direction as the light => full shadow */ float lightAmount = max(dot(norm, lightDir), 0); litColor.rgb += (lightAmount * lightTint) * distAttenuation; } } FragColor = texColor * (rgba + litColor); /* swy: mix in the correct fog term, if needed; additive and subtractive entities shouldn't use it */ FragColor.rgb = mix( FragColor.rgb, fogColor.rgb, min(max(fResult, 0.0), 0.7) ); #if EGXGL_SOFT_PARTICLES FragEyeDepth = eyeView.z; #endif } /********/