- Home /
Mesh not receiving shadows?
Hi,
I am new to Unity (this is my first project!), and I am running into some difficulties with a mesh not receiving shadows.
I am making a mesh for the graph of a function, and it seems to be working well, but my graph is very difficult to see (it is all the same color): 
I am thinking this is because it is not receiving shadows from itself. I noticed that it is in fact not receiving shadows from anything. I am not sure why, because I have set it to receive shadows:
Here is the code I used to generate the mesh:
void Start () {
verts = new Vector3[resolution * resolution];
tris = new int[12 * resolution * resolution];
Vector2[] uv = new Vector2[resolution * resolution];
float step = 10f / (resolution-1);
for (int i = 0; i < resolution; i++)
{
for (int j = 0; j < resolution; j++)
{
verts[resolution * i + j] = new Vector3(step * i - 5f, Mathf.Sin(100 * i), step * j - 5f);
if (i != resolution-1){
if (j != resolution - 1)
{
//print(resolution * (i + 1) + j + 1);
tris[12 * (resolution * i + j)] = resolution * i + j;
tris[12 * (resolution * i + j) + 1] = resolution * i + j + 1;
tris[12 * (resolution * i + j) + 2] = resolution * (i + 1) + j;
tris[12 * (resolution * i + j) + 3] = resolution * (i + 1) + j;
tris[12 * (resolution * i + j) + 4] = resolution * i + j + 1;
tris[12 * (resolution * i + j) + 5] = resolution * (i + 1) + j + 1;
tris[12 * (resolution * i + j) + 6] = resolution * i + j;
tris[12 * (resolution * i + j) + 7] = resolution * (i + 1) + j;
tris[12 * (resolution * i + j) + 8] = resolution * i + j+1;
tris[12 * (resolution * i + j) + 9] = resolution * (i + 1) + j;
tris[12 * (resolution * i + j) + 10] = resolution * (i + 1) +j + 1;
tris[12 * (resolution * i + j) + 11] = resolution * i + j + 1;
}
}
uv[resolution * i + j] = new Vector2((float)i / resolution, (float)j / resolution);
}
}
mesh = new Mesh();
mesh.vertices = verts;
mesh.uv = uv;
mesh.triangles = tris;
GameObject game = new GameObject("Mesh");
MeshFilter meshFilter = game.AddComponent<MeshFilter>();
meshFilter.mesh = mesh;
MeshRenderer meshRenderer = game.AddComponent<MeshRenderer>();
meshRenderer.material = AssetDatabase.GetBuiltinExtraResource<Material>("Default-Material.mat");
}
Is there some way to get the mesh to receive shadows? Any help would be greatly appreciated!
I just found the solution! mesh.RecalculateNormals() does the trick! :)
Your answer
Follow this Question
Related Questions
My mesh isnt creating any shadows 1 Answer
Imported mesh artifacts in casted shadow? 0 Answers
weird shadows 2 Answers
Problem in rendering meshes 2 Answers