Hi, I am currently working on a project in which we want to change a mesh’ secondary uv map based on various things including the alpha values in its main texture.
I have already been fiddling around with this, though for some reason I cannot seem to get the correct pixels.
I hope you guys can help me out, and thanks in advance!
Question is too vague. What exactly are you trying to do? How exactly are you trying to change uv map? What exactly goes wrong?
P.S. I think this should’ve been in shader subforum. Most likely what you want is modifying uvs in shader, not in scripts.
We have a 3D model of a person, and from this model we want to procedurally change the skin.
Though since it’s one model (without split up parts) we decided to add an alpha layer to the texture, so we can determine what is what.
Though when I try to grab the pixel from the texture that corresponds to the uv coordinate it doesn’t return the right colours (only black and green for some reason, both with a wrong alpha value).
In order to get the pixels from the texture, I use ‘GetPixels32()’, and know I have to take the width and height of the texture into consideration.
I’m not quite familiar with the use of shaders yet, but if that’s an easier way of solving this problem I will probably take a look into that.
Make sure you read manual: Unity - Scripting API: Texture2D.GetPixels32
Green/black issue can come from trying to get pixels of compressed texture.
Shaders might not be appropriate as solution for your problem because they are applied after filter on texture, so all your alphas on texture will be ‘blurred’. I’m not sure why you don’t want to split your model into several materials though, up to 5-10 materials should not cause any problem.
I solved the green / black colour problem (I miscalculated something *facepalm).
Though the determination whether the specific triangle is a piece of clothing still doesn’t work…
I currently have this:
clothing = new bool[triangles.Length / 3];
Color32[] pixels = (GetComponent<MeshRenderer>().material.mainTexture as Texture2D).GetPixels32();
int size = (int)Mathf.Sqrt(pixels.Length);
for (int i = 0; i < triangles.Length; i += 3)
{
/**
* Get uv's vertices
* translate these three points to texture positions
* if alpha = 0, clothing is true
* else, clothing is false.
*/
// The centre of the triangle
float uvc = (
((uv[triangles[i]].y * size) * size + uv[triangles[i]].x * size) +
((uv[triangles[i + 1]].y * size) * size + uv[triangles[i + 1]].x * size) +
((uv[triangles[i + 2]].y * size) * size + uv[triangles[i + 2]].x * size)) / 3;
// uv[i].y/.x returns a value beteen 0 and 1, and is therefore multiplied by the size.
clothing[i / 3] = (
pixels[(int)((uv[triangles[i]].y * size) * size + uv[triangles[i]].x * size)].a != 0 ||
pixels[(int)((uv[triangles[i + 1]].y * size) * size + uv[triangles[i + 1]].x * size)].a != 0 ||
pixels[(int)((uv[triangles[i + 2]].y * size) * size + uv[triangles[i + 2]].x * size)].a != 0 ||
pixels[(int)uvc].a != 0);
// This is for me to see what triangles are considered skin (it simply draws a line).
if (!clothing[i / 3])
{
DrawLineOnTriangle(i);
DrawLineOnVertex(i);
}
}
Unity might’ve downsized or somehow ‘optimized’ texture, causing alpha == 0 to always fail. Allow some margin of error. Check if that’s the case.
Also I often tend to get errors inside debug code… Are you sure DrawLineOnTriangle actually works?
And I do hope it’s a square texture, otherwise that code will fail.
P.S. UVs are not necessary within (0,1) range. It’s just having U of 2 means you sample same place as if it was 1 (but if what was error in this case, you’d see array out of bounds error).
Hmm… it doesn’t seem like that happened since it does actually respond in some cases.
The debug code does work, it simply draws a line from the vertex’ world position towards its normal direction. Same thing goes for the triangle, but then the three vertices combined. I’m sure it works because I have used it before.
The texture is square indeed, I took that in mind already.
I really have no clue what’s going wrong, but I’ve spent way too much time on this issue already…
I think I’ll reopen the discussion with our animator/3D-modeller once again to chop up the models.
Anyhow, thanks for the help even though we didn’t manage to solve the issue. ![]()