- Home /
Procedural Mesh with visible seams
Hello,
I've been working on proceduraly generated meshs with variable size. At the moment it's just a big square although I intend it to have holes in the future. Because I want those meshes to have multiple textures that I will get through WWW I am creating an atlas texture map (through PackTexture) but when I use the atlas, the squares' seams become noticeble. If I use a material with a single texture the seams are not visible. I've been searching the web but so far I have yet to figure out why this is happening.
The way I generate the mesh is: For each cell I create four vertices, then two triangles and finaly UV's. The UV's are based on the Rect list obtained from Texture.PackTextures.
The generating code:
using UnityEngine;
using UnityEditor;
using System.Collections;
public class MeshCreator : MonoBehaviour
{
public Texture2D tex1;
public Texture2D tex2;
public Texture2D tex3;
public Texture2D atlas;
void Start ()
{
GetComponent<MeshRenderer>().enabled = false;
Texture2D[] texs = new Texture2D[3];
texs[0] = tex1;
texs[1] = tex2;
texs[2] = tex3;
/*foreach (Texture2D texture in texs)
{
string path = AssetDatabase.GetAssetPath(texture);
//Debug.Log("path: " + path);
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
textureImporter.isReadable = true;
AssetDatabase.ImportAsset(path);
}*/
atlas = new Texture2D(0, 0);
Rect[] texUVS = atlas.PackTextures(texs, 0, 2048);
Mesh myMesh = new Mesh();
int XX = 10, YY = 10;// 10x10 Mesh
int CellSize = 2;
int NumCells = XX * YY;
int NumTriangles = NumCells * 2; // Each cell has 2 tris
int NumVertices = NumCells * 4; // Each cell has 4 verts
int p0, p1, p2, p3;
Vector3[] verts = new Vector3[NumVertices];
int[] triangles = new int[NumTriangles * 3];
Vector2[] uvs = new Vector2[NumVertices];
for(float c = 0, x = 0, z = 0, v = 0, t = 0; c < NumCells; c++, x+= CellSize, v+=4, t+=6)
{
if( c % XX == 0 && c > 0)
{
x = 0;
z+= CellSize;
}
p0 = (int)v;
p1 = (int)v + 1;
p2 = (int)v + 2;
p3 = (int)v + 3;
verts[p0] = new Vector3(x,0,z);
verts[p1] = new Vector3(x + CellSize,0,z);
verts[p2] = new Vector3(x,0,z + CellSize);
verts[p3] = new Vector3(x + CellSize, 0, z + CellSize);
// tri 1
triangles[(int)t] = p2;
triangles[(int)t+1] = p1;
triangles[(int)t+2] = p0;
// tri 2
triangles[(int)t+3] = p2;
triangles[(int)t+4] = p3;
triangles[(int)t+5] = p1;
//uvs
int r = Random.Range(0,(texUVS.Length ));
uvs[p0] = new Vector2(texUVS[r].xMin, texUVS[r].yMin); //(0,0)
uvs[p1] = new Vector2(texUVS[r].xMax, texUVS[r].yMin); //(1,0)
uvs[p2] = new Vector2(texUVS[r].xMin, texUVS[r].yMax); //(0,1)
uvs[p3] = new Vector2(texUVS[r].xMax, texUVS[r].yMax); //(1,1)
/*
uvs[p0] = new Vector2(0.0f, 0.0f);
uvs[p1] = new Vector2(1.0f, 0.0f);
uvs[p2] = new Vector2(0.0f, 1.0f);
uvs[p3] = new Vector2(1.0f, 1.0f);*/
}
myMesh.vertices = verts;
myMesh.triangles = triangles;
myMesh.uv = uvs;
myMesh.RecalculateNormals();
myMesh.RecalculateBounds();
myMesh.Optimize();
GetComponent<MeshFilter>().mesh = myMesh;
GetComponent<MeshCollider>().sharedMesh = myMesh;
GetComponent<MeshRenderer>().material.mainTexture = atlas;
GetComponent<MeshRenderer>().enabled = true;
}
}
And here's a picture using a black texture in the atlas:
Thank you.
Your answer
Follow this Question
Related Questions
[SOLVED]Procedural generated side by side cubes without 'seams'? 2 Answers
Create the visual spring in Unity? 3 Answers
Creating a Pentahedron (a 5 sided shape) Mesh procedurally 1 Answer
Create planar UVs on procedurally generated mesh 1 Answer
Generate mesh from raycast positions, independent of rotations 0 Answers