- Home /
Mesh UV min/max Y position
Hey there.
Does anyone know when you loop through a mesh, how to know when uv.y is on its bottom position (mesh.uv[i].y == lowest possible position) and when it reached his top position? (mesh.uv[i].y == maximum possible position)?
i need to paint the vertex color from bottom top like this:
vcol = Color.lerp(Color.clear,Color.red,(“uv y currentposition” / “uv y max position”));
sorry to tag you, but i’ve allready read severals answers from you regarding mesh and loops
thanks dan
EDIT 1:
Ok, if you look at the picture below, you will see following..
Bark / Stem on Submesh 0 and Leafs, which are all in Submesh 1
Vertex Color view of the Tree. Colors near the Bark are the same as the Bark (1,0,0). Colors at the end if the Leaf are (1,1,1)
UV is a propper box (Size (1,1))
So if i now procedurally change the Meshes Vertex Color, i'm getting not Correct results, with the method =>
i paint first all mesh on Submesh 0 then on Submesh 1 (getting submeshes based on a lovely solution by Bunny83 Unity Answer
//in a For Loop
color[i] = new Color (1,UV[i].y,UV[i].y);
Heres the link to a bigger res of the picture OneDrive
I'm not sure what you mean by "looping though a mesh" ^^. Also what $$anonymous$$ and max values you have in $$anonymous$$d? In general texture space goes from 0 to 1 in both axes, U and V. However values outside that range will also result in a mapping but that depends on the textures wrapmode.
If the wrapmode is set to "clamp" it just means that the outermost texels are used. In essence the texture coordinates are clamped between 0 and 1 internally. So a value of 2.3
would just be 1.0
and a value of -0.2
would become 0.0
.
A wrapmode of repeat can be imagined as if the texture is repeated in all directions. This is essentially the same what $$anonymous$$athf.Repeat does with a length of 1. So a value of 2.3
would become 0.3
and a value of -0.2
would become 0.8
.
It would help if you could go more into detail what you want to do :)
Hey Thanks!
Ok what i want to do is following. Paint vertex colors on a Tree.
the color channels are following: - R: $$anonymous$$ain Wind Bending - G: Random Wind Phase on Leafs - B: Shivering on Leafs
so as a little example:
for (int i = 0; i<mesh.colors.Length; i++)
{
// $$anonymous$$ain Bending Calculation
col[i] = Color.Lerp(new Color(0,0,0),new Color(1,0,0),currentHight / mesh.bounds.size.y);
// Rest Calculations
if (isLeaf)
{
var WindPhase = $$anonymous$$athf.Lerp(0, Random.value, currentUV_Y_pos / maxUV_Y_pos);
var fluttering = $$anonymous$$athf.Lerp(0, DistanceTo$$anonymous$$eshCenter * WindPhase, currentUV_Y_pos / maxUV_Y_pos);
col[i] = new Color(col[i].r, WindPhase, fluttering);
}
}
if i do not know where the Face of the Leaf begins, the results are in floating, not connected to the Trunk/Branch Leafs.
Answer by Bunny83 · Apr 23, 2020 at 12:51 AM
Ok I'm still not entirely sure what you want to do. What I understood is that you want to perform some vertex painting (inside the editor?!) and you essentially want to filter the vertices based on what part of the UV map the vertex belongs to. Is that what you want to do?
As mentioned in the comment above the UV space itself doesn't have an end. Even the texture itself is located between 0 and 1 where 0,0 is the bottom left corner and 1,1 the top right corner, you can specify coordinates greater than 1 / smaller than 0. Though those generally are mapped back to the 0 - 1 space based on the texture wrapmode.
If you have an unwrapped mesh in Unity you can use my UVViewer to view the UV map inside Unity. If you want to determine the min and max UV coordinates of your mesh you have to iterate through all vertices and just determine the min and max values. However I'm not sure how the min and max values would help to determine which vertices belong to which parts. This purely depends on the actual used texture and where the different parts are located on the texture.
Just to answer the question title: For a given mesh you can determine the min and max UV coordinate like this:
Vector2 min = Vector2.one * float.PositiveInfinity;
Vector2 max = -min;
var uvs = mf.sharedMesh.uv;
foreach(var uv in uvs)
{
if (uv.x < min.x) min.x = uv.x;
if (uv.x > max.x) max.x = uv.x;
if (uv.y < min.y) min.y = uv.y;
if (uv.y > max.y) max.y = uv.y;
}
This will essentially calculate the AABB of the first uv channel of all vertices in texture space.
Hey Thanks again =)
i've updated my Question with some Pictures!
Answer by CiberX15 · Apr 22, 2020 at 03:09 PM
If I recall correctly, UV coordinates are calculated from 0 to 1, regardless of the size of the image. So if your x and y coordinates are 0, then you are at the bottom left of the image. If they are 1, then you are at the top right. So if you wanted to figure out how close to the top you are you could just read from the Y position, 0.8f would mean you are 80% to the top of the image.
If you need to you can then back track to get how many pixels that is. I don't remember the exact code but its something like:
Image.size.y * uv.y == number of pixels from bottom
hm thats what i thought first aswell! but then i remeber blender and its uv editor, where you can clearly scale the uve over the border of the image. need to investigate further but thanks alot!
Your answer
Follow this Question
Related Questions
Animated tiles and blending between different tiles on a mesh tilemap 0 Answers
How to set up UV for lightmaps for generated meshes? 4 Answers
texturing a procedurally created mesh 0 Answers
Mesh breaks apart when using vertex displacement with shadergraph. 3 Answers
Change uvs on texture packing 0 Answers