So today we're gonna make a cube and we're gonna take this sort of slightly roundabout way to get there, but in doing so, we'll learn everything we need to know to make custom and parametric geometry of your own.
But before that, there is a little bit of boilerplate. So I know this code is probably too small to see. I'll have a link to a GitHub repo at the end, but what I wanna impress upon you is this is all that you need to set up a scene in Three.js. There's only about 30 lines here. It's all very straightforward.
So having gotten that out of the way, let's talk about geometry. So geometry can contain a lot of data, but the two most important types are vertices, so points that are positioned in 3D space, and then faces, surfaces that link those points together. There's a lot of other stuff that we won't have time to talk about today, but with vertices and faces we can make geometry.
So here's a cube. Let's take away its skin, and we'll see that we have eight points. So let's start by giving those points an order. So we'll just give them some numbers, zero to seven, to lay them out in some order. And then once we've done that, let's specify where they are in space. So to keep it simple, we're just using negative ones and ones, that'll give us a two unit cube centered at the origin, which is great. So having done all that, we can specify our vertex data. So to do that, I'm just gonna set up an array and I'm gonna push numbers into it for the X, Y, and Z coordinates of each vertex. Note that this is a one dimensional array. I'm not pushing a full set of a triplet at a time, and this just ends up being one dimensional, which is important later.
So that's our vertex data. Now we're ready to set up our faces. So faces are specified by giving a sequence of vertices in order. So what we do is instead of having actual data, we just reference the data that already exists in the vertex list. So the way the order in which we specify the vertices matters a lot. So in 3D graphics, a big part of getting things to be performant is to not draw things you don't need to see. And one of the ways that we do that is by only drawing one side of faces by default. So faces only really exist from one direction. And the way we specify which direction that is is the order in which we specify the vertices. So it's important that we specify our vertices in counter-clockwise order. So this blue triangle, for example, involves the vertices zero, four, and three.
Comments