I have a procedural generated mesh, Im having some issues mapping the textures to it.
The only method I have seen to do it is to use a triplanar shader with the vertices world coordinates. But I am running into issues where the textures don’t overlap well. Does anyone know if theres a way to solve this?
On the picture I uploaded, you can see the very light outlines of the other 2 projections appearing, I dont think that looks great when overlayed like that
I implemented it the same way as in your script, it seems to work in most parts, but fails on some areas. Let me know if you can easily tell why it would be happening, I’m gonna try to understand that code and see if I can figure it out
Thats a really cool project btw
Oh and here’s how I implemented it in my code,
public void OnGenerateMeshJobFinish() {
int vertexCount = vertexCountResult[0];
int indexBufferCount = indexBufferCountResult[0];
Color[] colors = new Color[vertexCount];
Vector3[] vertices = new Vector3[vertexCount];
Vector3[] transformedVertices = new Vector3[vertexCount];
Vector3[] normals = new Vector3[vertexCount];
Vector2[] uvs = new Vector2[vertexCount];
int[] trianglesArray = new int[indexBufferCount];
for (int i = 0; i < vertexCount; i++) {
var vertex = vertexes[i];
vertices[i] = vertex.position;
normals[i] = vertex.normal;
colors[i] = new Color(vertex.materialId / 10f, 0, 0, 0);
transformedVertices[i] = Quaternion.Inverse(transform.rotation) * transform.TransformPoint(vertex.position);
}
for (int i = 0; i < indexBufferCount; i++) {
trianglesArray[i] = triangles[i];
if (i % 3 == 0) {
Vector3 norm = Vector3.Cross(
vertices[triangles[i + 1]] - vertices[triangles[i + 0]],
vertices[triangles[i + 1]] - vertices[triangles[i + 2]]
).normalized;
float dotX = Mathf.Abs(Vector3.Dot(norm, Vector3.right));
float dotY = Mathf.Abs(Vector3.Dot(norm, Vector3.up));
float dotZ = Mathf.Abs(Vector3.Dot(norm, Vector3.forward));
if (dotX > dotY && dotX > dotZ) {
for (int j = 0; j < 3; j++) {
uvs[triangles[i + j]] = new Vector2(vertices[triangles[i + j]].z, vertices[triangles[i + j]].y);
}
} else {
if (dotY > dotX && dotY > dotZ) {
for (int j = 0; j < 3; j++) {
uvs[triangles[i + j]] = new Vector2(vertices[triangles[i + j]].x, vertices[triangles[i + j]].z);
}
} else {
for (int j = 0; j < 3; j++) {
uvs[triangles[i + j]] = new Vector2(vertices[triangles[i + j]].x, vertices[triangles[i + j]].y);
}
}
}
}
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.normals = normals;
mesh.triangles = trianglesArray;
mesh.colors = colors;
mesh.uv = uvs;
meshFilter.mesh = mesh;
meshCollider.sharedMesh = mesh;
vertexes.Dispose();
triangles.Dispose();
vertexCountResult.Dispose();
indexBufferCountResult.Dispose();
if (shouldSaveToFile && (initialized || !shouldLoadFromFile)) {
pendingSave = true;
}
if (OnStart != null) {
OnStart(this);
}
if (OnceOnMeshGenerated != null) {
OnceOnMeshGenerated();
OnceOnMeshGenerated = null;
}
initialized = true;
}
Looking purely at your image I speculate you are sharing vertices between all faces? This means it can be nicely smoothed automagically, but then it means your UV mapping is problematic. Remember that in Unity UVs live alongside normals and alongside vertices. So you cannot share a vertex AND have two different UV coords.
Thank you, glad you like it. I love procgen but it’s one of those arts I think that you never master, you just steadily try more things and get more and more comfortable with how the tools work. My current big achievement is my Jetpack Kurt game, which has lots of procgen. Here’s my lunar surface procgen: