unity3d 发布后动态修改地形高度源码

//tOdO:
                    if (m_TerrainData == null) {
                        m_Terrain= FindObjectOfType();
                        m_TerrainData =m_Terrain.terrainData;
                    }
                    else {
                        //FindObjectOfType().SampleHeight(new Vector3(h.point.x, h.point.y + 0.1f, h.point.z));
                        Vector3 terrainLocalPos = h.point-m_Terrain.transform.position;
                        Vector2 controlPos = new Vector2(terrainLocalPos.x / m_TerrainData.size.x * m_TerrainData.heightmapWidth, terrainLocalPos.z / m_TerrainData.size.z * m_TerrainData.heightmapHeight);
                        float addHeight = 1.0f;
                        float oldHeight = m_TerrainData.GetHeight((int)controlPos.x, (int)controlPos.y);
                        float[,] newHeightData = new float[16, 16]; //new float[1, 1] { { (oldHeight + addHeight) / m_TerrainData.heightmapScale.y } };
                        for (int i = 0; i < 16; i++) {
                            for (int j = 0; j < 16; j++) {
                                newHeightData[i, j] = (oldHeight + addHeight) / m_TerrainData.heightmapScale.y;
                            }
                        }
                            m_TerrainData.SetHeights((int)controlPos.x, (int)controlPos.y, newHeightData);
                        //
                        //float[,] oldHeightData = terrainData.GetHeights((int)controlPos.x, (int)controlPos.y, 1, 1);
                        //float[,] newHeightData = new float[1, 1] { { addHeight / terrainData.heightmapScale.y + oldHeightData[0, 0] } };
                        //terrainData.SetHeights((int)controlPos.x, (int)controlPos.y, newHeightData);
                    }

你可能感兴趣的:(Unity3D)