game maker 3d draw distance
3D in GameMaker can be catchy to set up if you're simply reading the transmission. In this tutorial I'll bear witness you how I use Surfaces and basic 3D functions to create a groundwork for my shmup style game. If you're new to GameMaker, brand sure yous take a expert understanding of it'southward interface and how to use GML first.
The start footstep is to import your textures equally a background, making sure the resolution is a power of two (32, 64, 128, etc…) then tick the "Used for 3D" check box.
Creation Code
Create an object and give information technology a depth of x,000. Give the object a Create Outcome and add this code:
surf_3d = noone; due west = room_width; h = room_height; direction = 270; zdirection = 35; x = 0; y = 0; z = 512; ratio = westward / h; fov = 45; // Textures layer0 = background_get_texture(bg_floor); layer1 = background_get_texture(bg_wall);
I'll requite you a run downward on what nosotros're doing in this lawmaking. surf_3d is going to be the surface. Considering surfaces are volatile nosotros're defining it equally zilch for at present every bit we don't need it until the draw event. westward and h volition be the width and height of the surface we're drawing to. If y'all don't desire these to be the room_width and room_height then set them every bit you wish.
You lot'll already be familiar with direction, x and y as nosotros utilize them in 2nd but for 3D we're going to need zdirection and z variables. These aren't built in to GameMaker but all that means is we need to define them. Management acts as the cameras Yaw and zdirection acts as the Pitch. 10, y and z define the point in 3D space that the photographic camera is located.
Ratio is the aspect ratio it'southward going to return in. Y'all want the surface width divided by the acme in this variable. I'm using a narrow FOV (field of view)because of my play field is narrow but you may want to increment it. Replace the texture names with the names you lot're using. I'll exist showing yous how to do walls and besides floors in this tutorial.
Draw Code
Now it's time to give the object we're working in a Draw Consequence with code. This is what I'm using but you'll want to piece of work out your own lawmaking beneath the comment "Draw Stuff in 3D".
if !surface_exists(surf_3d) { surf_3d = surface_create( w, h ); } surface_set_target(surf_3d); draw_clear_alpha( c_black, i ); draw_set_color(c_white); d3d_start(); // Photographic camera xto = x + lengthdir_x(180, direction) * lengthdir_x(1, zdirection); yto = y + lengthdir_y(180, management) * lengthdir_x(1, zdirection); zto = z + lengthdir_y(180, zdirection); d3d_set_projection_ext( x, y, z, xto, yto, zto, 0, 0, i, fov, ratio, 1, 32000 ); // Draw Stuff in 3D d3d_draw_wall( -512, 0, 512, -512, 512*40, 0, layer1, forty, 1 ); d3d_draw_wall( 512, 0, 512, 512, 512*40, 0, layer1, 40, 1 ); d3d_draw_floor( -512, 0, -5, 512, 1024*20, -5, layer0, 1, 20 ); // 3D END d3d_end(); surface_reset_target(); draw_surface( surf_3d, 0, 0);
You guessed it, it's run down time once more.
Surface Code
The first task for the estimator is checking if the surf_3d surface exists and if it doesn't, it creates 1 with the width and top we've saved in w and h variables. This is why in the create upshot we ready the surf_3d variable to equal noone. We need this lawmaking because surfaces are volatile and actions such equally minimize the game will destroy the surface, causing a crash if the surface isn't recreated. We so set the surface target to surf_3d and so that nosotros're not cartoon to the default application surface. The "draw_clear_alpha( c_black, 1 );" office clears the surface with opaque blackness. I don't quite sympathise why but if I don't put this in information technology simply renders a black image.
Photographic camera Setup
At present we're on to the 3D code. "d3d_start();" tells GameMaker nosotros're going to draw in 3D from at present until we do "d3d_end();".
While nosotros're using x, y, z to define where the photographic camera is, we demand to define another point for the camera to point towards. I'k using the variables xto, yto and zto for this. If you know how the "lengthdir_x"and "lengthdir_y" functions work, this isn't too hard to decode the workings of.
"d3d_set_projection_ext" is the camera. We've got a lot of arguments on this function but about are self explanatory. The commencement 3 are x, y, z followed by xto, yto, zto and and then we have xup, yup, zup. The last three decide what is "upward" relative to the photographic camera. Keeping this equally 0, 0, 1 is best unless you want to piece of work out how to scroll the camera. Next we have "bending" (FOV), aspect (ratio), near clipping and finally far clipping distance. Near clipping is how shut something has to be to the camera for it to prune within and far clipping is draw distance.
Drawing in 3D
I'll encompass "d3d_draw_wall" and "d3d_draw_floor" here as they are very similar to apply. First is 6 position arguments, then the texture to employ (we set information technology in the create result), horizontal repeat count and vertical repeat count. With the x, y andz positions you tin imagine yourself looking downwardly on the scene. The ten and y are the same equally 2D from that perspective and you merely add together z for how loftier you lot want. It'due south slow only really quite simple when you lot play around with it.
Ending 3D and Drawing the Surface
It's time for "d3d_end();" to stop the 3D cartoon. We then need to reset the surface target, meaning information technology'll be cartoon to the default application surface again. The last line is cartoon our image to the application surface. You tin can alter the ten and y if yous have a boarder commencement.
Determination
You lot tin play with the x, y, z, management and zdirection and come across what they all do. If you want a forward moving photographic camera, adding to the y and resetting it is useful. For example:
y += xvi; if y > 2048 { y -= 2048; }
This code volition move you forward and seamlessly knock the photographic camera back if your groundwork lines up just right (my case lawmaking will).
If this was your first time using 3D in GameMaker then I hope you've gained a good understanding of how to play around with it. GameMaker isn't designed for loftier quality 3D scenes but with some trial and error you can certainly make dainty backgrounds for 2D games.
If you have whatever problem please mail service a annotate and I'll attempt to help.
Download instance GMZ
Source: https://www.johnkeen.tech/gamemaker-3d-background-surface-basics/
This Mail service Has xiv Comments
John Keen 28 Feb 2017 Reply
KillerGhost91 28 Feb 2017 Reply
Thanks for this useful tutorial keep doing them they're amazing 😉
John Keen 28 Feb 2017 Reply
Thank y'all! Many more to come. 🙂
Darky202 11 Mar 2017 Reply
Cheers for this great tutorial! I tin can come across the dedication and effort you put in these tutorials. I really adore y'all. I'thousand sorry to bother you lot but I would really like to ask yous some assist. It'south about the spawning of enemies. I've been trying to exercise it but every time I effort I get something really bad…I don't know what to exercise to do it properly… So umm I was wondering if yous could brand a very simple tutorial (if you don't listen, of grade) I would actually appreciate information technology… Well give thanks you for reading this Goodbye!
John Keen 12 Mar 2017 Answer
Hullo Darky202,
Thank y'all! I may be able to do one on how I spawn enemies in my Shmup if you're interested. What kind of game are you making?
Darky202 13 Mar 2017 Reply
Thanks for replying! Well that's a very funny story… I noticed that in that location are no many alternatives to play Touhou in Linux than Wine or a VM…I only wanted to modify that by making a Touhou-like game on GMStudio and export information technology to a .deb file after finishing it (Maybe it sounds a niggling impaired…) It's been a week since I started this project and so umm yep It'due south kinda serious…Being honest at kickoff I was completely lost with the coding stuff just I think I'one thousand having a adept progress (Now even more because I establish your awesome tutorials) and then yeah that's it… I remember. I'm so happy you answered! Thank you! (Also…deplorable for my bad English )
Robert 2 May 2018 Reply
Hey John, fantastic tutorial! It was really helpful, merely in my game, it creates a certain problem for some reason. I've spent days on trying to fix that and I'd be actually glad if y'all could help me out a bit. So, the trouble is this: https://imgur.com/WCO1vgJ
The y-centrality gets flipped upside down in one case 3D is activated, but only for objects and text that are not fatigued on the GUI layer (the buttons at the bottom are drawn on the GUI layer.) I guess the reason for that is that gamemaker'southward y-axis is inverted, and once the y-axis becomes the z-axis, the vertical centrality isn't inverted anymore, and makes everything appear flipped upside down. Do you lot know where or how to change that? Leaving out the d3d_set_projection_ext function fixes the issue, but basically also deactivates 3d, so it makes the object useless. Thanks for your time.
John Keen 2 May 2018 Reply
How-do-you-do Robert,
I've been trying to recreate your problem but oasis't had any luck in doing so.
At the finish of the article I have a link to the GMZ, does it have the same problem for you?
Allow me know how you go with it, I'yard curious equally to what the problem is!
Best of luck,
John
Robert 21 May 2018 Reply
Hey John,
thank you for your answer! I downloaded the gmz, and when starting the project in GMS 1.iv, it all worked as in your video. Merely when I imported the project into GMS 2, everything got flipped upside down. Seems similar GMS2 is at fault. That'south really a good data, maybe I should try everything y'all did with those new GMS 2 functions.
Thanks for helping,
Robert
John Keen 24 May 2018 Reply
I had a good look through my GMS2 3d code (it's quite different than GMS1) and institute the "zup" is flipped so you need to use -1.
https://i.imgur.com/OAbIlt7.png
I hope this fixes it for y'all.
okasion 27 Nov 2019 Reply
This is improve than the actual Game Maker "official" tutorial.
Thanks a lot.
John Not bad 27 November 2019 Respond
Daniel vii Aug 2021 Respond
Looks awesome, but is unfortunately outdated. Got anything similar for GMS 2.3.three?