# Are there established conventions for naming “directions” (width,height) in 3D?

I want to create a 3D game world where the players can modify the environment. I would like to bring it to the point where a power-user could even create "game objects" inside the application using some kind of very simple 3D modeling UI. So I decided to write this UI first, as I can then use it to create the game content. I spent some time in the last two months trying to learn Blender, and decided that this was a total overkill for what I want to create.

Anyway, I have the classes representing the 3D "concepts" mostly worked out, but I have problems with the "unwrapping" code, that is, mapping the graphic primitives, triangles and rectangles, to a texture file.

The 3 points representing a triangle are in 3D, but I need to work in 2D for the texture mapping. How should I best choose which of the 3 points to use as the "origin" of that triangle in the texture file, and how do I define some "reliable" concept of width and height that make sense, when I am in 3D?

I can think of several methods, but if one is most commonly used, I would like to know, so that my code also make sense to other people.

[EDIT] It seems I did a very bad job of explaining what I want. I understand how unwrapping works. I sweated blood going through a Blender unwrapping tutorial. But now I'm trying to write code that can automatically create a basic unwrapping of simple objects (boxes, wedges, pyramids, ... and composition thereof). My idea was to group primitives that are 1) in the same plane, and 2) touch each other in (flat) "boxes" and map those boxes to the first free spot on the texture file that is big enough (under the assumption that all primitives have the same scale). Far from optimal, but good enough to get going. Later, the user could have refined the unwrapping as necessary, for example, by mapping several "boxes" to the same coordinates, if they look the same. The box would have a width and a height, and the unwrapping would map the "origin" of the box (say top left corner) to the coordinate in the texture file where it should be mapped.

But, for me, width is the length is along the X axis, and height is the length along the Y axis. So how do I decide what is the width and the length when the box is in the X-Z plane, instead of the X-Y plane, for example? And which vertices is the "top left" when all vertices have the same "Y" or "X" coordinate (but vary in Z)? Unlike the real world, there is no "obvious" "up and down", "left and right" ... in computer 3D. Anything can be in any direction. Things don't "fall down" because there is no down.

Hopefully I now made clear my problem. How can I consistently define "top", "left", "width", and "height" when I am in 3D, instead of in 2D, and when a primitive might, for example look like an horizontal line from user point of view because it stretches in Z (which traditionally "comes out of the monitor") instead of Y?

My code can only make sense if I can define all concepts clearly, and unequivocally. I first need a way of defining those concepts that always apply, for any 3 points in 3D space. Only then can I look at any piece of my code, and decide whether it is correct or not. What I cannot define clearly, I cannot program correctly.

What I really want to know is what I asked in the title. I only talked about unwrapping to explain why I need to know this.

What you speak of is called "unwrapping". This is the process of generating UV coordinates for your mesh, so that it will allow UV mapping.

UV coordinates are in the range of 0..1, where 0, 0 is the top left of the texture and 1, 1 is the bottom right of the texture.

The process of unwrapping a 3D mesh to a 2D surface is something tricky. Imagine a cube. In order to unwrap it, you would have to cut/separate some of the edges, as shown in the following image (from Wikipedia):

Finding these cuts (seams) is something that is usually done by artists, as there's no algorithm that creates good cuts for every type of mesh.

The most primitive approach of unwrapping would be to simply project every triangle to 2D. You can even have overlapping UV coordinates, so every triangle could have UV coordinates of (0, 0), (1, 1), (0, 1). That would look horrible though... a slightly better approach would be to have coordinates that correspond to the triangle size. But that will still result in visible texture seams at the edge of every triangle.

If you just have basic primitives like a triangle or rectangle, then generating the UV coordinates should be trivial. You can project the primitive to 2d (by looking from the surface-normal) and calculate a (2d) bounding-box of the vertex coordinates. The longer side of the bounding box will map to 0..1. Then every vertex will get it's UV coordinate proportionally to the bounding box size.

• Thanks! "0, 0 is the top left of the texture and 1, 1 is the bottom right of the texture" answers half of my question. I thought of using integral coordinates matching the pixel position in the texture, but I now understand why (0,0)-(1,1) makes more sense; you can change the texture resolution without changing the UV mapping! Oh, and the image is very useful. – Sebastien Diot Dec 23 '11 at 18:24
• @SebastienDiot Exactly, normalizing the texture dimensions to 0..1 allows for differently sized textures with the same set of UV coordinates. – bummzack Dec 23 '11 at 20:25
• For general-purpose UV unwrapping useful for arbitrary meshes when you want to paint those meshes in 3D, I'd suggest reading "Geometry Images" by Hoppe, Gortler & Gu. – Martin Sojka Dec 23 '11 at 20:45
• @Martin Very interesting! Reading the introduction, I realized that for what I want to do, using voxels might be a realistic option (although that's not what the document is really about). – Sebastien Diot Dec 24 '11 at 11:12