- Home /
remapping cube UVs
if I make a cube 10 times smaller, and so I want to make the UV remap also 10 times smaller, I remap all the ones on the cube to 0.1, except when I do it it looks like it's and one colour / one pixel?
ns = 10;
var mesh : Mesh = cube.GetComponent(MeshFilter).mesh;//change the square so that the UVs map okay
var vertices : Vector3[] = mesh.vertices;
var uvs : Vector2[] = new Vector2[vertices.Length];
for (var i = 0 ; i < uvs.Length; i++){
if ( uvs[i].x == 1 ){ uvs[i].x = 1/ns ;}
if ( uvs[i].y == 1 ){ uvs[i].y = 1/ns ;}
}
mesh.uv = uvs;
any ideas? I've added a picture, as you can see, the UVs are 1/10th of the texture but the texture is only one pixel of colour. Am I missing something obvious?
Answer by Bunny83 · Dec 20, 2012 at 08:15 PM
Yes, you're missing that an integer divided by an integer will result in an integer. 1 / 10 == 0
Try this:
for (var i = 0 ; i < uvs.Length; i++)
{
if ( uvs[i].x == 1 ){ uvs[i].x = 1.0/ns; }
if ( uvs[i].y == 1 ){ uvs[i].y = 1.0/ns; }
}
is there a way to make the UV length equals to the edge of the box? for example if the edge is 0.5, to set the UV also at 0, 0.5?
I'm not sure what you mean. What do you mean by UV length? UV coordinates are always between 0 and 1. They can be greater or lower, but in this case the texture's wrapmode needs to be set to repeat so the texture would tile across the mesh.
The UV mapping is usually done in a modelling application and not in Untiy. You need to be more specific what you want to do and how the UVs look at the moment.
sorry I meant is there a way to find the length in between 2 related UVs in space and remap the 1 figure according to that length? for example to keep the same material on lots of irregular shape boxes.Thank you!:)
Your last comment makes it even more unclear :)
The UV coordinates of a vertex are relative to the texture space of the mesh. A texture space is a 2d space and it covers the texture. Those coordinates are not related to world or local space coordinates of the mesh. Every mesh has it's own texture space.
You might want to use a world coordinates to texture space mapping. In this case you have to define two world space axis which can be used to project the 3d vertex position into 2d texture coordinates.
I'm not explaining that in detail here (I'm still writing on a tablet), but here's a link to the Half-life's map format which uses a similar method.
$$anonymous$$aybe I will post an example when I'm back home (this might take some days :) )
Thanks! i figured it out now. the UV's arrays each correspond to a vertex array, i.e. vector2 mesh.uv[1] = vector 3 mesh.vertices[1] so i can find the spacial position from the vertex.transform position and then i can see which axis and distance between 2 vertex arrays and if they are on the axis line x y z they are on the same edge of a cube so i can measure the distance and resize the UVs. i was confused because i couldnt figure it at first. thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613085615im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Apply texture to a bar (cube) 2 Answers
How to add my texture to a cube? 2 Answers
How do UVs work? 1 Answer
Coin shine 1 Answer
Is there a way to wrap a texture around a side of a cube? 1 Answer