[GUIDE] Getting Started with 3D in GMS2 (Project Download) (2024)

To begin with, the following code will all go in the create event of some kind of control object, unless I specify otherwise.

Okay, before we even get started, we need to enable some things.
In 3D, we have something called the "z-buffer" - this basically enables the sorting of draw order, so things that are further away appear to be behind things that are closer (Note that this behaviour CAN be changed in GMS2, . If we don't enable the z-buffer, then rendering will look very odd, as things far away may draw on top of things that are closer (even within a single model!)
To enable the z-buffer, we just use this code:

Code:

gpu_set_zwriteenable(true);//Enables writing to the z-buffergpu_set_ztestenable(true);//Enables the depth testing, so far away things are drawn beind closer things

You may also find it useful to force layers to draw at a depth of 0 (this will stop layers translating the z-coordinate of what you draw on any layer - though you may find the default behaviour preferable in some cases)

(documentation for layer_force_draw_depth)

Believe it or not, that's all the required set up needed to get rendering in 3D!

Now we can move onto the next step - setting up and using a camera in a 3D environment.
Since the GMS2 update, views have been totally replaced with cameras, but so has the d3d projection functions - they are now the same thing, which is far more convenient when you get used to them.
The first thing we'll want to do is create a camera, assign it to a view, as well as enable views and make yours visible. You can still create views in the room editor, but sometimes it's nice to have all the code in front of you.
Technically speaking, you don't even need a view to set up a 3D projection, however, it makes managing the camera much easier.
Note that, as previously, the game window size will default to the view port/room size of the first room. For this reason, you should have a "set-up room", to get things ready and set the size to be appropriate. Otherwise, you could use the window_* functions combined with view_set_Xport to get the size you want.

Anyway, the code!
First, getting our view to actually appear (you don't need to do this if you have set up a view in the room editor)

Code:

//First, we need to enable views and make our view visible (in this case, view 0)view_enabled = true;//Enable the use of viewsview_set_visible(0, true);//Make this view visible

Next, we need to create a camera, and make it fully functional. In order to do this, we must create the camera, assign a projection matrix to it and bind it to the camera. We will keep the camera variable, though we will be using "view_camera[0]" to reference it later. We keep the camera variable in case we want to bind the camera to another view later, perhaps in another room, without re-creating it.

Code:

//First, create the camera. We could use camera_create_view, but that is more useful in a 2D environmentcamera = camera_create();//Then, we need to build a projection matrix. I keep this in instance scope in case I need to reassign it later. (Though you can retrieve matrices from a camera with camera_get functions//I use matrix_build_projection_perspective_fov, as it gives the most control over how your projections looks.//Here's how I use the arguments: I give a 60 degree vertical field of view, with a ratio of view_wport/view_hport, with a 32 unit near clipping plane, and a 32000 far clipping plane. Some of these values may need tweaking to your liking.projMat = matrix_build_projection_perspective_fov(-60, -view_get_wport(0)/view_get_hport(0), 32, 32000);//Now we assign the projection matrix to the cameracamera_set_proj_mat(camera, projMat);//Finally, we bind the camera to the viewview_set_camera(0, camera);

NOTE: In GMS2, setting the projection differs from 1.4 - by default, it now does a weird conversion to right-handed coordinate space to stay consistent with 2D, but this makes the up-vector act weird. To make the projection act like 1.4 and before and use left-handed space and make the up-vector act as expected, you must make the fov and ratio values negative, hence the code above.
You may notice that we have assigned a projection matrix, but didn't actually point a camera at anything. This happens later, and separately - this is one of the benefits of the new camera system - we only have to build the projection once, hence saving a lot of time compared to calling d3d_set_projection_ext every draw frame, as half the math is already done.
If you want your camera to have an orthographic projection (instead of perspective), replace the matrix build with this function:

Code:

matrix_build_projection_ortho(view width, view height, znear, zfar);

There is one more thing you need to do to get your camera ready! Note that we still haven't assigned a lookat matrix to the camera! This is used to actually point a camera from a location, to a location.
Now, you could update this matrix in step or draw, but cameras come with a new, cool function - "camera_set_update_script()" (there are begin_script and end_script functions too! Check the manual to see more about them!).
Basically, this script is called every draw update per view, assuming the camera is bound to the view. This means you can enable frame skipping, or disable a view, and a camera won't waste time updating what it doesn't need to!
So, to use a camera update script, you must create a script. Ideally, the contents of these scripts should be able to be self contained, and the only external references should be globals or instance variables that you know exist, otherwise the game will probably crash.
For this demo, I just created a camera that would spin around the center of the room, based on time. The script is named "camera_update_script", and contains the code:

Code:

//Set up camera locationvar zz = -640;var xx = lengthdir_x(720,-current_time/10) + (room_width*0.5);//Rotation is negative now to match with the old gif and spin clockwisevar yy = lengthdir_y(720,-current_time/10) + (room_height*0.5);//Build a matrix that looks from the camera location above, to the room center. The up vector points to -zmLookat = matrix_build_lookat(xx,yy,zz, (room_width*0.5),(room_height*0.5),0, 0,0,-1);//Assign the matrix to the camera. This updates were the camera is looking from, without having to unnecessarily update the projection.camera_set_view_mat(view_camera[0], mLookat);

Note that we now reference view_camera[0] now. I have done this because it is the only proper way to guarantee that we are targeting the camera that belongs to view0.
As another point of interest, you could set up a test so that the camera only updates the lookat matrix if it has moved or changed orientation, which can save even more processing time.

Okay, the last thing you need to do is assign the update script to the camera. Back in the create event (where your other code should be), just add this:

Code:

//Assigns the update script named "camera_update_script" to the camera belonging to view0camera_set_update_script(view_camera[0], camera_update_script);

An that is it! If you set up this camera in a simple, tiled room, it should look something like this:

[GUIDE] Getting Started with 3D in GMS2 (Project Download) (1)

Aiming and moving your camera around via user input is a little more work and more project-specific (e.g. 3rd person will have different controls to first person, and a flight sim would be different again), but as long as you have a sound understanding of algebra and trigonometry, it should be pretty straightforward.

[GUIDE] Getting Started with 3D in GMS2 (Project Download) (2024)
Top Articles
Abingdon Avon Skyward
The Ultimate Guide to Bass Tracker Boats - BassboatZone
Fiskars X27 Kloofbijl - 92 cm | bol
Public Opinion Obituaries Chambersburg Pa
Craftsman M230 Lawn Mower Oil Change
The Definitive Great Buildings Guide - Forge Of Empires Tips
East Cocalico Police Department
A Complete Guide To Major Scales
Owatc Canvas
Lenscrafters Westchester Mall
J Prince Steps Over Takeoff
Max 80 Orl
Enderal:Ausrüstung – Sureai
Https E24 Ultipro Com
Cnnfn.com Markets
Guidewheel lands $9M Series A-1 for SaaS that boosts manufacturing and trims carbon emissions | TechCrunch
Puretalkusa.com/Amac
Nesz_R Tanjiro
Www Craigslist Milwaukee Wi
Vanessawest.tripod.com Bundy
Accuweather Mold Count
V-Pay: Sicherheit, Kosten und Alternativen - BankingGeek
A Biomass Pyramid Of An Ecosystem Is Shown.Tertiary ConsumersSecondary ConsumersPrimary ConsumersProducersWhich
Understanding Genetics
Evil Dead Rise Showtimes Near Pelican Cinemas
R&S Auto Lockridge Iowa
Drift Hunters - Play Unblocked Game Online
1145 Barnett Drive
Yale College Confidential 2027
Www.1Tamilmv.con
Math Minor Umn
Kattis-Solutions
Mg Char Grill
8005607994
Hebrew Bible: Torah, Prophets and Writings | My Jewish Learning
Boone County Sheriff 700 Report
Ksu Sturgis Library
One Main Branch Locator
Directions To Advance Auto
Man Stuff Idaho
Weekly Math Review Q2 7 Answer Key
Pekin Soccer Tournament
Sofia Franklyn Leaks
Jaefeetz
Conan Exiles Tiger Cub Best Food
8 4 Study Guide And Intervention Trigonometry
Rubmaps H
Raley Scrubs - Midtown
Grace Charis Shagmag
The Significance Of The Haitian Revolution Was That It Weegy
Vrca File Converter
Ff14 Palebloom Kudzu Cloth
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 6317

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.