Multiple UV maps (532016)

Hello, i created a mesh in blender with 2 UV maps, when i import it to Unity, the first UV maps is mesh.uv, and the second one is mesh.uv1 and mesh.uv2 (dont know why is duplicated)

i want to be able to swap between these UV maps in runtime, is there a good solution for this? my idea was to use mesh.uv1 and mesh.uv2 as my first and second maps, and asign mesh.uv to one of these. Is there a better solution than playing with this values? i heard something about changing the shader but i am not good with shaders.

Hello,

first of all, uv1 and uv2 refer to the same array of uv coordinates, so in total we only have two sets of uv coordinates per mesh (uv and uv1/uv2). To swap between uv’s during runtime you need to create a script that swaps your uv’s while also preserving the unused set.

//c# Snipped

Vector2[] unused;

...

void SwapUV(Mesh mesh)
{
     var temp = mesh.uv;
     mesh.uv = this.unused;
     this.unused= temp;
}

You can adapt this code to just swap uv and uv1 or store both uv and uv1 when loading the asset.

Best regards,
entro-p

You could also do it in the shader. Use a step() function in the vertex shader to switch between UVs sets based on a shader parameter you can set at runtime.

You could also do a texture fetch for both uv sets in the fragment shader and then blend between them (if you wanted to fade smoothly from one to the other).

You wouldn’t need to update the data for your entire mesh, and it would be more flexible to work with it in the shader.

I don’t have an textured vertex/frag shader on hand at the moment to modify, or I could write up a quick example for you. If you ask in the ShaderLab forum, someone will probably help you write one out to achieve the effect you’re looking for.

i did this code so when i change dualtexture bool, it executes setTextures that change the texture to single or atlas after assign the UV map needed.

public class paraScript : MonoBehaviour {

	private bool _DUALTEXTURE;
	private Mesh mesh;
	private Vector2[] unused;
	
	public int position, type, model, texture, texture2;
	public bool mainUV;
	public bool dualtexture
	{
		get {return _DUALTEXTURE;}
		set {_DUALTEXTURE = value; setTextures();}
	}

	void Start () {
		mainUV = true;
		this.mesh = GetComponent<MeshFilter> ().mesh;
	}

	void Update () {}


	public void setTextures()
	{
		Material m1 = Resources.Load ("Materials/" + Globals.textureList.Find (i => i.value == this.texture).name, typeof(Material)) as Material;

		if (_DUALTEXTURE) 
		{
			if (mainUV == true)
				swapUVS();

			Texture2D[] atlasTextures = new Texture2D[2];
			Texture2D atlas = new Texture2D (512, 512);

			Material m2 = Resources.Load ("Materials/" + Globals.textureList.Find (i => i.value == this.texture2).name, typeof(Material)) as Material;
			atlasTextures [0] = m1.mainTexture as Texture2D;
			atlasTextures [1] = m2.mainTexture as Texture2D;

			Rect[] rects = atlas.PackTextures (atlasTextures, 0);

			renderer.material.mainTexture = atlas;

		} 
		else 
		{
			if (mainUV == false)
				swapUVS();

			renderer.material = m1;
		} 
	}

	public void swapUVS()
	{
		unused = mesh.uv;
		mesh.uv = mesh.uv1;
		mesh.uv1 = unused;
		mesh.uv2 = unused;

		mainUV = !mainUV;
	}
}