gamemaker draw sprite to a 3d surface

3D in GameMaker can exist tricky to set if y'all're merely reading the manual. In this tutorial I'll bear witness you how I apply Surfaces and basic 3D functions to create a background for my shmup fashion game. If you're new to GameMaker, make certain yous have a good understanding of it'due south interface and how to use GML first.

The offset step is to import your textures as a background, making sure the resolution is a power of ii (32, 64, 128, etc…) then tick the "Used for 3D" bank check box.

Cosmos Code

Create an object and requite it a depth of ten,000. Give the object a Create Outcome and add this code:

surf_3d = noone;  w = room_width; h = room_height;  direction = 270; zdirection = 35; 10 = 0; y = 0; z = 512; ratio = w / h; fov = 45;  // Textures layer0 = background_get_texture(bg_floor); layer1 = background_get_texture(bg_wall);        

I'll requite you a run down on what we're doing in this code. surf_3d is going to be the surface. Considering surfaces are volatile we're defining it every bit nil for now as we don't need information technology until the depict issue. due west and h will be the width and tiptop of the surface we're drawing to. If you don't want these to exist the room_width and room_height so set them every bit you wish.

You lot'll already be familiar with direction, x and y equally we utilize them in 2nd but for 3D we're going to need zdirection and z variables. These aren't built in to GameMaker just 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 camera is located.

Ratio is the aspect ratio it'south going to render in. You want the surface width divided past the peak in this variable. I'chiliad using a narrow FOV (field of view)considering of my play field is narrow but you lot may desire to increment it. Supersede the texture names with the names you lot're using. I'll be showing y'all how to do walls and also floors in this tutorial.

Depict Code

Now it's time to give the object we're working in a Draw Upshot with code. This is what I'm using only y'all'll want to work out your ain code below 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, 1 );  draw_set_color(c_white); d3d_start();  // Camera xto = x + lengthdir_x(180, direction) * lengthdir_x(1, zdirection); yto = y + lengthdir_y(180, direction) * lengthdir_x(1, zdirection); zto = z + lengthdir_y(180, zdirection);  d3d_set_projection_ext( x, y, z, xto, yto, zto, 0, 0, 1, fov, ratio, i, 32000 );  // Draw Stuff in 3D d3d_draw_wall( -512, 0, 512, -512, 512*40, 0, layer1, 40, 1 ); d3d_draw_wall( 512, 0, 512, 512, 512*forty, 0, layer1, 40, ane ); d3d_draw_floor( -512, 0, -5, 512, 1024*20, -5, layer0, 1, twenty );  // 3D END d3d_end(); surface_reset_target(); draw_surface( surf_3d, 0, 0);

You guessed information technology, it'due south run down time again.

Surface Code

The first chore for the computer is checking if the surf_3d surface exists and if it doesn't, it creates one with the width and height we've saved in w and h variables. This is why in the create event we gear up the surf_3d variable to equal noone. We need this code 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 then set the surface target to surf_3d then that nosotros're not cartoon to the default application surface. The "draw_clear_alpha( c_black, 1 );" function clears the surface with opaque black. I don't quite sympathise why but if I don't put this in information technology just renders a blackness image.

Camera Setup

Now we're on to the 3D code. "d3d_start();" tells GameMaker we're going to draw in 3D from now until we do "d3d_end();".

While nosotros're using x, y, z to define where the camera is, we need to ascertain some other betoken for the camera to point towards. I'thousand using the variables xto, yto and zto for this. If you know how the "lengthdir_x"and "lengthdir_y" functions work, this isn't as well hard to decode the workings of.

"d3d_set_projection_ext" is the camera. We've got a lot of arguments on this role but most are self explanatory. The starting time three are x, y, z followed past xto, yto, zto so we have xup, yup, zup. The last 3 determine what is "up" relative to the camera. Keeping this as 0, 0, 1 is best unless you want to work out how to roll the photographic camera. Next we have "angle" (FOV), aspect (ratio), almost clipping and finally far clipping altitude. Near clipping is how close something has to be to the photographic camera for it to clip inside and far clipping is draw distance.

Cartoon in 3D

I'll embrace "d3d_draw_wall" and "d3d_draw_floor" here as they are very similar to use. First is 6 position arguments, then the texture to use (we fix it in the create event), horizontal repeat count and vertical echo count. With the x, y andz positions you can imagine yourself looking down on the scene. The ten and y are the same every bit 2D from that perspective and you only add z for how high you want. It's tedious merely really quite simple when yous play effectually with it.

Ending 3D and Drawing the Surface

It's time for "d3d_end();" to terminate the 3D drawing. Nosotros then need to reset the surface target, meaning information technology'll be drawing to the default application surface over again. The terminal line is drawing our image to the application surface. You can change the 10 and y if you have a boarder offset.

Decision

You can play with the x, y, z, direction and zdirection and run across what they all do. If you desire a forwards moving camera, adding to the y and resetting it is useful. For example:

y += xvi; if y > 2048 { y -= 2048; }

This code will move you lot forrard and seamlessly knock the camera back if your background lines upwards only correct (my example code will).

If this was your first time using 3D in GameMaker then I promise y'all've gained a skilful agreement of how to play around with it. GameMaker isn't designed for loftier quality 3D scenes only with some trial and mistake y'all can certainly make dainty backgrounds for 2D games.

If you lot accept any trouble delight post a comment and I'll try to assistance.

Download example GMZ

This Post Has fourteen Comments

    1. John Peachy

  1. KillerGhost91

    Thanks for this useful tutorial continue doing them they're astonishing 😉

    1. John Keen

      Cheers! Many more than to come. 🙂

  2. Darky202

    Thanks for this slap-up tutorial! I tin can see the dedication and try you lot put in these tutorials. I really admire you. I'm deplorable to carp you simply I would actually like to ask you some aid. It's about the spawning of enemies. I've been trying to practice it but every time I endeavor I get something really bad…I don't know what to do to do information technology properly… So umm I was wondering if you could brand a very uncomplicated tutorial (if you don't mind, of course) I would really appreciate it… Well thank you for reading this Bye!

    1. John Keen

      Hi Darky202,
      Cheers! I may be able to do ane on how I spawn enemies in my Shmup if you're interested. What kind of game are you making?

      1. Darky202

        Thanks for replying! Well that'south 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 merely wanted to change that by making a Touhou-like game on GMStudio and export it to a .deb file after finishing it (Maybe information technology sounds a little dumb…) It'southward been a calendar week since I started this project so umm yeah It's kinda serious…Being honest at kickoff I was completely lost with the coding stuff simply I think I'm having a good progress (Now fifty-fifty more because I found your awesome tutorials) so yes that'south it… I think. I'one thousand so happy you answered! Cheers! (Likewise…sorry for my bad English )

  3. Robert

    Hey John, fantastic tutorial! It was really helpful, but 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 you could help me out a flake. So, the trouble is this: https://imgur.com/WCO1vgJ
    The y-axis gets flipped upside down one time 3D is activated, but only for objects and text that are not fatigued on the GUI layer (the buttons at the bottom are fatigued on the GUI layer.) I gauge the reason for that is that gamemaker's y-axis is inverted, and once the y-centrality becomes the z-centrality, the vertical axis isn't inverted anymore, and makes everything appear flipped upside down. Do yous 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. Thank you for your time.

    1. John Bully

      Hi Robert,
      I've been trying to recreate your trouble but haven't had whatever luck in doing so.
      At the end of the article I take a link to the GMZ, does it have the same problem for y'all?
      Let me know how yous go with information technology, I'g curious equally to what the problem is!
      Best of luck,
      John

      1. Robert

        Hey John,
        cheers for your respond! I downloaded the gmz, and when starting the project in GMS one.4, it all worked as in your video. Merely when I imported the project into GMS 2, everything got flipped upside down. Seems like GMS2 is at fault. That's actually a good information, perchance I should try everything y'all did with those new GMS 2 functions.
        Thanks for helping,
        Robert

        1. John Peachy

          I had a good await through my GMS2 3d code (it's quite unlike than GMS1) and plant the "zup" is flipped so y'all need to use -ane.

          https://i.imgur.com/OAbIlt7.png

          I promise this fixes it for you.

  4. okasion

    This is improve than the bodily Game Maker "official" tutorial.
    Thanks a lot.

    1. John Swell

  5. Daniel

    Looks crawly, just is unfortunately outdated. Got anything like for GMS 2.three.three?

suitsweet1988.blogspot.com

Source: https://www.johnkeen.tech/gamemaker-3d-background-surface-basics/

0 Response to "gamemaker draw sprite to a 3d surface"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel