- Home /
procedural texture uv error
I am creating a game with procedural generated plane and uv, it works mostly fine, but there is one place where the uv seems distorted in a very confusing way.
Intro: the uv is mapped in a "Global" uv coordinates, instead of a local one, because the texture has to be aligned with adjacent planes. Each texture has a tiling of 2*2, I introduce a uv scale of 2 which means each plane "v" coordinates will be mapped in range [0, 0.5] of the texture vertically, to make sure horizontally "u" coordinates do not go out of range [0, 1]. (horizontally the noise is unpredictable to some extend). code:
Vector2[] CalculateU(float[] leftXMap, float[] rightXMap) {
int length = leftXMap.Length;
float totalHeight = (length - 1) * 1; // distance between two vertices vertically is 1
float uvheight = totalHeight * uvScale; // uvScale is 2, purpose is to map a plane's v(vertically), into a range of [0, 0.5]..
Vector2[] UMap = new Vector2[length * 2];
for(int i = 0; i < length; i++) {
float left = leftXMap[i];
float right = rightXMap[i];
// make left and right positive.
while (left < 0) {
left += uvheight;
}
while(right < 0) {
right += uvheight;
}
float leftu = (left % uvheight) / uvheight;
float leftv = (i / (float)(length-1)) / uvScale;
float rightu = (right % uvheight) / uvheight;
float rightv = leftv; //(i / (float)length) / 2f;
UMap[i * 2] = new Vector2(leftu, leftv);
UMap[i * 2 + 1] = new Vector2(rightu, rightv);
}
}
the distorted uv :
the detailed :
clarification:
the parameters for the function, are the noise maps for the generated plane. the noise is the x coordinates. while the y coordinates are simple value of 0, 1, 2, .... (length-1).
For a little further clarification, where exactly is this "one place" where the uvs wind up distorted? For example, is it near 0, where you'd flip between positive and negative value behavior?
Seeing how the UVs should fundamentally match the mesh, *does* it match? Here's an example script to help verify that element:
// DrawDebugUVs.cs
using UnityEngine;
public class DrawDebugUVs : $$anonymous$$onoBehaviour
{
public $$anonymous$$eyCode keyCode = $$anonymous$$eyCode.P;
Vector2[] uvs;
int[] tris;
public Vector3 position = Vector3.zero;
public Vector3 rotation = Vector3.zero;
public int uvCount;
private void GetUVs()
{
$$anonymous$$eshFilter mf = gameObject.GetComponent<$$anonymous$$eshFilter>();
$$anonymous$$esh m = mf.mesh;
uvs = m.uv;
tris = m.triangles;
uvCount = uvs.Length;
Debug.Log("Getting uvs for " + gameObject.name + ": UV count = " + uvs.Length + ", triangle count = " + tris.Length);
}
private void Update()
{
if(Input.Get$$anonymous$$eyDown(keyCode))
{
GetUVs();
}
if(uvs != null && uvs.Length > 0)
{
Quaternion rot = Quaternion.Euler(rotation);
// Draw 0-1 outline box
Debug.DrawLine(position, position + (rot * Vector3.up), Color.black);
Debug.DrawLine(position + (rot * Vector3.up), position + (rot * new Vector3(1, 1, 0)), Color.green);
Debug.DrawLine(position + (rot * new Vector3(1, 1, 0)), position + (rot * Vector3.right), Color.red);
Debug.DrawLine(position + (rot * Vector3.right), position, Color.black);
// Draw each uv triangle
for(int i = 0; i < tris.Length; i+=3)
{
Debug.DrawLine(position + (rot * uvs[tris[i + 0]]), position + (rot * uvs[tris[i + 1]]));
Debug.DrawLine(position + (rot * uvs[tris[i + 1]]), position + (rot * uvs[tris[i + 2]]));
Debug.DrawLine(position + (rot * uvs[tris[i + 2]]), position + (rot * uvs[tris[i + 0]]));
}
}
}
}
Note: This script uses a hotkey (currently a default of 'P') to activate
wow, I checked again and you are right, it's the place where x coordinates turn negative.
Great thanks!!! I will use that code for a test.
O$$anonymous$$G the problem is solved
I used to map the negative X coordinate values to positive values, then calculate UV coordinates based on the positive values.
But then there is problem, in my case the left noise map is negative, right noise map is positive, after mapping the left turns positive, but way larger than right coordinate value, then the UV is messed
I turned off mapping, it turns out correct.
Which means (very horrible to me), UV coordinate values can be negative, I mean it is $$anonymous$$d blowing
Your answer
Follow this Question
Related Questions
Polygon buffering/offsetting? 1 Answer
Unity procedural mesh generation UV texture flickering horizontal lines 0 Answers
How do we generate a UV mapping data for a complicated mesh? 0 Answers
Is it possible to combine materials into a pattern at runtime? 0 Answers
Procedural Mesh UVs Edge Case? 1 Answer