x


Offset UVs to allow multiple meshes to use same texture

Hey guys,

I am making a tile based game. I have many different 3d tiles using simple textures, which have been merged in Photoshop into the texture below.

Each tile has an id number which I use to instantiate the correct 3d tile at runtime.

My question is how should I offset the texture to ensure each tile uses the correct part of the texture UV.

The texture is made up of a 10x10 grid. The current code applies the texture for the bottom left corner to the whole selection.

    MeshRenderer tileRender = _tile.transform.GetComponentInChildren<MeshRenderer>();
    MeshFilter tileFilter = _tile.transform.GetComponentInChildren<MeshFilter>();

    tileRender.material = crunchTex;

    //Build the uvs 
    Vector2[] uvs  = new Vector2[tileFilter.sharedMesh.vertices.Length]; 
    uvs = tileFilter.sharedMesh.uv; // Take the existing UVs 

    //get correct positions in array (it is a 10x10 grid
    float tempxOffset = (_id % 10);

    xOffset = (tempxOffset /10);

    xOffset = (1 - xOffset);

    yOffset = (_id / 10);

    yOffset = Mathf.Ceil(yOffset);

    yOffset = (yOffset/10);

    for (var i = 0 ; i < uvs.Length; i++)
    {
       uvs[i] = new Vector2(uvs[i].x, uvs[i].y); // ???
    }

   // Apply the modded UV's 
   tileFilter.sharedMesh.uv = uvs; 

texture map

textureSet.png (218.6 kB)
more ▼

asked May 17 '12 at 07:11 AM

Dom gravatar image

Dom
13 2 3 7

(comments are locked)
10|3000 characters needed characters left

0 answers: sort newest
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x236
x4

asked: May 17 '12 at 07:11 AM

Seen: 416 times

Last Updated: May 17 '12 at 07:11 AM