0

我正在制作一个运行时地形编辑器,但我的光线投射已损坏。如何在鼠标位置进行光线投射,然后获取可用于扩展地形的坐标?

我尝试了原始鼠标位置,这只是稍微不准确,然后我尝试了Input.GetAxis()and 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();
            }
        }
    }
}

我预计地形会在鼠标位置增长,但它根本没有增长。

4

1 回答 1

0

地形 data.GetHeight

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

->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();
        }
    }
}

来源

于 2019-01-25T07:06:11.843 回答