- Home /
change uv of a plane so it takes a portion of texture
Hi!
I want to make 3x3 table with nine pieces, with one big texture so that all pieces share the same texture but every piece to have its portion displayed, like a puzzle.
i tried with tiling and offset of the material. here is what i have found: -texture is inverted (making tiling x and y = -1 fixes orientation) -setting tile x = -0.33 and y = -0.33 shows upper right third of the texture -setting offset x = 1.33 sets texture to upper left piece..
i know there is a rule for programming this, i want to generalize so it works for more pieces.
any help appreciated! thans!
Answer by ScroodgeM · Jul 29, 2012 at 05:32 PM
if your texture contains 3x3 fragments then:
set tiling to x=0.3333 y=0.3333 - this will crop one fragment only
set offset to x=0 or 0.3333 or 0.6666, y=0 or 0.3333 or 0.6666 for fragment you need
if you need to turn fragment, you can make negative tiling settings, but more correct will be to change mapping or object orientation. to save some unneeded drawcalls, use geometry UV sets instead of material's settings
bonus: MeshGeneratorPlanes3x3.cs
attach this script to any gameObject and assign a material you want to split to 3x3 tiles. after running game you'll got 9 separated tiles with same material
using UnityEngine; using System.Collections; public class MeshGeneratorPlanes3x3 : MonoBehaviour { public Material TileSet; void Start() { for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { GameObject tile = new GameObject(); MeshFilter mf = tile.AddComponent<MeshFilter>(); MeshRenderer mr = tile.AddComponent<MeshRenderer>(); tile.name = string.Format("tile_x{0}_y{1}", x, y); mr.material = TileSet; Mesh mesh = new Mesh(); mesh.vertices = new Vector3[] { new Vector3(-0.5f, -0.5f, 0f), new Vector3(+0.5f, -0.5f, 0f), new Vector3(-0.5f, +0.5f, 0f), new Vector3(+0.5f, +0.5f, 0f) }; mesh.normals = new Vector3[] { new Vector3(0f, 0f, 1f), new Vector3(0f, 0f, 1f), new Vector3(0f, 0f, 1f), new Vector3(0f, 0f, 1f) }; float th=1f/3f; Vector2 tileOffset = new Vector2((float)x, (float)y) * th; mesh.uv = new Vector2[] { new Vector2(0f, 0f) + tileOffset, new Vector2(th, 0f) + tileOffset, new Vector2(0f, th) + tileOffset, new Vector2(th, th) + tileOffset }; mesh.triangles = new int[] { 0,1,2,1,3,2 }; mf.mesh = mesh; tile.transform.position = tileOffset * 4f; } } } }
ok, i would if i knew how to access uv sets i used simple plane.
thanks! i managed to adopt this to my needs and learn a lot in the process! thank you again