Question by
dovker · Sep 29, 2019 at 08:40 PM ·
lightinggenerationmeshfiltermesh renderergenerate
Strange lighting behaviour when generating a mesh.
Hello,
I am pretty new to unity, coming from monogame.
I recently tried to generate this mesh and light it up, but it strangely acts like there's a curve to it.
I have used mesh.RecalculateNormals(); just for you to know. Here's the video:
And code:
Mesh mesh;
Vector3[] vertices; //Vertex array
int[] triangles; //Index array
private float width; //Texture Width
private float height; //Texture Height
public int tSize = 16; //Texture size for reference
public float splitLine = 8; //Fold Line
private float splitNorm; //Fold line Normalized
public Texture tex; //Texture
Vector2[] uvs; //UV array
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
GetComponent<MeshRenderer>().material.mainTexture = tex;
width = tex.width / tSize;
height = tex.height / tSize;
splitNorm = splitLine / tex.height;
CreateShape();
UpdateMesh();
}
void CreateShape()
{
vertices = new Vector3[]
{
new Vector3(0, 0, -height),
new Vector3(0, -height * splitNorm, -height),
new Vector3(width, -height * splitNorm, -height),
new Vector3(width, 0, -height),
new Vector3(0, -height, 0),
new Vector3(width, -height, 0),
};
triangles = new int[]
{
0,2,1,
0,3,2,
2,5,1,
1,5,4,
};
uvs = new Vector2[]
{
new Vector2(0, 0),
new Vector2(0, height * splitNorm),
new Vector2(width, height * splitNorm),
new Vector2(width, 0),
new Vector2(0, height),
new Vector2(width, height),
};
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
Comment