Skip to content

Commit

Permalink
Update lighting fragment shader
Browse files Browse the repository at this point in the history
- Simplify the computation process of pointlight source
- Make light verge smoother
  • Loading branch information
Jim00000 committed Mar 16, 2021
1 parent e0994c1 commit 0a83999
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions jpc/shaders/lighting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,13 @@ void main()
float dist = distance(lightsrc[i], pixelPos + vec2(0.0, 24.0));
float dd = lightRadius[i] - dist;
float totalBrightness = 0.0;
float magnitude = 1.3;
float time = uTime[i];
float vibration = vibrate(time);
int type = lightType[i];

// point light
if(isPointLight(type) == true && dd >= 0.0) {
vec2 toLight = abs(lightsrc[i] - pixelPos + vec2(0.0, -24.0));
float brightness = clamp(dot(normalize(toLight), pixelPos), 0.0, 1.0);
brightness *= clamp(1.0 - (length(toLight) / lightRadius[i] * vibration), 0.0, 1.0) * magnitude;
float brightness = 1.0 - dist / lightRadius[i];
totalBrightness += brightness;
}

Expand All @@ -153,12 +150,16 @@ void main()
if(abs(dd) >= 0.0 && theta <= perspective[i] * vibration) {
vec2 toLight = abs(lightsrc[i] - pixelPos + vec2(0.0, -24.0));
float brightness = clamp(dot(normalize(toLight), pixelPos), 0.0, 1.0) * clamp(1.0 - pow((theta / (perspective[i] * vibration)), 1.2), globalIllumination, 1.0);
brightness *= clamp(1.0 - (length(toLight) / fSpotlightRadius[i]), 0.0, 1.0) * magnitude;
totalBrightness += brightness;
brightness *= clamp(1.0 - (length(toLight) / fSpotlightRadius[i]), globalIllumination, 1.0);
totalBrightness = max(brightness, totalBrightness);
}
}

mixedLightColor += clamp(totalBrightness, globalIllumination, 1.0) * ambientColor[i];
// Original method. With this approach, the light verge is quite explicit if multiple light sources overlap
//mixedLightColor += totalBrightness * ambientColor[i];

// Use this approach to make light verge smoother if multiple light sources overlap.
mixedLightColor = mix(mixedLightColor, ambientColor[i], pow(totalBrightness, 1.4));
}

finalColor = diffuseColor.xyz * mixedLightColor;
Expand Down

0 comments on commit 0a83999

Please sign in to comment.