上一篇 :Lightmap之动态加载单个物件和其对应的Lightmap
本系列前两篇文章,实际上说明了两点:
1.Lightmap是可以根据脚本动态切换。适用于同一场景白天,黑夜的变换。
2.Lightmap是可以分割,以物件为单位, 每个物件单独保存各自的Lightmap。当然这种分割只是逻辑上的分割,实际并没有分割,而是存储了各自的偏移量和索引。适用于大场景的动态加载物件,能确保物件的Lightmap信息不丢失。
了解了上面两点,那另外一种实际应用就迎刃而解了。这种应用就是,大地型(terrain)的切割。场景比较大,大地形的需要切割,按九宫格之类的形式加载。那要解决这个问题,需要实现两点:
1.Terrain的切割
2.切割后的各个Terrain的Lightmap信息随Terrain单独保存。
先跳过第1点,第2点其实与上一篇 Lightmap之动态加载单个物件和其对应的Lightmap 很类似,只是之前处理的是Mesh,这里要处理的是Terrain。实现需要的关键信息原来MeshRenderer上带的索引和偏移量,在Terrain中也是带的有的。因此只要代码做小小的调整,把MeshRenderer换成Terrain。
回过来说第1点,如何切割Terrain? Terrain组件中包含一个可访问的TerrainData的数据结构,有了它,我们可以就是对Terrain进行切割,构建新的分块Terrain。下面的代码是把一块地形切成4*4供16个分块地形。
//开始分割地形
[MenuItem("Terrain/Slicing")]
private static void Slicing()
{
Terrain terrain = GameObject.FindObjectOfType<Terrain>();
if (terrain == null)
{
Debug.LogError("找不到地形!");
return;
}
if (Directory.Exists(TerrainSavePath)) Directory.Delete(TerrainSavePath, true);
Directory.CreateDirectory(TerrainSavePath);
TerrainData terrainData = terrain.terrainData;
//这里我分割的宽和长度是一样的.这里求出循环次数,TerrainLoad.SIZE要生成的地形宽度,长度相同
//高度地图的分辨率只能是2的N次幂加1,所以SLICING_SIZE必须为2的N次幂
//SLICING_SIZE = (int)terrainData.size.x / TerrainLoad.SIZE;
SLICING_SIZE = 4;
Vector3 oldSize = terrainData.size;
Debug.LogError("terrainData.size " + terrainData.size + " " +
terrainData.heightmapWidth + " " + terrainData.heightmapHeight);
//得到新地图分辨率
int newHeightmapResolution = (terrainData.heightmapResolution - 1) / SLICING_SIZE;
int newAlphamapResolution = terrainData.alphamapResolution / SLICING_SIZE;
int newbaseMapResolution = terrainData.baseMapResolution / SLICING_SIZE;
SplatPrototype[] splatProtos = terrainData.splatPrototypes;
//循环宽和长,生成小块地形
for (int x = 0; x < SLICING_SIZE; ++x)
{
for (int y = 0; y < SLICING_SIZE; ++y)
{
//创建资源
TerrainData newData = new TerrainData();
string terrainName = TerrainSavePath + TerrainLoad.TERRAIN_NAME + y + "_" + x + ".asset";
AssetDatabase.CreateAsset(newData, TerrainSavePath + TerrainLoad.TERRAIN_NAME + y + "_" + x + ".asset");
EditorUtility.DisplayProgressBar("正在分割地形", terrainName, (float)(x * SLICING_SIZE + y) / (float)(SLICING_SIZE * SLICING_SIZE));
//设置分辨率参数
newData.heightmapResolution = (terrainData.heightmapResolution - 1) / SLICING_SIZE;
newData.alphamapResolution = terrainData.alphamapResolution / SLICING_SIZE;
newData.baseMapResolution = terrainData.baseMapResolution / SLICING_SIZE;
//设置大小
newData.size = new Vector3(oldSize.x / SLICING_SIZE, oldSize.y, oldSize.z / SLICING_SIZE);
//设置地形原型
SplatPrototype[] newSplats = new SplatPrototype[splatProtos.Length];
for (int i = 0; i < splatProtos.Length; ++i)
{
newSplats[i] = new SplatPrototype();
newSplats[i].texture = splatProtos[i].texture;
newSplats[i].tileSize = splatProtos[i].tileSize;
float offsetX = (newData.size.x * x) % splatProtos[i].tileSize.x + splatProtos[i].tileOffset.x;
float offsetY = (newData.size.z * y) % splatProtos[i].tileSize.y + splatProtos[i].tileOffset.y;
newSplats[i].tileOffset = new Vector2(offsetX, offsetY);
}
newData.splatPrototypes = newSplats;
//设置混合贴图
float[,,] alphamap = new float[newAlphamapResolution, newAlphamapResolution, splatProtos.Length];
alphamap = terrainData.GetAlphamaps(x * newData.alphamapWidth, y * newData.alphamapHeight, newData.alphamapWidth, newData.alphamapHeight);
newData.SetAlphamaps(0, 0, alphamap);
//设置高度
int xBase = terrainData.heightmapWidth / SLICING_SIZE;
int yBase = terrainData.heightmapHeight / SLICING_SIZE;
float[,] height = terrainData.GetHeights(xBase * x, yBase * y, xBase + 1, yBase + 1);
newData.SetHeights(0, 0, height);
}
}
EditorUtility.ClearProgressBar();
}