- Home /
Realtime mesh triangles do not appear depending on camera angle
I am creating a mesh that is a cube with two vertices of the same edge removed. This makes it a prism. The problem is that after clearing the mesh then setting the new vertices and triangles, only half of the triangles appear at the same time. The other half appear when changing the scene camera's angle. Here are some screenshots to show the problem :
As you can see the triangle to the right does not appear in the first screenshot but appears through a missing triangle in the second screenshot. Here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class createPrism : MonoBehaviour {
public Mesh mesh;
void Start () {
mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = {
new Vector3(-0.5f,-0.5f,-0.5f),
new Vector3(0.5f,-0.5f,-0.5f),
new Vector3(-0.5f,0.5f,-0.5f),
new Vector3(0.5f,0.5f,-0.5f),
new Vector3(-0.5f,-0.5f,0.5f),
new Vector3(0.5f,-0.5f,0.5f)
};
int[] triangles = {
0,1,2,
1,2,3,
0,1,4,
1,4,5,
2,3,4,
3,4,5,
1,5,3,
0,2,4
};
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
}
This is probably an easy fix but I couldn't find anything like this online.
Answer by Bunny83 · Apr 17, 2020 at 11:20 AM
Triangles have a direction they face. They have a front side (which is visible) and a back side (which is not visible). The side depends on the winding order of your vertices. In Unity by default a clockwise winding denotes the front face. If the winding order is counter clockwise it will be a backface and (due to backface culling) not visible.
I did not go through your triangle list to figure out which one you have inverted. However note that in order to change the winding order you just have to swap any two vertices of a triangle. I would start by first adding comments to your vertices to keep track which one is which. I highly recommend you create a drawing of your mesh on paper and label your vertices. Then it's much easier to figure out the correct winding order. I also recommend to add comments to each triangle tripple to know which side / face it belongs to. Your prism has 3 quads (6 tirangles) and 2 triangle faces.
Thank you for the very fast response. I already have a drawing so this isn't going to take long. I will also make sure to add comments so I don't get lost. Thank you again for the fast answer !
I just had a bit time and sorted it out in my head ^^
void Start ()
{
mesh = GetComponent<$$anonymous$$eshFilter>().mesh;
Vector3[] vertices = {
new Vector3(-0.5f,-0.5f,-0.5f),
new Vector3(0.5f,-0.5f,-0.5f),
new Vector3(-0.5f,0.5f,-0.5f),
new Vector3(0.5f,0.5f,-0.5f),
new Vector3(-0.5f,-0.5f,0.5f),
new Vector3(0.5f,-0.5f,0.5f)
};
// 2------3
// |\ |\
// | 4----|-5
// |/ |/
// 0------1
int[] triangles = {
1,0,2, // front side
1,2,3, // front side
0,1,4, // bottom side
1,5,4, // bottom side
2,4,3, // back side
3,4,5, // back side
1,3,5, // right side
0,4,2 // left side
};
// [ ... ]
Your answer
Follow this Question
Related Questions
Why does the default Unity sphere have duplicate vertices? 1 Answer
Null Reference Exception Assigning Vertices to Mesh 0 Answers
How to find connected mesh triangles? 2 Answers
UNITY4 all i see is the triangle/verts of objects 1 Answer
How to return from shader the indexes of triangles/vertices that was actually rendered? 1 Answer