I’m making a mesh editor, and I’m currently facing a big problem, Z-Fighting
For Context: I am trying to make a procedural mesh generator similar to the one in Little Big Planet, and it has been going pretty alright as far as it goes.
But today, when I was getting ready to start adding textures, I found a problem I hadn’t tought of… And after looking everywhere and asking in discord groups where there are decently experienced people, my time to ask here came.
This is my code:
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class MeshBrush : MonoBehaviour
{
private bool isCreating = false;
Vector3 mousePOS;
Vector3 lastMousePOS;
Mesh mesh;
List<Vector3> verticesList = new List<Vector3>();
List<int> trianglesList = new List<int>();
void Awake()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
}
//----------------//
// UPDATE //
//----------------//
void Update() {
CreateSquare();
}
//---------------------------//
// MeshBrush Methods //
//---------------------------//
void CreateSquare() {
if (Input.GetMouseButtonDown(0)) {
isCreating = true;
lastMousePOS = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0)) {
isCreating = false;
}
if (isCreating)
{
mousePOS = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(mousePOS);
RaycastHit hit;
Ray lastRay = Camera.main.ScreenPointToRay(lastMousePOS);
RaycastHit lastHit;
if (Physics.Raycast(ray, out hit) && Physics.Raycast(lastRay, out lastHit)) {
Vector3 hitPointPOS1 = hit.point;
Vector3 hitPointPOS2 = lastHit.point;
// Add vertices
int baseIndex = verticesList.Count;
Vector3[] newVertices = new Vector3[]
{
//Square 1
hitPointPOS1 + new Vector3(-1, 1, 0), //0
hitPointPOS1 + new Vector3(1, 1, 0), //1
hitPointPOS1 + new Vector3(-1, -1, 0), //2
hitPointPOS1 + new Vector3(1, -1, 0), //3
//Square 2
hitPointPOS2 + new Vector3(-1, 1, 0), //4
hitPointPOS2 + new Vector3(1, 1, 0), //5
hitPointPOS2 + new Vector3(-1, -1, 0), //6
hitPointPOS2 + new Vector3(1, -1, 0), //7
//Square 1 Depth
hitPointPOS1 + new Vector3(-1, 1, 2), //8
hitPointPOS1 + new Vector3(1, 1, 2), //19
hitPointPOS1 + new Vector3(-1, -1, 2), //10
hitPointPOS1 + new Vector3(1, -1, 2), //11
//Square 2 Depth
hitPointPOS2 + new Vector3(-1, 1, 2), //12
hitPointPOS2 + new Vector3(1, 1, 2), //13
hitPointPOS2 + new Vector3(-1, -1, 2), //14
hitPointPOS2 + new Vector3(1, -1, 2), //15
};
verticesList.AddRange(newVertices);
// Create triangles
int[] newTriangles = new int[]
{
//Square - Front
baseIndex, baseIndex + 1, baseIndex + 2,
baseIndex + 1, baseIndex + 3, baseIndex + 2,
//Square - Top
baseIndex, baseIndex + 8, baseIndex + 9,
baseIndex, baseIndex + 9, baseIndex + 1,
//Square - Bottom
baseIndex + 2, baseIndex + 11, baseIndex + 10,
baseIndex + 2, baseIndex + 3, baseIndex + 11,
//Square - Left
baseIndex, baseIndex + 2, baseIndex + 10,
baseIndex, baseIndex + 10, baseIndex + 8,
//Square - Right
baseIndex + 1, baseIndex + 9, baseIndex + 3,
baseIndex + 9, baseIndex + 11, baseIndex + 3,
//-------------------//
// EXTRUSION //
//-------------------//
//Top Right
baseIndex, baseIndex + 1, baseIndex + 4,
baseIndex + 1, baseIndex + 7, baseIndex + 4,
baseIndex + 1, baseIndex + 3, baseIndex + 7,
//Top Left
baseIndex, baseIndex + 6, baseIndex + 2,
baseIndex, baseIndex + 1, baseIndex + 5,
baseIndex, baseIndex + 5, baseIndex + 6,
//Bottom Right
baseIndex, baseIndex + 4, baseIndex + 2,
baseIndex + 2, baseIndex + 4, baseIndex + 7,
baseIndex + 2, baseIndex + 7, baseIndex + 3,
//Bottom Left
baseIndex + 1, baseIndex + 3, baseIndex + 5,
baseIndex + 2, baseIndex + 6, baseIndex + 3,
baseIndex + 5, baseIndex + 3, baseIndex + 6,
};
trianglesList.AddRange(newTriangles);
UpdateMesh();
}
}
lastMousePOS = mousePOS;
}
void UpdateMesh()
{
mesh.vertices = verticesList.ToArray();
mesh.triangles = trianglesList.ToArray();
mesh.RecalculateNormals();
}
}
I was thinking at first to merge vertices, and triangles, but i couldn’t find a good solution to that, since I’m not the best with Procedural Mesh Generation.
I plan to add some more to this code later on, because this is still a pretty incomplete script, so I would like some solutions that could apply to similar shapes like polygons and triangles aswell.