- Home /
How to calculate UVs for a mesh created in runtime based on Vertex position
So I'm creating a 3d first person shooter game based on an older 2d game called Liero. The central element to the game is that both players are underground, and must blast their way through dirt to reach each other.
I've achieved this effect through the use of voxels, but I'm stumped as to how to shade them. I've looked up a lot of different techniques for mapping uvs, such as Tri-planar, but either I haven't understood them or they haven't worked (maybe both).
Currently in my implementation I calculate out the vertices for my mesh and then assign some UV value to that vertice which sticks with it. Note that I don't neccesarily have 3 vertices per triangle, as multiple triangles may share a single vertice (and be facing different directions).
I don't need it to be super precise, and in fact I like the somewhat random look I get when the UVs aren't super precise. However, as can be seen in the screen shot, when I have certain angles I get a sort of stretched look which is very unnatural looking. I believe this is caused by the UVs of two vertices being identical or near identical. So my question is really thus. Is there some good way of converting 3d vertices into 2d uvs in a way that doesn't produce adjacent symmetries or stretch out the UV unnaturally?
Currently my code for calculating the UV (as seen in the picture) is:
private Vector2 calculateUV(Vector3 position)
{
float greatest;
float middle;
float smallest;
position = new Vector3 ((position.x)*15f/17f+ voxelRadius/7f*5f, (position.y)*13f/17f + voxelRadius/7f*3f, (position.z)*11f/17f + voxelRadius/7f);
if (position.x > position.y) {
if (position.x > position.z) {
greatest = position.x;
if (position.y > position.z) {
middle = position.y;
smallest = position.z;
} else {
middle = position.z;
smallest = position.y;
}
} else {
greatest = position.z;
middle = position.x;
smallest = position.y;
}
} else if (position.x > position.z) {
greatest = position.y;
middle = position.x;
smallest = position.z;
} else {
if (position.y > position.z) {
greatest = position.y;
middle = position.z;
smallest = position.x;
} else {
greatest = position.z;
middle = position.y;
smallest = position.x;
}
}
return new Vector2 (greatest/25+smallest/25, middle/25+smallest/25);
}
Yes, the code is pretty random, and was made up on the fly in order to try and remove as much stretching as possible (and it has some) Thanks in advance!!
[1]: /storage/temp/78055-dirt.png
Update: I've played with my code a bit and have discovered two potential solutions. One produces a good mapping except at the "seams" as I've labeled them (see picture) and the other also produces a good mapping but is stretched out quite badly. I'm still interested if anyone has better solutions. return new Vector2((position.x%25)(3/5)/25+(position.y%25)(2/5)/25,(position.z%25)(3/5)/25+(position.y%25)(2/25)/25); And the second code return new Vector2((position.x%200)*5/(200*9)+(position.y%200)*4/(200*9),(position.z%200)*5/(200*9)+(position.y%200)*4/(200*9)); 200 is the width (and height and depth) of my arena. !
Your answer
Follow this Question
Related Questions
How can i achieve texturing of a dynamic generated Hex? 0 Answers
Merging faces without effecting texture mapping 2 Answers
Changing Texture of one side of cube when raycast is colliding with it. 1 Answer
How do I assign different parts of model to different materials during runtime? 1 Answer
Voxel terrain wont appear 1 Answer