如何在鼠标位置进行光线投射,然后获取要在地形中使用的坐标?

时间:2019-01-25 00:24:59

标签: unity3d

我正在制作运行时地形编辑器,而射线广播坏了。如何在鼠标位置进行光线投射,然后获取可用于扩大地形的坐标?

我尝试了鼠标的原始位置,这只是有点不准确,然后尝试了Input.GetAxis()Camera.main.ScreenPointToRay()

void Update () 
{
    if (Input.GetMouseButton(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        print("Got Ray" + ray.ToString());
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.gameObject.GetComponent<Terrain>())
            {
                Terrain terrain 
                hit.collider.gameObject.GetComponent<Terrain>();
                TerrainData data = terrain.terrainData;
                float height = data.GetHeight(
                    (int)Math.Round((double)Camera.main.ScreenToWorldPoint(Input.mousePosition).x), 
                    (int)Math.Round((double)Camera.main.ScreenToWorldPoint(Input.mousePosition).y));

                data.SetHeightsDelayLOD((int)Math.Round(
                    (double)Camera.main.ScreenToWorldPoint( Input.mousePosition).x), 
                    (int)Math.Round((double)Camera.main.ScreenToWorldPoint(Input.mousePosition).y), 
                    new float[3, 3] 
                    { 
                        { height + 1.0f / 240000.0f, height + 1.0f / 240000.0f, height + 1.0f / 240000.0f }, 
                        { height + 1.0f / 240000.0f, height + 1.0f / 240000.0f, height + 1.0f / 240000.0f }, 
                        { height + 1.0f / 240000.0f, height + 1.0f / 2400.0f, height + 1.0f / 2400.0f } 
                    });

                terrain.ApplyDelayedHeightmapModification();
            }
        }
    }
}

我希望地形会在鼠标位置处增长,但根本不会增长。

1 个答案:

答案 0 :(得分:0)

Terrain data.GetHeight

  

xBase :要检索的高度图样本的第一个x index

-> GetHeight不需要世界坐标,但需要数据索引!


您必须计算哪个索引位于射线的击中点(而不是鼠标位置):

if (Input.GetMouseButton(0))
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    print("Got Ray" + ray.ToString());
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
        if (hit.collider.gameObject.GetComponent<Terrain>())
        {
            Terrain terrain 
            hit.collider.gameObject.GetComponent<Terrain>();

            TerrainData data = terrain.terrainData;

            //
            // !!! Mouse position to terrain coordinates
            //
            var point = hit.point;
            var xResolution = data.heightmapWidth;
            var zResolution = data.heightmapHeight;
            var terX =(int)((point.x / data.size.x) * xResolution);
            var terZ =(int)((point.z / data.size.z) * zResolution);

            var height = data.GetHeight(terX, terZ);

            // Same when you set values
            var newValues = new float[3, 3] 
            { 
                { height + 1.0f / 240000.0f, height + 1.0f / 240000.0f, height + 1.0f / 240000.0f }, 
                { height + 1.0f / 240000.0f, height + 1.0f / 240000.0f, height + 1.0f / 240000.0f }, 
                { height + 1.0f / 240000.0f, height + 1.0f / 2400.0f, height + 1.0f / 2400.0f } 
            };
            data.SetHeightsDelayLOD(terX, terZ, newValues);
            terrain.ApplyDelayedHeightmapModification();
        }
    }
}

Source