Exemplo n.º 1
0
    public void exportScaleHeightMap()
    {
        string fileName = string.Format("{0}/{1}.png", Application.dataPath, "heightmap");

        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);
        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;
        Texture2D heightMap = new Texture2D(w, h, TextureFormat.BGRA32, true);
        Color     color     = new Color(0, 0, 0, 0);

        for (int idy = 0; idy < h; idy++)
        {
            for (int idx = 0; idx < w; idx++)
            {
                //tVertices[y * w + x] = Vector3.Scale(meshScale, new Vector3(x, (int)(tData[(int)(x * tRes), (int)(y * tRes)]), y)) + terrainPos;
                height = tData[(int)(idy * tRes), (int)(idx * tRes)];
                color  = new Color(height, height, height, height);
                heightMap.SetPixel(idx, idy, color);
            }
        }

        UtilPath.saveTex2File(heightMap, fileName);
    }
Exemplo n.º 2
0
    public void exportHeightMap()
    {
        string  fileName  = string.Format("{0}/Resources/Materials/Textures/Terrain/{1}_{2}.png", Application.dataPath, mHeightMapNamePrefix, mTerrainId);
        int     w         = terrainData.heightmapWidth;
        int     h         = terrainData.heightmapHeight;
        Vector3 meshScale = terrainData.size;

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

        //float fx = 0;
        //float fy = 0;

        Color     color     = new Color(0, 0, 0, 0);
        float     height    = 0;
        Texture2D heightMap = new Texture2D(w, h, TextureFormat.BGRA32, true);

        for (int idy = 0; idy < h; ++idy)
        {
            for (int idx = 0; idx < w; ++idx)
            {
                height = tData[idy, idx];
                // 这个是插值的高度
                //fx = ((float)idx) / w;
                //fy = ((float)idy) / h;
                //height = terrainData.GetInterpolatedHeight(fx, fy);
                //height /= meshScale.z;
                color = new Color(height, height, height, height);
                heightMap.SetPixel(idx, idy, color);
            }
        }

        UtilPath.saveTex2File(heightMap, fileName);
    }
Exemplo n.º 3
0
    public void exportSplatTexture_NotRun()
    {
        string         path       = Application.streamingAssetsPath;
        int            idx        = 0;
        SplatPrototype splatLayer = null;
        Texture2D      writeTex   = null;
        Color          color;

        for (idx = 0; idx < terrainData.splatPrototypes.Length; ++idx)
        {
            splatLayer = terrainData.splatPrototypes[idx];
            writeTex   = new Texture2D(splatLayer.texture.width, splatLayer.texture.height, TextureFormat.RGB24, false);

            for (int imageY = 0; imageY < splatLayer.texture.height; ++imageY)
            {
                for (int imageX = 0; imageX < splatLayer.texture.width; ++imageX)
                {
                    // 这个纹理是不能读写的,需要使用 AssetDatabase.GetAssetPath 读取纹理目录
                    color = splatLayer.texture.GetPixel(imageX, imageY);
                    writeTex.SetPixel(imageX, imageY, color);
                }
            }
            UtilPath.saveTex2File(splatLayer.texture, path + "/SplatTextures" + idx + ".png");
        }
    }
Exemplo n.º 4
0
    public void exportAlphaMap()
    {
        float[,,] splatmapData = terrainData.GetAlphamaps(0, 0, terrainData.alphamapWidth, terrainData.alphamapHeight);

        int alphamapWidth  = terrainData.alphamapWidth;
        int alphamapHeight = terrainData.alphamapHeight;
        int alphamapLayers = terrainData.alphamapLayers;

        int channel    = 4;
        int channelIdx = 0;

        float r = 0;
        float g = 0;
        float b = 0;
        float a = 0;

        string    fileName = "";
        Color     color    = new Color(0, 0, 0, 0);
        Texture2D alphaMap = new Texture2D(alphamapWidth, alphamapHeight, TextureFormat.BGRA32, true);

        int imageIdx = 0;

        while (channelIdx < alphamapLayers)
        {
            r = 0;
            g = 0;
            b = 0;
            a = 0;

            for (int idy = 0; idy < alphamapHeight; ++idy)
            {
                for (int idx = 0; idx < alphamapWidth; ++idx)
                {
                    r = splatmapData[idx, alphamapHeight - 1 - idy, channelIdx];
                    if (channelIdx + 1 < alphamapLayers)
                    {
                        g = splatmapData[idx, alphamapHeight - 1 - idy, channelIdx + 1];
                    }
                    if (channelIdx + 2 < alphamapLayers)
                    {
                        b = splatmapData[idx, alphamapHeight - 1 - idy, channelIdx + 2];
                    }
                    if (channelIdx + 3 < alphamapLayers)
                    {
                        a = splatmapData[idx, alphamapHeight - 1 - idy, channelIdx + 3];
                    }

                    color = new Color(r, g, b, a);
                    alphaMap.SetPixel(idx, idy, color);
                }
            }

            fileName = string.Format("{0}/{1}_{2}.png", Application.dataPath, "AlphaMap", imageIdx);
            UtilPath.saveTex2File(alphaMap, fileName);

            imageIdx   += 1;
            channelIdx += channel;
        }
    }
Exemplo n.º 5
0
    // 导出 AlphaMap
    public void exportAlphaMap_a()
    {
        string fileName = "";

        Texture2D[] alphamapTextures = terrainData.alphamapTextures;
        for (int idx = 0; idx < alphamapTextures.Length; ++idx)
        {
            fileName = string.Format("{0}/{1}_{2}.png", Application.dataPath, "AlphaMap", idx);
            UtilPath.saveTex2File(alphamapTextures[idx], fileName);
        }
    }
Exemplo n.º 6
0
    public void exportAlphaTexture()
    {
        string path = "";
        int    idx  = 0;

        while (idx < terrainData.alphamapTextures.Length)
        {
            path = string.Format("{0}/Resources/Materials/Textures/Terrain/{1}_{2}_{3}.png", Application.dataPath, mAlphaMapNamePrefix, mTerrainId, idx);
            UtilPath.saveTex2File(terrainData.alphamapTextures[0], path);
            ++idx;
        }
    }