3D Multiple textures on a (really) basic 3D shape - now including a useful tutorial, see last post)

M

mysticjim

Guest
Hello :) I have a rule whereby if I've been fruitlessly searching for a solution for more than a couple of hours, I bite the bullet and ask for help!

Just as a pre-text, this dude has asked the question before;

https://forum.yoyogames.com/index.php?threads/texturing-d3d_draw_block-multiple-textures.18518/

So, I want to draw really basic 3D shapes, we're talking cube and oblongs here, absolutely nothing clever there, and for the most part I have a handle on that. But they're going to be buildings, so I want different textures on each face of the block. I think it sounds exactly like what is being asked above.

3 possible solutions come out of the above thread, and here are my issues with each - I'm hoping some of you clever people can put me on the right track;

1) Externally model and texture, then import into Gamemaker.

One of the posters suggests using a combo of Sketchup and UV Mapper and then converting into d3d. This sounds like a sledgehammer to crack a nut. I really don't like Sketchup and UVMapper looks like something from the dawn of time. These obviously are personal prejudices and probably unreasonable.

I've gotten on slightly better using Model Creator for Gamemaker, but it's interface is a bit odd, it's documentation (I find) counter-intuitive and the only useful tutorial I've found on it is the Youtube series by DragoniteSpam, who - talented as he obviously is, talks too damn fast, skips fleetingly over all the bits I struggle with and he sounds like Kermit the Frog, which obviously annoys me even more. If he'd started by modelling and texturing a cube, then importing it, I'd be sorted, but he went and did a clock tower and the tutorial makes me lose the will to live. Again, these are personal prejudices and probably unreasonable.

2) Modelling and texturing within Gamemaker using

d3d_model_primitive_begin
d3d_model_vertex_texture
d3d_model_primitive_end

As suggested by one of the posters in the above thread. I think I could handle this with a few tangible examples and an idiots guide tutorial. The manual I found difficult, no screenshots and examples that I really couldn't put into context.

Something along the lines of 'this draws you a cube, and this slaps a texture on a specified face of that cube, the size of which you've specifically made to fit the face' would be awesome.

I could only find one video tutorial on this kind of thing, the presenter of which made Dragonitespam look like an Award Winning broadcaster!

3) Define all of the faces individually as walls/floors and texture accordingly. Now this I know how to do, but in the thread above it's described as being a very inefficient way of achieving the result I want. Is this really the case? Sadly, at present using that approach is looking more efficient than the fruitless searching and sitting through the above tutorials!

I appreciate this issue exists mostly due to my own ineptitude - but in my learning of 3D in GM I've breezed through 'here are basic shapes and here's how you colour them in with a single colour and texture' - but the nest logical steps seem to be missing and it jumps straight to, 'now decode the much more complicated bits of the manual on primitives with nothing to guide you' or 'learn to use a bunch of 3rd party software, a lot of which is undocumented.'

Just wondering if anyone can point me in the direction of something that explains / steps through either of the first two solutions above, well, clearly?
 
I'd go with option 2. The primitive pages give you examples on creating models, and the first page you linked will show you how to define each triangle properly. A trianglestrip is what you will want. Using those pages (especially the image on pr_* types), you can simply build a cube by creating two triangles per side, 12 altogether. Start by making a square. Once you get that working, add on another side and see how that goes. The best way to define the cube when viewed as a flat model (and therefore, the texture as well) is with a strip of four sides, with the fifth and sixth above and below the second, like a sideways t.

When it comes to the texture, keep in mind that the left side of a texture is 0, and the right side is 1, so you'll have to scale the UVs accordingly. For example, with two images in a texture, 0-0.5 is the left side and 0.5-1 is the right side. The same applies for the y axis.
 
M

mysticjim

Guest
Thanks @BattleRifle BR55 - very grateful for you input- just a few quick follow up questions;

Firstly, I actually did try out the walls and floor approach whilst waiting for a reply, just as a proof of concept. My project is a really simple topdown game akin to GTA 1 and 2 in the PS1 era using a combination of topdown view of 2D sprite for characters and an overhead view where you see the depth of the basic 3D buildings. I did get the result I wanted and in the small experiment it actually worked very well, was quite easy to put together, with careful coding it's quite easy to create a good variety of different buildings of different heights and widths. Obviously, it requires 5 lines of 3D drawing code (4 walls for sides and a raised floor for the top) and at least 2 textures (sides and top). As it's topdown I don't need to render any sky effects, etc, but I'm planning quite big playing environments, probably 5000x5000 pxls or greater. For what I want, which is essentially buildings that are just window dressing - you don't enter them or anything, they're just solid, stationary objects that look pretty, does the efficiency argument ring true?

It actually sounds a lot less laborious than the whole modelling exercise which I'm still struggling to visualise properly. What does worry me is whether performance would be hit adversely with this approach - especially if I go for atmospheric lighting and stuff. So my second question would be whether modelling and texturing is better from a performance/optimisation point of view? is a single, textured model easier on processing than 5 individually drawn and textured faces using walls and floors?

Finally, assuming I attempt the modelling, to create the first square, that's going to need 2 right angle triangles, slotted together to make the square, right? You mentioned needing 12 triangles, 2 per face for a cube. Can I skip the 2 for the floor being that they will never be visible as they'll be obscured by the top and sides? And you've lost me with the texturing explanation - no disrespect to your explanation, it's my ineptitude again!!!
 
How are you rendering the four walls and the floor, using d3d_draw_* or d3d_model_*? If the former, switch over to the latter as the draw variants are unoptimized.

Yes, two right angle triangles, and you don't need the floor. The single model will be faster, but assuming your viewpoint will be similar to the GTA games in that not much will really be drawn in the view (and you are not drawing stuff outside the view), the performance impact won't be all that grand. If it were me, I'd do a single model, but it's up to whatever is more comfortable for you.

For the UVs, let's think of them like the bounding boxes in a sprite, but instead of in pixels, it goes from a percentage that stays in the 0-1 range instead of 0-100. If you have a square model for a floor and a square texture (256,256) of a section of grass, to draw this full section of grass, the top left UV is 0,0, the top right is 1,0, the bottom right is 1,1, and the bottom left is 0,1. What this means is that you are telling the texture that for the top left, you want it at the very start, which is 0,0,. For the bottom right corner, you want it to expand all the way to the bottom right corner of the texture, which takes it to 1,1. Now, imagine this texture has been doubled in width (512x256), so it has a square grass texture and a square asphalt texture. Using the same square model, if you were to use the same UVs, the entire texture would be drawn on it, but because it is a longer texture, you'll get a squished image of both the grass and asphalt. Let's say we want to remedy this so that now only the asphalt draws. The asphalt is on the right side of the texture, so the left side of it starts in the middle of the whole texture. The halfway point between 0 and 1 is 0.5, so now the top left and bottom left vertices need their UV arguments updated in order to reflect this. For the right side of the model, because the asphalt is all the way on the right of the texture, we can leave the 1,0 and 1,1, but the left side needs to start in the middle, so the top left becomes 0.5,0 and the bottom becomes 0.5,1.

 
M

mysticjim

Guest
Apologies, I'm with you on the theory, and I like the pictures, but if I were to say - based on that explanation I had the first idea how to draw and texture the model, I'd be lying!

The manual gives an example in terms of creating and texturing a primitive model;

http://docs.yoyogames.com/index.htm...ng 3d/3d models/d3d_model_vertex_texture.html

Code:
model = d3d_model_create(); d3d_model_primitive_begin(model, pr_trianglestrip); 
d3d_model_vertex_texture(model, 0, 480, 0, 0, 0); // - ***are these last two numbers the ones you're referring to above?
d3d_model_vertex_texture(model, 640, 480, 0, 1, 0);
d3d_model_vertex_texture(model, 640, 480, 1000, 1, 1);
d3d_model_vertex_texture(model, 0, 480, 1000, 0, 1);
d3d_model_primitive_end(model);

The above code will draw a 4 vertex triangle strip textured with the texture held in the "tex" variable.
The manual also references that the "tex" variable denotes the texture, yet the code itself makes no reference to the tex variable or where it goes in the code.

Apologies, totally not your fault, but I think I'm slightly more confused than when I started.
 
Yes, the last two numbers. Maybe once you start playing around with this example it will all make sense. It's a lot simpler than it seems!

The reference to the tex variable is an oversight from the d3d_vertex_texture() page, since the versions without _model_ draw the primitive directly instead of storing it in a variable to use in d3d_model_draw() (tex would go in d3d_primitive_begin_texture(type,texture)).

Take this edited version from the draw_vertex_texture() page and play around with it in a new project with some textures of your choice. Take note that the only difference between the d3d_ and draw_ variants is the lack of a z axis, which will make this example a little easier to look at.

Code:
var tex = background_get_texture(back);
draw_primitive_begin_texture(pr_trianglelist, tex);
draw_vertex_texture(  0, 480, 0, 0);
draw_vertex_texture(640, 480, 1, 0);
draw_vertex_texture(640, 480, 1, 1);
draw_vertex_texture(  0, 480, 0,  1);
draw_primitive_end();
 
M

mysticjim

Guest
Thanks dude, that I can get my head around :) I'm really sorry if I'm being slow here!

I'll give it a go.

Be warned, if I manage this then it's only a matter of time before I ask about lighting and shadows! Been having trouble with that too!!!!
 
M

mysticjim

Guest
Okay, bit of an update on this. Naturally I failed miserably at coding and texturing a 3D primitive, sorry @BattleRifle BR55 - I'm useless! But I did actually find an incredibly useful and very good beginners tutorial on models, texturing and importing into GM. Being that good beginners 3D tutorials are rarer than verifiable sightings of the Loch Less Monster I can't believe this isn't more widely recommended and appreciated;

http://gmc.yoyogames.com/index.php?showtopic=650003&hl=model

I guess this is because it's archived in the old forums - everyone has either forgotten about it or didn't know of it's existence in the first place!. It took me hours to find it, and it was more by luck than judgement, but it's awesome and has helped me massively - I was so glad to find it I felt the need to share it. No idea what happened to the originator, they're not listed as a member of the current forum.

And, despite my earlier reservations about the whole 3D modelling/UV Mapping stuff, between the tools available and the guidance given in the tutorial I'd say it was essential for any 3D newbies like me.

Very big karma to @icuurd12b42 - who I'm in complete awe of with regards to GMmodelfix. I don't like the sliding menus or the tiny icons, but that's purely cosmetic - from a functionality point of view it's truly amazing - I honestly think if Yo-Yo Games were ever to get serious about GMS's 3D potential then I think they could do a lot worse including it in a dedicated suite of 3D orientated tools built into GM UI. That alone would probably make me buy GMS2 instantly.

And I definitely take back my comments about UVMapper Classic, it's actually very good at what it does.

Just a few of final things - the tutorial advises using Blender to make the initial model - I actually made mine using a quick and dirty browser-based 3D modeller called Vectary;

https://www.vectary.com/engine/

Which is free for non-commercial use and exports to obj format. I found it easier and more intuitive than GM Model Creator, which I've never had any luck with! Through my research I came across an old alternative to GM Model Creator called SivModeler - which is quite old and not really supported. Does anyone here use it? It exports directly to d3d format and looks pretty easy to use, but haven't tested it yet. Is it any good?

One thing I found in GMmodelfix is that my model loaded in smaller than the scale I created it and had to be re-scaled back to it's original dimensions. I've no idea why this happened, probably something I did, but I didn't notice first time, so my first attempt at importing in GMS produced laughable results where my intended soaring tower blocks ending up the size of cereal packets!

Anyway, to sum up, what this exercise has further highlighted to me is that if you give me a decent tutorial, aimed at the very basics, something tangible but simple, then I can work with that, pull it apart, learn constructively from it and move forward. I'm actually considering adapting the tutorial for Youtube to maybe help others in my position because I'd have found it a lot easier and would have saved a lot time and effort to get the result I was looking for.

Hope some other peeps find this useful :) I'm off to model a 3D city!
 
Top