3D Vertex manipulating Shader in 3D FPS game (GMS:2)

DUBBELFISK

Member
Hello,
I'm creating a 3D FPS game in GMS2 with doom-like graphics (all enemies are planes facing you etc).
I've made a simple shader that I'm using to manipulate the world. It looks like this:

Code:
void main()
{
    vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);

    object_space_pos.z -= sin(0.01*(time+in_Position.x))*8.0;
    object_space_pos.x -= sin(0.01*(time+in_Position.y))*8.0;
    object_space_pos.y -= sin(0.01*(time+in_Position.x))*8.0;
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
    
    v_vColour = in_Colour;
    v_vTexcoord = in_TextureCoord;
}
time is just a uniform float that increases each step.

This creates a cool effect that wobbles the entire world and I use this when I draw the entire geometry of my game like this:
Code:
shader_set(shd_3D);
    shader_set_uniform_f(u_t,time);

    var mat = matrix_build(0, 0, 0, 0, 0, 0, 1, 1, 1);
    matrix_set(matrix_world, mat);
    //Draw the buffer
    vertex_submit(vb_walls, pr_trianglelist, sprite_get_texture(wall_sprite, wall_ii));//surface_get_texture(surf));//
    shader_reset();
what I'm drawing here (vb_walls) is a vertex buffer with all the world geometry in it.

My problem occurs when I'm drawing all the other objects in the game (like enemies).
I want the shader to apply to them as well, however their movement does not sync with the world geometry movement.
Right now I'm drawing them in the same place as the previous code, just going through all of them and drawing their vertex buffers with their own matrix:
Code:
//DRAW ENEMIES
    if(instance_exists(obj_enemy_parent))
    {
        var _enemies = instance_number(obj_enemy_parent);
        for(i=0;i<_enemies;i+=1)
        {
            var _tmp_enemy = instance_find(obj_enemy_parent,i);
            matrix_set(matrix_world,_tmp_enemy.matrix);
            vertex_submit(_tmp_enemy.vb_plane,pr_trianglelist,sprite_get_texture(_tmp_enemy.sprite,_tmp_enemy.ii));
            //matrix_set(matrix_world, matrix_build_identity());
        }
    }

I'm quite new to both 3D in GMS2 and the vertex shader. (have worked a bit with fragment shaders before)
Does anyone know how I would solve this?
Sorry If I'm not explaining my problem good enough.

I guess my main issue is trying to sync the shader effect to multiple vertex buffers (with different matrixes)
 

kraifpatrik

(edited)
GameMaker Dev.
Hey there, good to see more people getting into 3D! After skimming through the code, I think that the problem is that you are calculating the wobble based on vertex positions, before they are transformed into world space. What I mean is that if there is a vertex in the model of the world at [10,10,0] for example and matrix_world is an identity, then the wobble will be calculated from [10,10,0], but if the enemy model has a vertex on [0,0,0] for example and matrix_world translates it to [10,10,0], the wobble will still be calculated from the zeros, hence the out-of-sync motion. You should first multiply the in_Position with matrix world, then apply wobble based on the resulting coords and then multiply with view and projection matrices. GL!
 

DUBBELFISK

Member
Oh thanks! I will try this asap!
An yes 3D is so much fun! Just got into it and i feel like there is SOOOO much to learn.
 

DUBBELFISK

Member
It worked!
However some objects (with different zrotations) will wobble "inversed" instead (when I manipulate the object_space_pos.x and y. How do I take rotation in account as well?

Thanks so much for the help!
 

DUBBELFISK

Member
Hey there, good to see more people getting into 3D! After skimming through the code, I think that the problem is that you are calculating the wobble based on vertex positions, before they are transformed into world space. What I mean is that if there is a vertex in the model of the world at [10,10,0] for example and matrix_world is an identity, then the wobble will be calculated from [10,10,0], but if the enemy model has a vertex on [0,0,0] for example and matrix_world translates it to [10,10,0], the wobble will still be calculated from the zeros, hence the out-of-sync motion. You should first multiply the in_Position with matrix world, then apply wobble based on the resulting coords and then multiply with view and projection matrices. GL!
Hey!
I got it to work with the WORLD_MATRIX in account.
However, now I have a hard time to adjust the aiming (done with the mouse) to the wobbling world (my bullets won't travel along the lines I am aiming)
Do you have any tips on how to adjust the projection/aiming to the shader? I tried to apply the same wobbling function to the camera update scripts z and zto, but ti doesn't really work... the bullets trajectory appear above/under the crosshair when I shoot.
 

kraifpatrik

(edited)
GameMaker Dev.
Hmm, not sure if I can help you there :-/ Maybe wobble the crosshair in the GUI as well, to make it look intentional?
 
Top