- Home /
Front Culling
"I need a hero to save me now I need a hero (save me now) I need a hero to save my life A hero'll save me (just in time) I need a hero to save my life I need a hero just in time Save me just in time Save me just in time"
So I am trying to mesh a cylinder of linearly varying radius from some finite number all the way down to 0. However, when I view this object from outside the front side doesn't render while showing me only the backside, I guess the terminology for that is front culling. When viewed from inside it seems fine however (see attached photos). I am new to meshes so I have no idea what's going on, I have been dealing with this bug for hours but to no avail...its driving me insane, please save me.
I will also attach my source code below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class Branch : MonoBehaviour
{
public Mesh mesh;
int[] triangles;
public float[] weights;
public float radius = 10;
public float height = 10;
public int thetaNo = 9;
public int zNo = 5;
public float offset = 0.5f;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
mesh.Clear();
triangles = new int[thetaNo * zNo * 6];
weights = new float[zNo * thetaNo];
for (int j = 0; j < zNo - 1; j++)
{
for (int i = 0; i < thetaNo; i++)
{
if (i == thetaNo - 1)
{
triangles[j * (thetaNo * 6) + i * 6 + 2] = j * thetaNo + 0 + thetaNo;
triangles[j * (thetaNo * 6) + i * 6 + 0] = j * thetaNo + i + 0;
triangles[j * (thetaNo * 6) + i * 6 + 1] = j * thetaNo + i + 0 + thetaNo;
triangles[j * (thetaNo * 6) + i * 6 + 5] = j * thetaNo + 0;
triangles[j * (thetaNo * 6) + i * 6 + 3] = j * thetaNo + i + 0;
triangles[j * (thetaNo * 6) + i * 6 + 4] = j * thetaNo + 0 + thetaNo;
}
else
{
triangles[j * (thetaNo * 6) + i * 6 + 2] = j * thetaNo + i + 1 + thetaNo;
triangles[j * (thetaNo * 6) + i * 6 + 0] = j * thetaNo + i + 0;
triangles[j * (thetaNo * 6) + i * 6 + 1] = j * thetaNo + i + 0 + thetaNo;
triangles[j * (thetaNo * 6) + i * 6 + 5] = j * thetaNo + i + 1;
triangles[j * (thetaNo * 6) + i * 6 + 3] = j * thetaNo + i + 0;
triangles[j * (thetaNo * 6) + i * 6 + 4] = j * thetaNo + i + 1 + thetaNo;
}
}
}
mesh.vertices = createShape(radius, height, thetaNo, zNo, offset);
mesh.triangles = triangles;
for(int i = 0; i < mesh.normals.Length; i++)
{
mesh.normals[i] = -mesh.normals[i];
}
}
private void Update()
{
mesh.RecalculateNormals();
}
Vector3[] createShape(float radius, float height, int thetaNo, int zNo, float offset)
{
int zStep = (int) (height / zNo);
int thetaStep = 360 / thetaNo;
Vector3[] array = new Vector3[zNo * thetaNo];
for (int i = 0, z = 0; z < height; z += zStep)
{
float newRadius = (-radius / (height - zStep)) * z + radius;
float xOffset = Random.Range(-newRadius * offset, newRadius * offset);
float yOffset = Random.Range(-newRadius * offset, newRadius * offset);
for (int theta = 0; theta < 360f; theta += thetaStep)
{
weights[i] = 1 - newRadius/radius;
array[i] = new Vector3(newRadius * Mathf.Cos(Mathf.Deg2Rad * theta) + xOffset, newRadius * Mathf.Sin(Mathf.Deg2Rad * theta) + yOffset, z);
i++;
}
}
return array;
}
}
Answer by Eno-Khaon · Jul 01, 2021 at 02:57 AM
Well, simply put, it's probably safe to assume the shader is culling back faces. The problem is that you're building the faces inside-out.
Whether you build the faces clockwise or counter-clockwise makes a difference, so you simply need to reverse that. For a quick-fix sort of approach, it should be as simple as:
// Swap triangle order here v
triangles[j * (thetaNo * 6) + i * 6 + 1] = j * thetaNo + 0 + thetaNo;
triangles[j * (thetaNo * 6) + i * 6 + 0] = j * thetaNo + i + 0;
triangles[j * (thetaNo * 6) + i * 6 + 2] = j * thetaNo + i + 0 + thetaNo;
triangles[j * (thetaNo * 6) + i * 6 + 4] = j * thetaNo + 0;
triangles[j * (thetaNo * 6) + i * 6 + 3] = j * thetaNo + i + 0;
triangles[j * (thetaNo * 6) + i * 6 + 5] = j * thetaNo + 0 + thetaNo;
// Swap triangle order here ^
In this example, I swapped 1<->2 and 4<->5. That reverses the order of vertices in each triangle and, therefore, inverts the faces' fronts and backs.