Xor Shaders
  • Tutorials
  • About

#7 Introduction to 3D part 3

8/8/2015

7 Comments

 
In the last tutorial I explained how to create 3D lights using vertex attributes. Today, I'll explain 3D distortion. The main difference is that 3D distortion is done in the vertex shader unlike our last 2 shaders. That means you can move each vertex with the vertex shader.

Flattening shapes

We will first try making a shader that flattens the 3d shapes. To do this we will simply set the z position to 0 for all vertices while leaving x and y positions unchanged. Make sure to comment out the normal attribute and use the default pass through fragment shader.
An important variable  is "object_space_pos". It is the variable we can use for setting the position.
We will use the x and y components but will use 0 instead of z.
Here is the result:
Picture
As you can see the shapes are properly flattened. Because you now know how to change the vertex position we'll try making water.

Creating a plane

Before we begin with the water shader, we will need something to apply the shader to. We will make a plane instead of our basic shapes. In GameMaker, this may be a little tricky, because we need to generate one with enough vertices. Here is the code I used:

water = d3d_model_create();
cells = 64;//Number of cells
cell = 8;//Cell width
height = 32;//Water max height
for(var X = -cells/2;X<cells/2;X++)
{
    d3d_model_primitive_begin(water,pr_trianglestrip)
    for(var Y = -cells/2;Y<cells/2-1;Y++)
    {
        d3d_model_vertex(water,X*cell,Y*cell,height)
        d3d_model_vertex(water,(X+1)*cell,Y*cell,height)
    }
    d3d_model_primitive_end(water)
}



This generates a plane model with many vertices to create higher quality water.
Apply your shader to this model instead of the basic shapes using "d3d_model_draw()".

Water shader

We will use our knowledge to make detailed 3d water using perlin noise and height displacement. You should see the Vertex shader tutorial. We will be using the rand and noise functions to determine the height of the water for each vertex so put them in the vertex shader. Now we'll create a variable called height. This will be used to set the height of the water per vertex. Use this code:

float height = noise(in_Position.xy/64.0)*0.4+noise(in_Position.xy/32.0)*0.3+noise(in_Position.xy/16.0)*0.2+noise(in_Position.xy/8.0)*0.1;

That code generates a perlin noise pattern to make waves in the water.
Instead of setting the z position to 0 we'll set it to "height * in_Position.z". This makes it so we can determine the wave height by setting z position. And now finally we'll make a varying float called "v_vHeight" and pass our height variable through it. Now set the color in the fragment shader:

gl_FragColor = vec4(mix(vec3(0.1,0.2,0.4),vec3(0.1,0.1,0.3),v_vHeight),1.0);

This is the result:
Picture
This is a start to your water shader! We'll add more in the next tutorial.

DOWNLOAD EXAMPLE
7 Comments
Odolwa
4/25/2017 10:52:03 am

Hello, Xor.

I recently got Game Maker Studio 2 and carried over a project I'm currently working on which also has the water shader you describe above in one of the rooms. However, the game now seems to slow down and sometimes crash whenever I enter the room where I placed it.

After some testing I narrowed the problem down to the 'for' loop in the Create event where you create the plane. This slow down and crash does not occur in the earlier version of Game Maker, so I'm not entirely sure what's going on. Do you have any ideas?

Thanks.

Reply
Xor link
4/25/2017 02:15:40 pm

Hi Odolwa! There's only two things I can think of that you can do:
(1) Use vertex buffers instead of d3d models. This isn't easy to do, so I'm not going to go in to details as it's not a shader issue. (2) Lower "cells" value to 48 or 32. This will result in a smaller plane, but it will be faster.

I'm surprised this is slow for you as this ran very well on my machine.
You put the plane code in the Create event right? It's intended to be generated once and drawn using d3d_model_draw in the Draw event.

Reply
Odolwa
4/26/2017 01:16:10 pm

Thanks for responding.

Yes, I put it into the Create event and really all I did was copy/paste everything over from GM1. I did notice that compiling the project takes longer (my computer is not too fancy), so maybe GM2 doesn't work as well for my system? Everything else runs fine, so I'm not sure.

I tried reducing the cell numbers, like you suggested, and that seems to have improved things. However, when I run the shader, everything gets flipped upside down and I don't mean because of the camera rotation; the room, the objects, everything. Even if I restart the game everything is still flipped.

I also noticed that they ditched all the 'd3d_draw_...' functions. Do they go by another name, or are they totally gone? How would we do the earlier parts of your 3D tutorials now?

Xor link
4/27/2017 01:02:01 pm

Hi.
I didn't realize you were using GMS 2, so it must have to do with porting from GM:S 1 to GMS 2. GameMaker Studio 2 exclusively uses vertex buffer instead of d3d_model_function. I won't go too much into detail, but you can create your on model format and then create your models as vertex buffers. There's a great tutorial which explains 3D in GMS 2 here : https://forum.yoyogames.com/index.php?threads/guide-getting-started-with-3d-in-gms2-project-download.12145/#post-79653.

Your camera can be flipped when you set the camera "zup" with the opposite sign (positive to negative or vice-versa) in d3d_set_projection.

Odolwa
4/29/2017 04:06:13 am

Thanks for the link.

As for changing the sign on the zUp argument, it doesn't seem to make any difference. When the game restarts, for a brief moment, everything is upright and then suddenly flips. The flip also seems to mess with some other GUI elements and the moving background I have is showing only 1 panel, rather than a continuous image.

Reply
Xor link
4/29/2017 09:34:51 am

You're welcome.

I have no idea why the zup is flipped then. I suggest asking the GameMaker Community. This isn't really my area of experience.
I hope you can get it resolved!

Reply
Tyson Holt link
6/11/2022 09:32:48 pm

Helllo mate nice blog

Reply



Leave a Reply.

    Tutorials

    New tutorial at GMshaders.com!

    Categories

    All
    3D
    Beginner
    Intermediate

    Archives

    October 2021
    November 2019
    January 2019
    May 2016
    August 2015
    June 2015
    March 2015
    February 2015
    January 2015
    December 2014

    RSS Feed

Powered by Create your own unique website with customizable templates.