- Home /
How does UV mapping via script work?
I've been looking into UV mapping, in order to give different sections of the same mesh different textures, and the UV mapping needs to be done programmatically since the mesh's textures can change at runtime. Although there are already numerous questions and answers on this forum about the topic, I still seemingly cannot understand them. I don't understand how UV's, being an array of Vector2s, could tell how to texture an item. Could someone please explain to me how this works?
You generally don't change UVs when you change textures.
The only time you would is if the textures have different usable areas. Like one is a wood square, but another is bottom half stone and top half junk.
For characters or armour with texture swaps (where the texture isn't just a big tilable square) it's the artist's job to paint each texture using the same UV's.
Answer by robertbu · Aug 16, 2013 at 06:58 PM
UV coordinates specify a specific point on a texture by fractions in the range of 0.0 to 1.0.
So a value of (0.5, 0.5) would refer to the center-most pixel in a texture. In meshes, we are specifying the uvs for the vertices (which are used to define triangles). So all the pixels inside the triangles are filled by interpolating between the values we specify for the vertices.
So to experiment a bit:
Create a vertical plane but leave all other setting at default. This will create a two-triangle, four-vertices plane.
Add a material with a texture.
Attach this script:
pragma strict
var mesh : Mesh; var uvs : Vector2[];
function Start() { mesh = GetComponent(MeshFilter).mesh; uvs = mesh.uv; for (var i = 0; i < uvs.Length; i++) Debug.Log(uvs[i]);
}
function Update() {
// Upper right quarter if (Input.GetKeyDown(KeyCode.A)) {
uvs[0] = Vector2(0.5, 0.5); uvs[1] = Vector2(1.0, 0.5); uvs[2] = Vector2(0.5, 1.0); mesh.uv = uvs; } // Middle quater if (Input.GetKeyDown(KeyCode.B)) { uvs[0] = Vector2(0.25, 0.25); uvs[1] = Vector2(0.75, 0.25); uvs[2] = Vector2(0.25, 0.75); uvs[3] = Vector2(0.75, 0.75); mesh.uv = uvs; } //Left half if (Input.GetKeyDown(KeyCode.C)) { uvs[0] = Vector2(0.0, 0.0); uvs[1] = Vector2(0.5, 0.0); uvs[2] = Vector2(0.0, 1.0); uvs[3] = Vector2(0.5, 1.0); mesh.uv = uvs; } //A mess if (Input.GetKeyDown(KeyCode.D)) { uvs[0] = Vector2(0.25, 0.55); uvs[1] = Vector2(0.0, 0.1); uvs[2] = Vector2(0.7, 0.2); uvs[3] = Vector2(0.4, 0.6); mesh.uv = uvs; } }
Pressing the 'A', 'B', 'C' and 'D' key resets the vertices in the mesh.
This simple example doesn't have an object with multiple materials and submeshes. While the uv concept is simple, identifying the right vertices and assigning the right values can be a challenge.
Answer by Joyrider · Aug 16, 2013 at 05:33 PM
Every mesh is composed of 1 or more submeshes, and every submesh has a material ID. So if you have several materials on a mesh, your have several submeshes, each with their material.
Every submesh is composed of triangles, that you can find in the triangle list of your submesh
Every triangle, that is composed of 3 vertices, and each of those vertices have a UV coordinate that correspond to X and Y coordinates in your texture.
so if you know your submesh, you know what material you are using, and thus the texture. And if you know what triangle, you can get the ID of the vertices of that triangle and thus their UV.
Your answer
Follow this Question
Related Questions
Blender to Unity strange normal import problem (bright spots) 1 Answer
Delete Uv2 of a mesh? 1 Answer
Unity procedural mesh generation UV texture flickering horizontal lines 0 Answers
uv layout problem 0 Answers
Normalizing mesh UVS? 1 Answer