动态创建地形,并设置splatPrototypes,最后使用photoShop绘制2D图编辑地形贴图。
步骤:
1.创建一个新场景并命名为Tutor_4_CreateTerrainWithSplat,并新建一个GameObject起名为Manager,新建一个名为
Tutor_4_CreateTerrainWithSplat.cs的脚本并拖至Manager上。
2.粘贴并覆盖如下脚本至Tutor_4_CreateTerrainWithSplat里,unity3d脚本该脚本与上个例子的脚本基本一致。
using UnityEngine;
using System.Collections;
using UnityEditor;

public class Tutor_4_CreateTerrainWithSplat : MonoBehaviour {
public AnimationCurve animationCurve;
void Start()
{
CreateTerrain();
}
public Terrain CreateTerrain()
{
TerrainData  terrainData = new TerrainData();
terrainData.heightmapResolution = 513;
terrainData.baseMapResolution = 513;
terrainData.size = new Vector3(50, 50, 50);
terrainData.alphamapResolution = 512;
     terrainData.SetDetailResolution(32, 8);
ModifyTerrainDataHeight(terrainData);
GameObject obj = Terrain.CreateTerrainGameObject(terrainData);
AssetDatabase.CreateAsset(terrainData, "Assets/Tutorial/Tutor_4_TerrainWithSplats.asset");
     AssetDatabase.SaveAssets();
return  obj.GetComponent<</span>Terrain>();
}
public void ModifyTerrainDataHeight(TerrainData terrainData)
{
int width = terrainData.heightmapWidth;
int height = terrainData.heightmapHeight;
float[,] array = new float[width,height];
print ("width:"+ width +" height:"+ height );
for(int i=0; i< width;i++)
for(int j=0; j< height;j++)
{
float f1 = j;
float f2 = height;
array[i,j] = animationCurve.Evaluate(f1/f2);
}
terrainData.SetHeights(0,0,array);
}
}
之后添加地形贴图变量,在脚本里加上public Texture2D[] splats;
然后在Inspector里指定任意一张贴图。
现在开始添加Splat了。
核心是SplatPrototype,也就是Splat原型的信息,包含贴图信息和地形块的信息。
SplatPrototype outSplatPrototype = new SplatPrototype();
之后设置SplatPrototype的如下属性
outSplatPrototype.texture: 贴图
outSplatPrototype.tileOffset:地形块的偏移。
outSplatPrototype.tileSize:地形块的尺寸
组成好SplatPrototype[] outSplatPrototypes后,赋予TerrainData.splatPrototypes 即可。

代码如下。
using UnityEngine;
using System.Collections;
using UnityEditor;

public class Tutor_4_CreateTerrainWithSplat : MonoBehaviour {
public AnimationCurve animationCurve;
public Texture2D[] splats;
void Start()
{
CreateTerrain();
}
public Terrain CreateTerrain()
{
TerrainData  terrainData = new TerrainData();
terrainData.heightmapResolution = 513;
terrainData.baseMapResolution = 513;
terrainData.size = new Vector3(50, 50, 50);
terrainData.alphamapResolution = 512;
     terrainData.SetDetailResolution(32, 8);
terrainData.splatPrototypes = CreateSplatPrototypes(splats,new Vector2(15,15),newVector2(0,0));
ModifyTerrainDataHeight(terrainData);
GameObject obj = Terrain.CreateTerrainGameObject(terrainData);
AssetDatabase.CreateAsset(terrainData, "Assets/Tutorial/Tutor_4_TerrainWithSplats.asset");
     AssetDatabase.SaveAssets();
return  obj.GetComponent<</span>Terrain>();
}
public void ModifyTerrainDataHeight(TerrainData terrainData)
{
int width = terrainData.heightmapWidth;
int height = terrainData.heightmapHeight;
float[,] array = new float[width,height];
for(int i=0; i< width;i++)
for(int j=0; j< height;j++)
{
float f1 = j;
float f2 = height;
array[i,j] = animationCurve.Evaluate(f1/f2);
}
terrainData.SetHeights(0,0,array);
}
public SplatPrototype[] CreateSplatPrototypes(Texture2D[] tmpTextures,Vector2 tmpTileSize,Vector2 tmpOffset)
{
SplatPrototype[] outSplatPrototypes = new SplatPrototype[tmpTextures.Length];
for(int i =0;i
{
outSplatPrototypes = CreateSplatPrototype(tmpTextures,tmpTileSize,tmpOffset);
}
return outSplatPrototypes;
}
public SplatPrototype CreateSplatPrototype(Texture2D tmpTexture, Vector2 tmpTileSize,Vector2 tmpOffset)
{
SplatPrototype outSplatPrototype = new SplatPrototype();
outSplatPrototype.texture = tmpTexture;
outSplatPrototype.tileOffset = tmpOffset;
outSplatPrototype.tileSize = tmpTileSize ;
return outSplatPrototype;
}
}