Comments and answers for "Remove Distortion on custom UV mesh"
http://answers.unity.com/questions/1550584/remove-distortion-on-custom-uv-mesh.html
The latest comments and answers for the question "Remove Distortion on custom UV mesh"Comment by Superdsgross on Superdsgross's answer
http://answers.unity.com/comments/1550591/view.html
That worked perfectly. Thanks so much!Fri, 07 Sep 2018 05:43:17 GMTSuperdsgrossAnswer by Bunny83
http://answers.unity.com/answers/1550590/view.html
Using "abs" makes not really sense here. You calculate the whole range between the min and max positions. However if min is negative you do a strange folding on the negative values. For example imagine X min is -2 and X max is 18. That gives a range of "20". You take the absolute value of the actual x value and divide by 20. Since no x value is actually 20 you never get a value of 1. Due to the absolute value you just flip the negative part into the positive.
<br>
There are several things you should change. First of all to get the min and max values, don't use two seperate lists that's just a waste of memory and processing time.
Second you have to subtract the min value from the actual coordinate before you divide by the range.
Vector3[] vertices = mesh.vertices;
Vector3 min = Vector3.one * float.PositiveInfinity;
Vector3 max = Vector3.one * float.NegativeInfinity;
foreach(Vector3 v in vertices)
{
if (v.x > max.x) max.x = v.x;
if (v.y > max.y) max.y = v.y;
if (v.z > max.z) max.z = v.z;
if (v.x < min.x) min.x = v.x;
if (v.y < min.y) min.y = v.y;
if (v.z < min.z) min.z = v.z;
}
Vector3 size = max - min;
Vector2[] uvs = new Vector2[vertices.Length];
for (int i = 0; i < uvs.Length; i++)
{
Vector3 v = vertices[i] - min;
uvs[i] = new Vector2(v.x / size.x, v.z / size.z);
}
mesh.uv = uvs;Fri, 07 Sep 2018 05:39:40 GMTBunny83