UV Mesh only on one Side - how to?
Hello everybody.
I want to make a 3d Jigsaw Puzzle, for that I generate several jigsaw puzzle parts. And I have a Image - like in a real jigsaw puzzle. I want to show this image just on the front of the jigsaw puzzle parts.
Whats the best way to do this?
I tried this two approaches:
A) set the UVs of the puzzle part only for the front face. Result is this Error: Mesh.uv is out of bounds. The supplied array needs to be the same size as the Mesh.vertices array.
B) Generate a plane in code and set the GetComponent().material.mainTextureScale and GetComponent().material.mainTextureOffset. This works for rectangular plane. But not for more complicated "plane-structures".
Thanks a lot for help
Lukas
Answer by lukas_werz · Dec 07, 2016 at 11:58 AM
Solution is to add an empty GameObject and create again the "front mesh" and set the image to it.
Puzzle has an empty GameObject called ImageFrontPlane; mesh and vertices of the front face are presaved.
public void SetImage(int puzzlePosX, int puzzlePosY, int puzzleSizeX, int puzzleSizeY, Material puzzleImage)
{
_imagePlane = gameObject.transform.FindChild("ImageFrontPlane").gameObject;
MeshFilter meshFilter = _imagePlane.AddComponent<MeshFilter>();
if (meshFilter == null)
{
Debug.LogError("MeshFilter not found!");
return;
}
Mesh imageMesh = meshFilter.mesh;
imageMesh.Clear();
imageMesh.vertices = _verticesOfPlane;
imageMesh.triangles = _trianglesOfPlane;
imageMesh.RecalculateNormals();
imageMesh.RecalculateBounds();
imageMesh.Optimize();
float scaleX = 1.0f/puzzleSizeX;
float scaleY = 1.0f/puzzleSizeY;
float offsetX = scaleX*puzzlePosX;
float offsetY = scaleY*puzzlePosY;
ArrayList uvList = new ArrayList();
foreach (Vector2 point in _pointsOfPlane)
{
uvList.Add(new Vector2(offsetX + point.x*scaleX, offsetY + point.y*scaleY));
}
imageMesh.uv = (Vector2[]) uvList.ToArray(typeof(Vector2));
MeshRenderer meshRenderer = _imagePlane.AddComponent<MeshRenderer>();
if (meshRenderer != null)
{
meshRenderer.material = puzzleImage;
}
}