예제 #1
0
    // 导出高度数据
    public void exportHeightData()
    {
        string fileName = string.Format("{0}/Resources/TerrainData/{1}_{2}.bytes", Application.dataPath, mHeightMapNamePrefix, mTerrainId);

        int     w         = terrainData.heightmapWidth;
        int     h         = terrainData.heightmapHeight;
        Vector3 meshScale = terrainData.size;
        float   tRes      = Mathf.Pow(2, (int)saveResolution);

        meshScale = new Vector3(meshScale.x / (w - 1) * tRes, meshScale.y, meshScale.z / (h - 1) * tRes);
        Vector2 uvScale = new Vector2(1.0f / (w - 1), 1.0f / (h - 1));

        float[,] tData = terrainData.GetHeights(0, 0, w, h);

        w = (int)((w - 1) / tRes + 1);
        h = (int)((h - 1) / tRes + 1);
        Vector3[] tVertices = new Vector3[w * h];

        float      height = 0;
        ByteBuffer buffer = new ByteBuffer();

        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                height = tData[(int)(y * tRes), (int)(x * tRes)];
                buffer.writeFloat(height);
            }
        }

        UtilPath.saveByte2File(fileName, buffer.dynBuff.buff);
    }