Skip to main content
added 87 characters in body
Source Link
Steyrix
  • 279
  • 1
  • 8

I managed to do it. The key is to use diffusion and ambient light values. If the distance is less than radius of target lightened area, the light should be diffused and combined with ambient.

In case of fragment being too far away from light source, ambient is used to set default light value.

vec2 lightSourcePos = vec2(lightSourceX, lightSourceY);

vec3 ambient = vec3(0.8, 0.8, 0.8);

float dist = distance(lightSourcePos, pos.xy);
float lightIntensity = 1.0/dist;
float targetRadius = 0.3;
float diffuse = 0;

if(dist < targetRadius) {
    diffuse = 1.0 - abs(dist / targetRadius);
}

if (lightIntensity >= lightIntensityCap) {
    lightIntensity = lightIntensityCap;
}

if (lightIntensity <= 1) {
    lightIntensity = 1;
}

vec4 fragColor = texture(textureSample, fragmentUV).rgba;
vec3 lightCol = vec3(lightIntensity, lightIntensity, lightIntensity);
fColor = vec4(min(fragColor.rgb * ((lightCol * diffuse) + ambient), fragColor.rgb), 1.0);

enter image description here

I managed to do it. The key is to use diffusion and ambient light values. If the distance is less than radius of target lightened area, the light should be diffused and combined with ambient.

In case of fragment being too far away from light source, ambient is used to set default light value.

vec2 lightSourcePos = vec2(lightSourceX, lightSourceY);

vec3 ambient = vec3(0.8, 0.8, 0.8);

float dist = distance(lightSourcePos, pos.xy);
float lightIntensity = 1.0/dist;
float targetRadius = 0.3;
float diffuse = 0;

if(dist < targetRadius) {
    diffuse = 1.0 - abs(dist / targetRadius);
}

if (lightIntensity >= lightIntensityCap) {
    lightIntensity = lightIntensityCap;
}

if (lightIntensity <= 1) {
    lightIntensity = 1;
}

vec4 fragColor = texture(textureSample, fragmentUV).rgba;
vec3 lightCol = vec3(lightIntensity, lightIntensity, lightIntensity);
fColor = vec4(min(fragColor.rgb * ((lightCol * diffuse) + ambient), fragColor.rgb), 1.0);

I managed to do it. The key is to use diffusion and ambient light values. If the distance is less than radius of target lightened area, the light should be diffused and combined with ambient.

In case of fragment being too far away from light source, ambient is used to set default light value.

vec2 lightSourcePos = vec2(lightSourceX, lightSourceY);

vec3 ambient = vec3(0.8, 0.8, 0.8);

float dist = distance(lightSourcePos, pos.xy);
float lightIntensity = 1.0/dist;
float targetRadius = 0.3;
float diffuse = 0;

if(dist < targetRadius) {
    diffuse = 1.0 - abs(dist / targetRadius);
}

if (lightIntensity >= lightIntensityCap) {
    lightIntensity = lightIntensityCap;
}

if (lightIntensity <= 1) {
    lightIntensity = 1;
}

vec4 fragColor = texture(textureSample, fragmentUV).rgba;
vec3 lightCol = vec3(lightIntensity, lightIntensity, lightIntensity);
fColor = vec4(min(fragColor.rgb * ((lightCol * diffuse) + ambient), fragColor.rgb), 1.0);

enter image description here

Source Link
Steyrix
  • 279
  • 1
  • 8

I managed to do it. The key is to use diffusion and ambient light values. If the distance is less than radius of target lightened area, the light should be diffused and combined with ambient.

In case of fragment being too far away from light source, ambient is used to set default light value.

vec2 lightSourcePos = vec2(lightSourceX, lightSourceY);

vec3 ambient = vec3(0.8, 0.8, 0.8);

float dist = distance(lightSourcePos, pos.xy);
float lightIntensity = 1.0/dist;
float targetRadius = 0.3;
float diffuse = 0;

if(dist < targetRadius) {
    diffuse = 1.0 - abs(dist / targetRadius);
}

if (lightIntensity >= lightIntensityCap) {
    lightIntensity = lightIntensityCap;
}

if (lightIntensity <= 1) {
    lightIntensity = 1;
}

vec4 fragColor = texture(textureSample, fragmentUV).rgba;
vec3 lightCol = vec3(lightIntensity, lightIntensity, lightIntensity);
fColor = vec4(min(fragColor.rgb * ((lightCol * diffuse) + ambient), fragColor.rgb), 1.0);