您的位置:首页 > 移动开发 > Unity3D

Unity3D分割地形Terrain

2017-04-09 16:51 906 查看
在制作地形的时候通常是直接刷出整个地形,但是在实际使用中也许地形过大,我们不能直接把整个地形完全加载,这样对内存的消耗很高,所以有时需要一小块一小块的加载地形.这时就需要把制作好的地形分割成几块后,再来动态加载.

首先我制作了一个完整的地形,如下图:



直接上分割地形的代码

public class TerrainSlicing : Editor
{
public stati
4000
c string TerrainSavePath = "Assets/Resources/" + TerrainLoad.TERRAIN_PATH;
//分割大小
public static int SLICING_SIZE = 4;

//开始分割地形
[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;

Vector3 oldSize = terrainData.size;

//得到新地图分辨率
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();
}
}


分割地形的操作肯定是在Editor模式下,所以这里我就直接使用Unity的Editor扩展了,直接添加进MenuItem.

然后分割后动态加载地形,这里我的整张地形宽度为1200,长度也是1200.分割出来的小块地形为300,所以我一共创建了16个小地形.现在来看看动态加载的效果





非常的简单.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity3d Terrain