Page 1 of 1

[Solved] Problem with light shader for g3d

Posted: Thu Feb 16, 2023 7:26 pm
by Bigfoot71
Hey everyone :awesome:

I post because there I am lost with a shader that I tried to write (inspired by the one proposed by groverburger here), it produces an effect that I cannot solve, here is the problem in image:
Image

We can see that the problem is in the corner of the wall, the transition between the two is completely messed up (I still played with the values ​​to make it more obvious and this happens in all corners of the walls regardless of the direction).

And I can't determine if the problem comes from the shader or the normals of my wall model, so I prefer to share the shader with you first if someone more comfortable with GLSL than me can tell me if there is an error in it before I look at the normals of the model (which seems to me to be quite correct after dozens of proofreading...)

So here is my shader that I haven't stopped tweaking to always have the same result...

Code: Select all

// variables provided by g3d's vertex shader
varying vec4 worldPosition;
varying vec3 vertexNormal;

// the model matrix comes from the camera automatically
uniform mediump mat4 modelMatrix;

// value given by main program
uniform vec3 lightPosition;

// constant values
const float light_dist = 10;
const float ambient = 0.2;


vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {

    // Get the current pixel, if alpha is zero return nothing

    vec4 pixel = Texel(texture, texture_coords);
    if (pixel.a == 0.0) discard;

    // Get the distance between the light position and the point in the world

    vec3 delta_pos = lightPosition.xyz - worldPosition.xyz;
    float dist = length(delta_pos);

    if (dist > light_dist)
        return vec4(pixel.rgb * ambient, 1.0) * color;

    // Computed by the dot product of the normal vector and the direction to the light source

    vec3 lightDirection = normalize(delta_pos);
    vec3 normal = normalize(mat3(modelMatrix) * vertexNormal);
    float diffuse = max(dot(lightDirection, normal), 0.0);

    // Calculate the final brightness, taking into account the distance that light can travel and ambient value

    float lightness = max(diffuse * smoothstep(light_dist, 0.0, dist) + ambient, ambient);

    return vec4((pixel.rgb * color.rgb) * lightness, 1.0);

}
I made a small demo quickly with the generation of the walls and the shader.

Edit: I just tried with the cube model provided in the g3d repository and there is the same behavior, so I actually think the normals are good.

Re: Problem with light shader for g3d

Posted: Thu Feb 16, 2023 8:15 pm
by pgimeno
Without trying your example, I can say that I don't see anything that looks obviously wrong in the picture. Have you tried with a texture that helps discern UV coordinates, like e.g. the default one from Blender? Could you post a picture with it?

Re: Problem with light shader for g3d

Posted: Thu Feb 16, 2023 9:16 pm
by Bigfoot71
pgimeno wrote: Thu Feb 16, 2023 8:15 pm Without trying your example, I can say that I don't see anything that looks obviously wrong in the picture. Have you tried with a texture that helps discern UV coordinates, like e.g. the default one from Blender? Could you post a picture with it?
Indeed I feel very stupid, I think I spent way too much time in front of my PC :death:
The shader produces exactly what should happen in real life, I tried in real life with a light against a wall and in blender and everything matches :ultrahappy:

Image

Excuse me, at least I will have shared a shader ^^

Re: Problem with light shader for g3d

Posted: Fri Feb 17, 2023 7:19 am
by pgimeno
I meant a texture like e.g. this one: https://realtimevfx.com/uploads/default ... 10a4f.jpeg but if you have it sorted out, great!

Re: Problem with light shader for g3d

Posted: Fri Feb 17, 2023 11:00 am
by Bigfoot71
I understood what you meant but speaking of Blender it made me think that I could try it directly in it, thank you ^^