I have made a custom mesh
in code (but this seems to happen on any and when I view it in game or in the editor it will make some faces disappear so that I can see it more clearly. I could understand how this would be useful, but it is rather annoying not only for gameplay but just working on the project. There is probably just a box that I have to uncheck, but I don't know what that is. I've included some screenshots (the first is in game and the second is in the editor. The mesh should be a full pyramid shape). My mesh for a cube has 12 triangles, 8 vertices, and no uvs where as the default mesh has 24 vertices, 12 triangles, and uv and uv2, just for reference. GameObject
that I make)
[EDIT] No! I was wrong! It works perfectly fine with the default cube
and other default objects
Here is the code I used:
if (GameObject.Find ("Hopefully a Cube") == null) {
GameObject cube = new GameObject ("Hopefully a Cube");
Mesh mesh = new Mesh ();
Vector3[] verticies = new Vector3[8];
verticies [0] = new Vector3 (0, 0, 0);
verticies [1] = new Vector3 (100, 0, 0);
verticies [2] = new Vector3 (0, 100, 0);
verticies [3] = new Vector3 (100, 100, 0);
verticies [4] = new Vector3 (0, 0, 100);
verticies [5] = new Vector3 (100, 0, 100);
verticies [6] = new Vector3 (0, 100, 100);
verticies [7] = new Vector3 (100, 100, 100);
mesh.vertices = verticies;
int[] tris = new int[36];
tris = new[] { 0, 2, 1, 2, 3, 1, 4, 2, 0, 6, 2, 4, 4, 6, 5, 6, 7, 5, 1, 3, 5, 3, 7, 5, 0, 4, 1, 4, 5, 1, 2, 6, 3, 6, 7, 3 };
mesh.triangles = tris;
mesh.RecalculateNormals ();
MeshFilter meshFilter = cube.AddComponent<MeshFilter> ();
meshFilter.mesh = mesh;
MeshRenderer meshRenderer = cube.AddComponent<MeshRenderer> ();
Texture2D texture = new Texture2D (1, 1);
texture.SetPixels (new[] { Color.blue });
meshRenderer.sharedMaterial = new Material (meshShader);
meshRenderer.sharedMaterial.mainTexture = texture;
cube.SetActive (true);
}
if (GameObject.Find ("Hopefully a Pyramid") == null) {
GameObject pyramid = new GameObject ("Hopefully a Pyramid");
Mesh mesh = new Mesh ();
Vector3[] verticies = new Vector3[5];
verticies [0] = new Vector3 (0, 0, 0);
verticies [1] = new Vector3 (100, 0, 0);
verticies [2] = new Vector3 (0, 0, 100);
verticies [3] = new Vector3 (100, 0, 100);
verticies [4] = new Vector3 (50, 100, 50);
mesh.vertices = verticies;
int[] tris = new int[18];
tris = new[] { 0, 2, 1, 2, 3, 1, 0, 4, 1, 2, 4, 0, 3, 4, 2, 3, 4, 1 };
mesh.triangles = tris;
mesh.RecalculateNormals ();
MeshFilter meshFilter = pyramid.AddComponent<MeshFilter> ();
meshFilter.mesh = mesh;
MeshRenderer meshRenderer = pyramid.AddComponent<MeshRenderer> ();
Texture2D texture = new Texture2D (1, 1);
texture.SetPixels (new[] { Color.blue });
meshRenderer.sharedMaterial = new Material (meshShader);
meshRenderer.sharedMaterial.mainTexture = texture;
pyramid.SetActive (true);
}
The indices (the integers in the triangle array) of your mesh are in a wrong order. They need to be clockwise from the direction you look at the face. A simple tip, if they appear wong, just flip 2 indices of the triangle in question.
This is your cube.
tris = new[] {
0, 2, 1, 2, 3, 1,
4, 2, 0, 6, 2, 4,
4, 5, 6, 6, 5, 7,
1, 3, 5, 3, 7, 5,
0, 1, 4, 4, 1, 5,
2, 6, 3, 6, 7, 3
};
If you wonder why it has such a odd shading, then create a cube out of 24 vertices and see the difference. This is because the normals on the face depend on the normals of the vertices making it up. (same goes for vertex colors and uvs). So if you want sharp edges, differnt colors from vertexes on adjacent faces or a uv seam, you will need individual vertices for the faces/edges (so 2 verts in one spot), since you can only set those features on vertices.