- Home /
How to move UV's of a mesh so you can have two textures in one file
Hi,
I'd like to move the a mesh's uv.x value += 512 so it displays a different version of the objects skin that sits inside the same texture.
I've seen this:
mesh.Clear(); mesh.vertices = newVertices; mesh.uv = newUV; mesh.triangles = newTriangles;
Which seems to stretch or shrink the skin.
The end goal to is to only move certains vertices so I have some of the skin coming from one side of the texture and some of it from the other side.
Anyone know what I'm talking about?
As a general note: 512 sounds like you are talking about a shift of 512 pixels. However, uv's are always given in a resolution-independent, normalized space. So if your texture is 1024x1024, a 512 pixel shift would be uv.x+=0.5
Answer by jtbentley · Sep 03, 2010 at 05:12 AM
Offsetting only a few triangle's worth of UV's is going to be needlessly complicated.. However, if you wish to offset the entire UV set of an object, i wrote this..
var xOffset = 0.0; var yOffset = 0.0;
function Start() {
// Build the uvs
var uvs : Vector2[] = new Vector2[(GetComponent(MeshFilter) as MeshFilter).mesh.vertices.Length];
uvs = (GetComponent(MeshFilter) as MeshFilter).mesh.uv; // Take the existing UVs
for (var i = 0 ; i < uvs.Length; i++) uvs[i] = Vector2 (uvs[i].x + xOffset, uvs[i].y + yOffset); // Offset all the UV's
// Apply the modded UV's (GetComponent(MeshFilter) as MeshFilter).mesh.uv = uvs;
// Blow this shiz away
Destroy(this);
}
This helped me tremendously after hours of searching and not finding what I needed.. Thanks!
thanks for this code, i tried to paste it to a simple instantiation and it runs ok but the UV's of all the clone objects are still the same.
Answer by lowbloodsugar · Mar 17, 2010 at 06:28 AM
1) Have you consider trying to achieve the effect by manipulating a material rather then animating a mesh's uvs?
Materials with texture have a tiling xy AND an offset xy.
I would highly recommend experimenting with the material in the editor before writing your script to automate it.
You will need to adjust the tiling to get the new skin to work. This will have the effect of stretching or shrinking the skin until you get it right. Next, manipulate the offset x or y. This will slide the entire texture map across the surface of your object.
Once you understand how get control over that, then write the script that manipulates the material.
2) It is difficult to understand your question without seeing it. Can you post an concept image that describes the look you are going for?
Here's an example. The first picture is the original undamaged texture, the second is a texture that shows the entire object damaged and the third represents only 1 face of the object damaged.
[url=http://i.imagehost.org/view/0319/test2][img]http://i.imagehost.org/t/0319/test2.jpg[/img][/url]
I wouldn't advise doing this since it will create a new instance of the material thus another draw call
Answer by theRaddRedd · Nov 18, 2012 at 01:15 PM
Don't know how old this is, but I wrote a script that let's you slide UVs in the editor visually while still batching. It's a little buggy, but it works :)
Link: http://ethanredd.tumblr.com/post/35949640385/simple-free-uvslider-i-wrote
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
Textures - n00b questions 3 Answers
How do I map a texture to a primitive instance? 4 Answers
creating a mesh and generating it's specific uvs 2 Answers
Lightmapping Issues 0 Answers