需求:
在一个绘制好的mesh上面,在鼠标所指的那个六边形处,再画出来一个红色的六边形。
问题:
鼠标的位置是A,gameobject也被设置在A,但是六边形Mesh却画在了B
代码:
private void SetLight(int x, int y, int z)
{
LightMesh.Clear();
float posx = z % 2 * MC.innerRadius + MC.innerRadius * x * 2;
float posz = MC.outerRadius * z * 1.5f;
Vector3 center = new Vector3(posx, 0, posz);
light.position = center + Vector3.up;
center = light.InverseTransformPoint(center) + Vector3.up * 0.1f; // 坐标转换
vertices.Clear();
indexs.Clear();
colors.Clear();
for (int i = 0; i < 6; i++)
{
int vertexIndex = vertices.Count;
vertices.Add(center);
vertices.Add(center + MC.corners[i]);
vertices.Add(center + MC.corners[i + 1]);
Debug.DrawRay(center + MC.corners[i], Vector3.up * 10, Color.blue, 2);
Debug.DrawRay(center + MC.corners[i + 1], Vector3.up * 10, Color.blue, 2);
indexs.Add(vertexIndex);
indexs.Add(vertexIndex + 1);
indexs.Add(vertexIndex + 2);
colors.Add(Color.red);
colors.Add(Color.red);
colors.Add(Color.red);
}
LightMesh.vertices = vertices.ToArray();
LightMesh.colors = colors.ToArray();
LightMesh.triangles = indexs.ToArray();
LightMesh.RecalculateNormals();
}
这个问题出现的原因是少了坐标转换