[Unity] Mesh网格位置偏移

需求:

在一个绘制好的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();
}

这个问题出现的原因是少了坐标转换

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值