- Home /
Drawing a cube by a new Mesh
Hi guys, I am with a very simple problem, but it is blocking my work. I am instantiating a cube with a new Mesh, setting manually all the vertices, uv's, triangles and normals. This is my code:
using UnityEngine;
using System.Collections;
public class cube : MonoBehaviour {
public Vector3 index;
void Start(){
mesh.Clear();
mesh.vertices = new Vector3[] {new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 0, 0), //front
new Vector3(1, 0, 1), new Vector3(1, 1, 1), new Vector3(0, 1, 1), new Vector3(0, 0, 1), //back
new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(1, 1, 1), new Vector3(1, 0, 1), //right
new Vector3(0, 0, 1), new Vector3(0, 1, 1), new Vector3(0, 1, 0), new Vector3(0, 0, 0), //left
new Vector3(0, 1, 0), new Vector3(0, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 0), //top
new Vector3(0, 0, 0), new Vector3(0, 0, 1), new Vector3(1, 0, 1), new Vector3(1, 0, 0)}; //bottom
mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2 (1, 0),
new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2 (1, 0),
new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2 (1, 0),
new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2 (1, 0),
new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2 (1, 0),
new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2 (1, 0)};
mesh.triangles = new int[] {0,1,2,0,2,3,4,5,6,4,6,7,8,9,10,8,10,11,12,13,14,12,14,15,16,17,18,16,18,19,20,21,22,20,22,23};
mesh.normals = new Vector3[] {new Vector3( 0, 0,-1),new Vector3( 0, 0,-1),new Vector3( 0, 0,-1),new Vector3( 0, 0,-1), //front
new Vector3( 0, 0, 1),new Vector3( 0, 0, 1),new Vector3( 0, 0, 1),new Vector3( 0, 0, 1), //back
new Vector3( 1, 0, 0),new Vector3( 1, 0, 0),new Vector3( 1, 0, 0),new Vector3( 1, 0, 0), //right
new Vector3(-1, 0, 0),new Vector3(-1, 0, 0),new Vector3(-1, 0, 0),new Vector3(-1, 0, 0), //left
new Vector3( 0, 1, 0),new Vector3( 0, 1, 0),new Vector3( 0, 1, 0),new Vector3( 0, 1, 0), //top
new Vector3( 0,-1, 0),new Vector3( 0,-1, 0),new Vector3( 0,-1, 0),new Vector3( 0,-1, 0)}; //bottom
}
}
The problem is: the bottom face normals are inverted, and i can't invert them. This is the cube made by this code, with the normals drawn on Gizmos.
The normals settings are correct. This is the same cube, with only the bottom face drawn.
Then the normals are really bugged, not simply inverted. In the image1, the bottom normals are settled like the posted code. The image2 is the bottom normals with the inverted normals.
I don't know why it's happening. Please help me!! thanks!
Answer by joonturbo · Nov 16, 2013 at 11:26 AM
It seems like the problem is your triangle is generated facing the wrong way. Normals are mainly used to calculate light, but the triangles are what decide whether something faces one way or the other. Try changing 20,21,22 and 20,22,23 to 20,22,21 and 20,23,22
Answer by Berenger · Jan 28, 2013 at 05:23 PM
Sounds like the perfect time to bring out my freshly made wiki entry : http://wiki.unity3d.com/index.php/ProceduralPrimitives
drawing the cube with 8 vertices the bottom render perfectly. But, i need to have the faces separated to mapping every face on diferent regions on the texture map.
Your answer
Follow this Question
Related Questions
How to set mesh normals per face? 2 Answers
Sphere made of cubes algorithm 4 Answers
issues with mobile build 0 Answers
Character always standing normal to mesh - without using physics system 2 Answers
My meshes arent loading properly. 1 Answer