• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [SOLVED] Shader draw coordinates change on room size!?

I am drawing noise. For some reason, the room size is affecting the final draw position of the shader, and I am lost as to why. Technically, it's not the room size per se, but the further right (x positive) one draws into the room, the further the miss.

I can see that the shader is being correctly located at x and y, but the larger the room size, the further the noise misses being drawn inside the defined rectangle. Might someone offer some assistance?


Code:
// DRAW EVENT
// this shader draws noise inside a rectangle
// note: using a surface to trigger the draw instead of a sprite
     X1 = x;
     Y1 = y;
     X2 = x + surface_x_size; // (512 pixels)
     Y2 = y + surface_y_size; // (512 pixels)

    var R1   = shader_get_uniform(shader, "Rect");
    var L1   = shader_get_uniform(shader, "Position");

shader_set(shader);
    shader_set_uniform_f(R1, X1, Y1, X2, Y2); // rectangle
    shader_set_uniform_f(L1, (X1 + surface_x_size) / 2, (Y1 + surface_y_size) / 2, 5000); // light position
    if (surface_exists(surface))
        draw_surface(surface, x, y);
shader_reset();
Code:
// VERTEX SHADER
attribute vec4 in_Colour;
attribute vec3 in_Position;
attribute vec2 in_TextureCoord;

varying vec4 VColor;
varying vec2 VTexcoord;
varying vec2 VPosition;

void main()
{
    vec4 Position = vec4( in_Position, 1);
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * Position;
 
    VColor = in_Colour;
    VTexcoord = in_TextureCoord;
    VPosition = in_Position.xy;
}
Code:
// FRAGMENT SHADER
varying vec4 VColor;
varying vec2 VTexcoord;
varying vec2 VPosition;

uniform vec4  Rect;                            
uniform vec3  Position;
// some details snipped

void main()
{
    float  H = F(VPosition);          // Wave height
    vec3   R = N(vec3(VPosition,H));  // Wave normal
    float  L = dot(R,normalize(vec3(VPosition-Position.xy,H*T+Position.z)));
    L = max(1.-(1.-L)*Light,.1);      // Wave lighting
 
    vec4 C = vec4(0.0, 0.0, 0.0, 0.0);
    gl_FragColor = mix(C,vec4(Color.rgb*L,1),H*Color.a);
}
 
Last edited:
There's alot missing in the fragment shader you posted. At least a header with all the definitions. But I guess I wouldn't udnerstand the moise maths anyways.

But what I can tell you is that in_Position in the vertex shader are the vertex coordinates in room space. So the varying VPosition in the fragment shader are interpolated room coordinates. That's why the room position affects the result of the fragment shader.

As said though: I don't understand the maths behind your shader and I can't really image what actually happens on your screen and what you expected to happen. So I can't show you how to fix the code.
 

Bart

WiseBart
Hi,

you seem to be using (X1, Y1) as the rectangle's position/offset, so the position of its top-left corner.
When setting the light position uniform in your code, you're scaling that offset by 1/2 as well, while you're likely trying to position the light at the center of the rectangle (I assume?):
Code:
shader_set_uniform_f(L1, (X1 + surface_x_size) / 2, (Y1 + surface_y_size) / 2, 5000); // light position
In the special case where x and y are 0, this will work as expected. But not in all other cases.
Either use x + surface_size/2 or x1+(x2-x1)/2 to get the correct light position.
You can also get rid of the variables X1 and Y1. You already have that information in the variables x and y.
 
Thank you very much The Reverend, and Bart for writing in!
I am able to position the light correctly regardless of the room size, but the noise is always off the mark. I think it therefore must be related to the Vposition and coordinates code in the fragment shader. I will see what I can do there, and I'll post back here if I can get it solved.

UPDATE: SOLVED
The problem was in the shader code--it was using the scaled coordinates instead of the room coordinates.
 
Last edited:
Top