GameMaker Z-tilting

S

ShroomTastic

Guest
I added a 3d camera to game maker but I want to sprites to be tilted on the Z axis in a way that the sprites look like they're standing on the floor and not laying down on it. I used a Z-tilting shader tutorial the code runs but the sprite isn't tilting.
 
Last edited by a moderator:
S

ShroomTastic

Guest
//This is what I used
//Vertex shader

attribute vec3 in_Position;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
vec4 col = in_Colour;
vec4 object_space_pos = vec4( in_Position, 1.0);

float top = 1.0 - mod( col.b * 255.0, 2.0); // identify upper vertex
object_space_pos.z -= 255.0 * col.a * top; //tilt using alpha
object_space_pos.y += col.a / 10.0; //tweak zfighting

gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
v_vColour = vec4(col.rgb, 1.0); // lock alpha blend to fully opaque
v_vTexcoord = in_TextureCoord;
}

//fragment shader

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
vec4 sprite_col = texture2D( gm_BaseTexture, v_vTexcoord );
if (sprite_col.a < 1.0) { discard; } // discard non opaque sprite pixels
gl_FragColor = sprite_col * v_vColour;
}
 
A

Ariak

Guest
Its a one line edited to the example project that came with my blog post.
Modify the vertex shader as follows:

Code:
    // === Z-tilting === //
    float top = 1.0-mod( col.b * 255.0, 2.0);
    object_space_pos.z-=255.0*col.a*top;
    object_space_pos.y+=255.0*col.a*top;            // NEW LINE: col.a contains the sprite height - shift back y to overlap the bottom vertex => stands up straight.
    object_space_pos.y+=col.a/10.0;                    // optional: helps with z-fighting
 
S

ShroomTastic

Guest
It still didn't tilt I don't know where I went wrong.

if event_number!=0 exit;
gpu_set_ztestenable(true);
gpu_set_zwriteenable(true);
shader_set(shd_zsort);
shader_enable_corner_id(true);

if event_number!=0 exit;
shader_reset();
shader_enable_corner_id(false);
gpu_set_ztestenable(false);
gpu_set_zwriteenable(false);

attribute vec3 in_Position;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
vec4 col = in_Colour;
vec4 object_space_pos = vec4( in_Position, 1.0);

float top = 1.0 - mod( col.b * 255.0, 2.0); // identify upper vertex
object_space_pos.z-=255.0*col.a*top; //tilt using alpha
object_space_pos.y+=255.0*col.a*top; // NEW LINE: col.a contains the sprite height - shift back y to overlap the bottom vertex => stands up straight.
object_space_pos.y+=col.a/10.0; // optional: helps with z-fighting

gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
v_vColour = vec4(col.rgb, 1.0); // lock alpha blend to fully opaque
v_vTexcoord = in_TextureCoord;
}

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
vec4 sprite_col = texture2D( gm_BaseTexture, v_vTexcoord );
if (sprite_col.a < 1.0) { discard; } // discard non opaque sprite pixels
gl_FragColor = sprite_col * v_vColour;
}
 
S

ShroomTastic

Guest
Just to be clear I don't know how to code but after trial and error I think the problem is my event_number isn't equal to 0. When I remove the if event_number != 0 exit; and just leave the:

gpu_set_ztestenable(true);
gpu_set_zwriteenable(true);
shader_set(Z_tilt);
shader_enable_corner_id(true);

the sprite does get tilted but without the:

if event_number!=0 exit;
shader_reset();
shader_enable_corner_id(false);
gpu_set_ztestenable(false);
gpu_set_zwriteenable(false);

to revert the settings I get this
 
S

ShroomTastic

Guest
Also how do I make the sprite's height shorter and just to clarify the object I am trying to tilt is a moving object not sure if that affects anything
 
Last edited by a moderator:
Top