Page 1 of 1

Blending lights rays with base texture.

Posted: Sun Aug 13, 2023 5:16 pm
by Andrey01
I try to combine in the shader which renders the screen quad the total rays texture with the base texture. The first one contains all simple rendered cube light sources emitting godrays (algorithm based on this implementation: https://lokeshsharma97.wordpress.com/20 ... ys-opengl/ the only difference is I don't add the colors of both textures in the one shader). The second texture contains just all rendered non-glowing mesh objects, in my case this is the room filled with some random furniture. How I combine is just adding colors from both textures:

Code: Select all

vec4 color = base_color + rays_color;

gl_FragColor = color;
GL blending is disabled in my rendered material, i.e.I use video::EMT_SOLID as a base material. How those textures and their combination result look like:

The base texture
Image

The rays texture
Image

The final texture
Image

How you can see, the problem is that the total cube lights colors, especially of the red one, look like semi-transparent. However, in the rays texture they are fully opaque. I tried also to bypass this problem calculating luminance of the given rays color and using this as an alpha component:

Code: Select all

float luminance = dot(rays_color, vec3(0.3, 0.59, 0.11))/3.0; // I divide by 3.0 because I suppose this is the maximum value that the dot product can output, such way normalizing this

// The brighter the given fragment of the rays, the higher its opacity
vec4 color = (1.0-luminance)*base_color + luminance*rays_color;

gl_FragColor = color;
However, that doesn't render the rays around the light sources leaving only the cubes themselves. How could I combine them correctly?

Re: Blending lights rays with base texture.

Posted: Sun Aug 13, 2023 8:32 pm
by CuteAlien
Strange, not sure why you get alpha with EMT_SOLID. Did you try just adding the rgb part? So set color to base_color. And then set color.rgb += rays_color.rgb

Re: Blending lights rays with base texture.

Posted: Sun Aug 13, 2023 8:49 pm
by Andrey01
Do you mean so? Always have alpha = 1.0 independing on the mixed alpha?

Code: Select all

vec4 color = base_color + rays_color;

gl_FragColor = vec4(vec3(color), 1.0);
In any case that doesn't change anything.

Re: Blending lights rays with base texture.

Posted: Sun Aug 13, 2023 9:08 pm
by CuteAlien
No, I meant simply not changing alpha. Like I wrote above (you can set rgb values like that in glsl).

Re: Blending lights rays with base texture.

Posted: Sun Aug 13, 2023 9:14 pm
by CuteAlien
Ah - I get it. This isn't about alpha at all. It just looks like it. It's simply you are for example adding only red - so green/blue are not affected. Which kinda looks transparent. You could do some if check - if any color is set - replace all rgb colors.
Or some other formula - like calculating intensity and adding it to all colors. Or have your lights always have all 3 components, just a bit more red.

Re: Blending lights rays with base texture.

Posted: Wed Aug 16, 2023 9:11 pm
by Andrey01
I've come up with how to solve this issue finally, only partially, because now although the lamps look like opaque, but their rays became very faint. I caulculate the alpha as a ratio of the current fragment's luminance and the original lamp color luminance:

Code: Select all

float luminance = dot(color*255.0, vec3(0.3, 0.59, 0.11)); // 'color' is the current light color that becomes more dim on moving away from the source
float luminance_orig = dot(vec3(mLightColor)*255.0, vec3(0.3, 0.59, 0.11)); // 'mLightColor' is the original light color

float ratio = luminance / luminance_orig;
color = (1.0-ratio)*base_color + ratio*color;

gl_FragColor = vec4(color, 1.0);
Image

In any case, thanks for trying to suggest ideas of fixing that.

Re: Blending lights rays with base texture.

Posted: Wed Aug 23, 2023 7:45 pm
by Noiecity
It must be because the scene contains more red than green, making red look like this, if you try another scene with models that have more green than red, this problem will probably stand out, this also happens in real life, that's why the light of a lamp seems to be less strong during the day

Re: Blending lights rays with base texture.

Posted: Fri Sep 01, 2023 10:54 am
by Andrey01
The red light amount is the same as the green one in my scene. The actual problem was the red light has the green and blue components null, so in the result of the blending the light color with the scene color, the total color gets semitransparent with the red tint. I could fix that by calculating the luminance of each color and their ratio using that as an alpha component. But unfortunately it caused other problem as you can see in the last screenshot, the rays got very faint.

Re: Blending lights rays with base texture.

Posted: Fri Sep 01, 2023 1:33 pm
by CuteAlien
Yeah, it's not the amount of red light. It's the amount of red in your textures. Red light on brown texture (brown has lots of red components). So red light on texture with lots of red = light very faint. Green light on texture with lots of red - very visible. If you put a green texture on the wall the results will be reversed. So don't use pure light with rgb something like 255,0,0. Instead use something like 255, 100, 100 (totally guessing numbers). Light still seems red - but has enough other components to never fade away completely. When you add luminance instead of color you kinda do something like that (adding green/blue components which aren't there). But easier to modify the original light color.