本文整理汇总了C#中UnityEngine.Terrain.RandomPositionOnTerrain方法的典型用法代码示例。如果您正苦于以下问题:C# Terrain.RandomPositionOnTerrain方法的具体用法?C# Terrain.RandomPositionOnTerrain怎么用?C# Terrain.RandomPositionOnTerrain使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Terrain
的用法示例。
在下文中一共展示了Terrain.RandomPositionOnTerrain方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddLake
/// <summary>
/// Add a lake to the terrain
/// </summary>
private void AddLake(Terrain terrain, int tileX, int tileZ, int lakeSize = 150)
{
var data = terrain.terrainData;
//Access height map
var width = data.heightmapWidth;
var height = data.heightmapHeight;
var heightmap = data.GetHeights(0, 0, width, height);
//random X and Z coordinate and sample height
Vector3 lakeCenter = terrain.RandomPositionOnTerrain(lakeSize);
var lakeRadius = lakeSize / 2;
var coords = terrain.WorldToTerrainMapCoordinates(lakeCenter);
int xCenter = (int)coords.x;
int zCenter = (int)coords.z;
var minDepth = 0.09f;
for (int x = xCenter - lakeRadius; x < xCenter + lakeRadius; x++)
{
for (int z = zCenter - lakeRadius; z < zCenter + lakeRadius; z++)
{
//deeper the closer to the center
float centerX = 1 - Math.Abs((float)(xCenter - x) / lakeRadius);
float centerZ = 1 - Math.Abs((float)(zCenter - z) / lakeRadius);
float avg = (centerX * centerZ) / 2;
var depth = minDepth * avg;
Mathf.Clamp(depth, minDepth - 0.01f, minDepth + 0.01f);
heightmap[z, x] -= depth;
}
}
//Update height map
data.SetHeights(0, 0, heightmap);
lakeCenter.y -= 4.5f;
var waterInstance = (GameObject)GameObject.Instantiate(_terrainSettings.WaterPrefab, lakeCenter, Quaternion.identity);
waterInstance.SetParent(terrain.gameObject);
}