drawing more than one of my glowing lights makes them share the light, cant split them

55 Views Asked by At

I am trying to create two independent glowing lights but when a make the second share the light stretches between the 2

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = fragCoord/iResolution.xy;

uv =(fragCoord-.5*iResolution.xy)/iResolution.y;

vec3 col = vec3(0.);
 
float radius = 0.5;
float glowSpeed = 1.;
vec2 glowPos = vec2(uv.x, uv.y+0.5);
vec2 glowPos2 = vec2(uv.x+0.5, uv.y+0.0);
float glowCol1 = radius * (cos(glowSpeed * iTime) + 6.) / 7. - length(uv+glowPos);    
float glowCol2 =  radius * (cos(glowSpeed * iTime) + 6.) / 7. - length(uv+glowPos2);  
col += vec3(glowCol1, glowCol1, 0.);

col += vec3(glowCol2, glowCol2, 0.);



// Output to screen
fragColor = vec4(col, 1);
}

enter image description here

1

There are 1 best solutions below

0
On

uv is a position relative to the fragment currently being processed. So the position of a light source must not depend on uv. e.g.:

vec2 glowPos = vec2(0.5, 0.5);
vec2 glowPos2 = vec2(-0.5, -0.5);

The distance between 2 points is the length of the vector from one point to the other. A vector between 2 points is calculated by subtracting one point from the other, but not by calculating the sum:

float glowCol1 = radius * (cos(glowSpeed * iTime) + 6.) / 7. - length(uv-glowPos);    
float glowCol2 = radius * (cos(glowSpeed * iTime) + 6.) / 7. - length(uv-glowPos2); 

The result for glowCol1 and glowCol2 can become negative. Thus, one light source would negatively affect the other. You must clamp the result in the range [0, 1]:

glowCol1 = clamp(glowCol1, 0.0, 1.0);
glowCol2 = clamp(glowCol2, 0.0, 1.0);

Complete and working shader:

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = fragCoord/iResolution.xy;
    uv = uv * 2.0 - 1.0;
    uv.x *= iResolution.x / iResolution.y;

    vec3 col = vec3(0.);

    float radius = 0.5;
    float glowSpeed = 1.;
    vec2 glowPos = vec2(0.5, 0.5);
    vec2 glowPos2 = vec2(-0.5, -0.5);
    
    float glowCol1 = radius * (cos(glowSpeed * iTime) + 6.) / 7. - length(uv-glowPos);    
    float glowCol2 = radius * (cos(glowSpeed * iTime) + 6.) / 7. - length(uv-glowPos2); 
    glowCol1 = clamp(glowCol1, 0.0, 1.0);
    glowCol2 = clamp(glowCol2, 0.0, 1.0);
    col += vec3(glowCol1, glowCol1, 0.);
    col += vec3(glowCol2, glowCol2, 0.);

    // Output to screen
    fragColor = vec4(col, 1);
}