Generated Mesh not being lit?
I'm creating a simple mesh (literally just a one sided plane). I'm applying a custom material (default settings from creation). Then I'm applying an 'advanced' texture which has Read/Write enabled and wrap mode set to repeat.
this is the code I'm using specifically, to generate and set-up the mesh
public Mesh GenerateMesh()
{
vertices = new Vector3[] { //positions are off by 0.5f because coordinates point to the CENTER of a tile, not a corner
new Vector3( -0.5f, -0.5f, 0), //0
new Vector3( -0.5f, height - 0.5f, 0), //1
new Vector3( width - 0.5f, height - 0.5f, 0), //2
new Vector3( width - 0.5f, -0.5f, 0) //3
};
uv = new Vector2[]
{
new Vector2(0, 0), //0
new Vector2(0, 1), //1
new Vector2(1, 1), //2
new Vector2(1, 0) //3
};
triangles = new int[]
{
3, 0, 1,
1, 2, 3
};
roomMesh = new Mesh();
roomMesh.vertices = vertices;
roomMesh.uv = uv;
roomMesh.triangles = triangles;
MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
meshFilter.mesh = roomMesh;
MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
meshRenderer.material = Sys.LoadMaterial("Materials/floor_material");
meshRenderer.material.SetTexture("_MainTex", Sys.LoadTexture(texPath));
meshRenderer.material.SetTextureScale("_MainTex", new Vector2(width, height));
return roomMesh;
}
All the textures used are fairly bright, vibrant colors, however when I run the game they look dark
I've tried adding local lights and directional lights, set for runtime lighting (baking is not an option as I'm generating the floors at run time)
Surely there's something I'm missing? Why won't these planes light up?
I could switch the shader to an unlit shader, but Ideally I'd like to do lighting in the scene to simulate torches, lanterns, etc.
Try GetComponent<Renderer>()
ins$$anonymous$$d of GetComponent<$$anonymous$$eshRenderer>()
.
What shader is the floor_material material using? Also its better to add a private serialized $$anonymous$$aterial variable ins$$anonymous$$d, assign your floor material to the above made variable in the inspector and assign the renderer's material to be that variable ins$$anonymous$$d of dynamically looking for it each time.
$$anonymous$$aybe you need t recalculate normals? room$$anonymous$$esh.RecalculateNormals(); or set them yourself as it is likely they should all be -Vector3.forward for a 2D game!
Thank you Scribe, recalculating the normals was exactly the fix!
Answer by smugleafdev · Apr 01, 2021 at 03:22 AM
It would be nice if people posted actual replies instead of comments that are hidden and for some reason rendering full of $$anonymous$$.
You need to call roomMesh.RecalculateNormals(); and then it will render properly.