2.terrainData.baseMapResolution int,Resolution of the base map used for rendering far patches on the terrain
如513
3.terrainData.size:
4.terrainData.alphamapResolution alphamap的分辨率,如512;
terrainData.splatPrototypes 就是地形包含的贴图信息
splatPrototypes 为SplatPrototype[],
SplatPrototype为单张贴图信息
SplatPrototype的属性有
SplatPrototype.texture
SplatPrototype.tileOffset
SplatPrototype.tileSize
terrainData.SetAlphamaps(int x,int y,float[,,]) ,其中x,y为起点
float[i,j,k]为通道信息,i,j为对应的点,k为第几张图,float值储存的是该点该图的灰度值。
terrainData.splatPrototypes的长度 = 贴图数量 = splatArray (float[,,])的第三维的长度
- using
UnityEngine; - using
System.Collections; - using
UnityEditor; -
- public
class Tutor_1_CreateSimpleTerrain : MonoBehaviour { - 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); - GameObject
obj = Terrain.CreateTerrainGameObject(terrainData); - AssetDatabase.CreateAsset(terrainData,
"Assets/Tutorial/Tutor_1_SimpleTerrain.asset"); -
AssetDatabase.SaveAssets(); - return
obj.GetComponent<</span>Terrain>(); - }
- }
- using
UnityEngine; - using
System.Collections; - using
UnityEditor; -
- public
class Tutor_2_CreateTerrain_ModifyHeight : MonoBehaviour { - 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_2_Terrain_ModifyHeight.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 = i; - float
f2 = width; - float
f3 = j; - float
f4 = height; - float
baseV = (f1/f2 + f3/f4)/2 * 1; - array[i,j] =baseV*baseV;
- }
- terrainData.SetHeights(0,0,array);
- }
- }
- using
UnityEngine; - using
System.Collections; -
- public
class Tutor_3_CreateTerrainWithAnimati onCurve : MonoBehaviour { - public
AnimationCurve animationCurve; - }
array[i,j] = animationCurve.Evaluate(f1/f2); 其中f1是array里的y,f2是整个高度图的高度。具体代码如下:
- using
UnityEngine; - using
System.Collections; - using
UnityEditor; -
- public
class Tutor_3_CreateTerrainWithAnimati onCurve : 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_3_TerrainWithAnimationCurv
e.asset"); -
AssetDatabase.SaveAssets(); - return
obj.GetComponent(); - }
- 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);
- }
- }
最后创建的地形和我们之前设置的曲线一致。
Tutor_4_CreateTerrainWithSplat.cs的脚本并拖至Manager上。
- 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);
- }
- }
outSplatPrototype.texture: 贴图
outSplatPrototype.tileOffset:地形块的偏移。
outSplatPrototype.tileSize:地形块的尺寸
- 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[i] = CreateSplatPrototype(tmpTextures[i],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; - }
- }
现在我们再进一步,通过Photoshop绘制splat的alpha图。
1.选中Manager,在Inspector面板里,将Terrain Textures设置为任意两张贴图,这里用的是unity Terrain包里的“Grass&Rock”和 “Grass (Hill)” 两张贴图。
2.然后脚本里也添加splats属性,public Texture2D[]
3.打开photoshop,创建一张512x512的rgb图,先铺满红色,在绘制一些绿色,如下图
4.将该图拖进unity,并设置Import Settings里的 Texture Type为Advanced,然后勾选Read/Write Enable。最后把importType设置为Default(importType不能为其他的)。
5. Manager的Inspector面板里,splatAlphaMaps,此Texture2D数组设置为1,并指定为splatAlphaMap1
6.在Tutor_4_CreateTerrainWithSplat脚本里补充以下函数
- public float[,,] CreateSplatAlphaArray(Texture2D[] splatAlphaMaps,int numOfSplatPrototypes )
- {
- List cArray = new List();
- int splatAlphaMap_SizeX = splatAlphaMaps[0].width;
- int splatAlphaMap_SizeY = splatAlphaMaps[0].height;
- float[,,] outSplatAlphaArray = new float[splatAlphaMap_SizeX,splatAlphaMap_SizeY,numOfSplatPrototypes];
- //第几张SplatAlphaMap
- for(int splatAlphaMapIndex=0; splatAlphaMapIndex
- {
- //RGBA第几个通道
- for(int alphaIndex =0;alphaIndex<4; alphaIndex ++)
- {
- //Splat ID
- int splatIndex = alphaIndex+splatAlphaMapIndex*4;
- //仅当Splat ID小于Splat的数量时
- if(splatIndex < numOfSplatPrototypes)
- {
- for (int index_heightmapY = 0; index_heightmapY < splatAlphaMap_SizeY; index_heightmapY++)
-
{ -
for (int index_heightmapX = 0; index_heightmapX < splatAlphaMap_SizeX; index_heightmapX++) -
{ - //取第splatAlphaMapIndex张SplatAlphaMap上的位于index_heightmapY,index_heightmapX的颜色值
- Color c =
splatAlphaMaps[splatAlphaMapIndex].GetPixel(index_heightmapY,index_heightmapX); - cArray.Add(c);
- //赋予outSplatAlphaArray的index_heightmapX,index_heightmapY,splatIndex对应的通道值
- outSplatAlphaArray[index_heightmapX, index_heightmapY, splatIndex] = c[ alphaIndex ];
- }
- }
- }
- else
- {
- return outSplatAlphaArray;
- }
- }
- }
- return outSplatAlphaArray;
- }
-
void Start() - {
- CreateTerrain();
- TerrainData
terData =AssetDatabase.LoadAssetAtPath("Assets/Tutorial/Tutor_4_TerrainWithSplats.asset",typeof(TerrainData)) as TerrainData; - float[,,] splatAlphaArray = CreateSplatAlphaArray(splatAlphaMaps, terData.splatPrototypes.Length);
- terData.SetAlphamaps(0,0,splatAlphaArray);
- }
之后点击运行,发现地形确实是我们之前在photoshop里绘制的形状,如下
- using
UnityEngine; - using
System.Collections; - using
UnityEditor; -
- public
class Tutor_6_TreePrototype : MonoBehaviour { - public
GameObject treePrefab; - public
TerrainData terrainData; - 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); - TreePrototype
treePrototype = new TreePrototype(); - treePrototype.prefab = treePrefab;
- treePrototype.bendFactor =
1; - terrainData.treePrototypes =
new TreePrototype[1]; - terrainData.treePrototypes[0] = treePrototype;
- GameObject
obj = Terrain.CreateTerrainGameObject(terrainData); - AssetDatabase.CreateAsset(terrainData,
"Assets/Tutorial/Tutor_1_SimpleTerrain.asset"); -
AssetDatabase.SaveAssets(); - return
obj.GetComponent<</span>Terrain>(); - }
- }
TreeInstance.prototypeindex:使用的prototype序号,从0开始
TreeInstance.position:在地形里的相对位置(不是世界坐标的位置),范围为[0,1]
TreeInstance.color:树的颜色
TreeInstance.lightmapColor:树如果有lightmap的话,lightmap的颜色
TreeInstance.heightScale:树高的缩放
TreeInstance.widthScale:树宽的缩放,即xz轴上的缩放
Terrain terrain;
terrain.AddTreeInstance(tmpTreeInstances);
TerrainCollider
tc.enabled =
tc.enabled =
- using
UnityEngine; - using
System.Collections; -
- public
class Tutor_6_TreeInstances : MonoBehaviour { - public
Terrain terrain; -
- void
Start () { - AddTrees(terrain,100);
- }
- public
void AddTrees(Terrain terrain,int numOfTrees) - {
- if(terrain.terrainData!=null)
- {
- terrain.terrainData.treeInstances =
new TreeInstance[numOfTrees]; - for(int
i =0;i< numOfTrees;i++) - {
- TreeInstance
tmpTreeInstances = new TreeInstance(); - tmpTreeInstances.prototypeIndex =0;
// ID of tree prototype - tmpTreeInstances.position =
new Vector3(Random.Range(0f,1f),0,Random.Range(0f,1f)); // not regular pos, [0,1] - tmpTreeInstances.color =
new Color(1,1,1,1); - tmpTreeInstances.lightmapColor =
new Color(1,1,1,1);//must add - float
ss= Random.Range(0.8f,1f); - tmpTreeInstances.heightScale =ss;
//same size as prototype - tmpTreeInstances.widthScale =ss;
- terrain.AddTreeInstance(tmpTreeInstances);
- }
- TerrainCollider
tc = terrain.GetComponent<</span>TerrainCollider>(); - tc.enabled =
false; - tc.enabled =
true; - }
- }
- }