Blending lights rays with base texture.

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Andrey01
Posts: 57
Joined: Mon Jul 27, 2020 9:08 pm

Blending lights rays with base texture.

Post 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?
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Blending lights rays with base texture.

Post 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
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Andrey01
Posts: 57
Joined: Mon Jul 27, 2020 9:08 pm

Re: Blending lights rays with base texture.

Post 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.
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Blending lights rays with base texture.

Post by CuteAlien »

No, I meant simply not changing alpha. Like I wrote above (you can set rgb values like that in glsl).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Blending lights rays with base texture.

Post 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.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Andrey01
Posts: 57
Joined: Mon Jul 27, 2020 9:08 pm

Re: Blending lights rays with base texture.

Post 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.
Noiecity
Posts: 92
Joined: Wed Aug 23, 2023 7:22 pm
Contact:

Re: Blending lights rays with base texture.

Post 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
**
If you are looking for people with whom to develop your game, even to try functionalities, I can help you, free and in an anonymous way if necessary. You can send me a private message.

https://www.artstation.com/noiecty
**
Andrey01
Posts: 57
Joined: Mon Jul 27, 2020 9:08 pm

Re: Blending lights rays with base texture.

Post 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.
CuteAlien
Admin
Posts: 9643
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Blending lights rays with base texture.

Post 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.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply