- Home /
Why would my texture split across the triangle at runtime?
I am trying to understand basic uv manipulation in Unity. I use Blender and get the fundamentals of using UVs. In this example I have cached the uvs of a Blender quad into an array. I use a conditional to know which uv pair is at which index in the .mesh.uv (seems like the array arrangement in .mesh.uv varies per import!), so I know when I have a given uv pair, then I cache them to a new array so their order makes more sense. For example, I keep the (0,0) at [0], the (0,1) at [1], etc. All I want to do is input a new uv "bounding box" with new uv values across these indices to crop out sections for basic texture atlas effect on the big image. What happens though is my texture crops wierdly across the triangles. I must be missing something obvious. I assume - and I'm big on assumptions :) - that the uvs in Unity should give me a discreet bounding box "cropping" result based on their 0 to 1 axial values. Below is my for loop I run in the Start() and then a function I call under Update to write in any new uv values (they are public exposed for the editor).
for (var i = 0; i < _targetMesh.uv.Length; i++) {
// Debug.Log ("_UV [" + i + "]: " + _targetMesh.uv [i]);
if (_targetMesh.uv [i] == new Vector2 (0, 0)) {
Debug.Log ("we have " + _targetMesh.uv [i] + " at [" + i + "]");
_UVs [0] = _targetMesh.uv [i];
uv00 = _UVs [0];
}
if (_targetMesh.uv [i] == new Vector2 (0, 1)) {
Debug.Log ("we have " + _targetMesh.uv [i] + " at [" + i + "]");
_UVs [1] = _targetMesh.uv [i];
uv01 = _UVs [1];
}
if (_targetMesh.uv [i] == new Vector2 (1, 0)) {
Debug.Log ("we have " + _targetMesh.uv [i] + " at [" + i + "]");
_UVs [2] = _targetMesh.uv [i];
uv10 = _UVs [2];
}
if (_targetMesh.uv [i] == new Vector2 (1, 1)) {
Debug.Log ("we have " + _targetMesh.uv [i] + " at [" + i + "]");
_UVs [3] = _targetMesh.uv [i];
uv11 = _UVs [3];
}
My function:
private void updateUVs ()
{
_UVs [0] = uv00;
_UVs [1] = uv01;
_UVs [2] = uv10;
_UVs [3] = uv11;
_targetMesh.uv = _UVs;
}
(editor view) (runtime view)
I just want to implement some basic texture atlas setup. I'm stumped at the moment and I have poured over the forums/stacks but not seen this specific issue. Seems such a simple task for a quad and 4 verts and 4 uv pairs. Thanks for any input!
Your answer
Follow this Question
Related Questions
More materials smaller texture size vs less materials bigger texture size 0 Answers
Wrong import of UV from blender 1 Answer
Is it possible to UV map a cylinder without duplicating a column of vertices? 1 Answer
UV mapping tiles on mesh bleeds texture when camera is at certain angle 2 Answers
Assigning UV Map to model at runtime 0 Answers