public void GenerateTextureMap(uint uiSize, ushort maxHeight, ushort minHeight) { if (mTerrainTiles.Count <= 0) { return; } mTerrainTexture = null; int tHeightStride = maxHeight / mTerrainTiles.Count; float[] fBend = new float[mTerrainTiles.Count]; //注意,这里的区域是互相重叠的 int lastHeight = -1; for (int i = 0; i < mTerrainTiles.Count; ++i) { CTerrainTile terrainTile = mTerrainTiles[i]; //lastHeight += 1; terrainTile.lowHeight = lastHeight + 1; lastHeight += tHeightStride; terrainTile.optimalHeight = lastHeight; terrainTile.highHeight = (lastHeight - terrainTile.lowHeight) + lastHeight; } for (int i = 0; i < mTerrainTiles.Count; ++i) { CTerrainTile terrainTile = mTerrainTiles[i]; string log = string.Format("Tile Type:{0}|lowHeight:{1}|optimalHeight:{2}|highHeight:{3}", terrainTile.TileType.ToString(), terrainTile.lowHeight, terrainTile.optimalHeight, terrainTile.highHeight ); Debug.Log(log); } mTerrainTexture = new Texture2D((int)uiSize, (int)uiSize, TextureFormat.RGBA32, false); CUtility.SetTextureReadble(mTerrainTexture, true); float fMapRatio = (float)mHeightData.mSize / uiSize; for (int z = 0; z < uiSize; ++z) { for (int x = 0; x < uiSize; ++x) { Color totalColor = new Color(); for (int i = 0; i < mTerrainTiles.Count; ++i) { CTerrainTile tile = mTerrainTiles[i]; if (tile.mTileTexture == null) { continue; } int uiTexX = x; int uiTexZ = z; //CUtility.SetTextureReadble(tile.mTileTexture, true); GetTexCoords(tile.mTileTexture, ref uiTexX, ref uiTexZ); Color color = tile.mTileTexture.GetPixel(uiTexX, uiTexZ); fBend[i] = RegionPercent(tile.TileType, Limit(InterpolateHeight(x, z, fMapRatio), maxHeight, minHeight)); totalColor.r = Mathf.Min(color.r * fBend[i] + totalColor.r, 1.0f); totalColor.g = Mathf.Min(color.g * fBend[i] + totalColor.g, 1.0f); totalColor.b = Mathf.Min(color.b * fBend[i] + totalColor.b, 1.0f); totalColor.a = 1.0f; //CUtility.SetTextureReadble(tile.mTileTexture, false); }// //输出到纹理上 if (totalColor.r == 0.0f && totalColor.g == 0.0f && totalColor.b == 0.0f) { ushort xHeight = (ushort)(x * fMapRatio); ushort zHeight = (ushort)(z * fMapRatio); Debug.Log(string.Format("Color is Black | uiX:{0}|uiZ:{1}|hX:{2}|hZ:{3}|h:{4}", x, z, xHeight, zHeight, GetTrueHeightAtPoint(xHeight, zHeight))); } mTerrainTexture.SetPixel(x, z, totalColor); } } //OpenGL纹理的操作 mTerrainTexture.Apply(); CUtility.SetTextureReadble(mTerrainTexture, false); //string filePath = string.Format("{0}/{1}", Application.dataPath, "Runtime_TerrainTexture.png"); //File.WriteAllBytes(filePath,mTerrainTexture.EncodeToPNG()); //AssetDatabase.ImportAsset(filePath, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate); //AssetDatabase.SaveAssets(); } //