It's been I while. I've finally made time to continue the tutorial series, and I would like to make an introduction to explain how shaders handle 3D in GameMaker. This is not a 3D tutorial for the GameMaker Language, but only for the shader aspect of it. To do this it is important that you read the Vertex Shader Tutorial.
getting 3D information
In order to make a shader for 3D you will most likely need to no 3d information such as position and normals. In the Vertex Shader Tutorial I explained in briefly how to get the position with the "in_Position" vector (only in the vertex shader). In order to add this to the fragment shader you'll need a varying vec3 for position. Create one and call it "v_vPosition". Make sure it's in the fragment shader also.
To get the position in 3D we will simply use the Z component along with the X and Y components. You can also get the normals (which is essentially a vector perpendicular to the currently processed triangle) of the triangles as they are set (d3d_draw functions have the normals already). When you apply the shader to a 3D object then you can retrieve the normal which is useful for lighting.
To get the position in 3D we will simply use the Z component along with the X and Y components. You can also get the normals (which is essentially a vector perpendicular to the currently processed triangle) of the triangles as they are set (d3d_draw functions have the normals already). When you apply the shader to a 3D object then you can retrieve the normal which is useful for lighting.
Flood shader
For our first example we'll make a simple shader which makes all 3D objects below a certain height turn black. This is rather simple to do. In the fragment shader we can check if "v_vPosition.z" is less than your desired height, then set it to black if it is. Because the vertex shader is only run for every vertex it's not precise enough for this shader. That is why we passed it to the fragment shader so that we can check each pixel rather than each vertex. For my example I used:
if (v_vPosition.z>0.0)
{
gl_FragColor = vec4(0.0,0.0,0.0,1.0);
}
Here is a screenshot of a simple 3D demo scene:
if (v_vPosition.z>0.0)
{
gl_FragColor = vec4(0.0,0.0,0.0,1.0);
}
Here is a screenshot of a simple 3D demo scene:
As you can see the parts below 0 Z are black.
DOWNLOAD EXAMPLE
DOWNLOAD EXAMPLE