Procedurally generated mesh having some faces that don't receive light
Hi,
I'm having some troubles using lights on a procedurally generated mesh. It seems that depending on the point light position, some faces will not receive light, even if they are fully exposed.
This is how I generate the mesh :
using UnityEngine;
using System.Collections;
public class MeshGen : MonoBehaviour {
public GameObject obj;
void Start () {
generate();
}
void generate(){
Vector3 pt0 = new Vector3(0,0,0);
Vector3 pt1 = new Vector3(0,1,0);
Vector3 pt2 = new Vector3(1,1,0);
Vector3 cross = GetNormal (pt0, pt1, pt2);
Vector3 pt3 = pt0+cross;
Vector3 pt4 = pt1+cross;
Vector3 pt5 = pt2+cross;
Vector3[] newVertices = {pt2, pt1, pt0,
pt3, pt4, pt5,
pt0, pt1, pt3,
pt1, pt4, pt3,
pt0, pt3, pt2,
pt2, pt3, pt5,
pt1, pt2, pt4,
pt2, pt5, pt4};
int[] newTriangles = {0,1,2,
3,4,5,
6,7,8,
9,10,11,
12,13,14,
15,16,17,
18,19,20,
21,22,23
};
GameObject myFace = (GameObject) Instantiate(obj, new Vector3(0,0,0), Quaternion.identity);
Mesh mesh = new Mesh();
myFace.GetComponent<MeshFilter>().mesh = mesh;
mesh.vertices = newVertices;
Vector3[] vertices = mesh.vertices;
Vector2[] uvs = new Vector2[vertices.Length];
for (int i=0; i < uvs.Length; i++) {
uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
}
mesh.uv = uvs;
mesh.triangles = newTriangles;
}
public static Vector3 GetNormal(Vector3 a, Vector3 b, Vector3 c) {
Vector3 side1 = b - a;
Vector3 side2 = c - a;
return Vector3.Cross(side1, side2).normalized;
}
}
And this is the result (the generated mesh is on the left) :
Could someone tell me what I'm doing wrong please ?
Thanks a lot.
I think you're forgetting the call to RecalculateNormals at the end.
Answer by OswaldP · Feb 01, 2016 at 09:36 PM
I found the solution to my problem, I only needed to recalculate the normals of the mesh at the end of the mesh generation script using :
mesh.RecalculateNormals();
Hope it will help someone else !
Your answer
Follow this Question
Related Questions
How is a Mesh Built? 1 Answer
Can't get submesh to work 1 Answer
Issue with Modifying Vertices of multiple Game objects 0 Answers
Procedural Mesh Problems 0 Answers
Find the volume of an object that is underneath a mesh 0 Answers