1 回答

TA貢獻1794條經(jīng)驗 獲得超7個贊
返回的數(shù)據(jù)由GetAlphamaps
返回的數(shù)組是三維的 - 前兩個維度表示地圖上的 x 和 y 坐標(biāo),而第三個維度表示應(yīng)用 alphamap 的 splatmap 紋理。
或者簡單來說就是“float[x, y, l]
哪里”
x
= 寬度(以像素為單位)y
= 高度(以像素為單位)l
= 紋理層
假設(shè)您想將其設(shè)置為該像素坐標(biāo)處的某個紋理,您要做的就是
將紋理層的權(quán)重設(shè)置為
1
將所有其他層權(quán)重設(shè)置為
0
假設(shè)您有 3 層,并且您希望第二層(=索引1
)是完全加權(quán)的紋理:
float[,,] splatmapData = terrainData.GetAlphamaps(mapX, mapZ, 15, 15);
// Iterate over x-y coordinates within the array
for(var y = 0; i < 15; y++)
{
? ? for(var x = 0; x < 15; x++)
? ? {
? ? ? ? // Set first layers weight to 0
? ? ? ? splatmapData[x, y, 0] = 0;
? ? ? ? // Set second layer's weight to 1
? ? ? ? splatmapData[x, y, 1] = 1;
? ? ? ? // Set third layer's weight to 0
? ? ? ? splatmapData[x, y, 2] = 0;
? ? }
}
terrainData.SetAlphamaps(mapX, mapZ, splatmapData);
然后我會enum為各層實現(xiàn)一個,比如
public enum TerrainLayer
{
? ? Default = 0,
?
? ? Green,
? ? Red
}
因此您可以簡單地將相應(yīng)的圖層索引作為參數(shù)傳遞 - 比傳遞值本身更安全int:
private void ChangeTexture(Vector3 worldPos, TerrainLayer toLayer)
{
? ? print ("changeTexture");
? ? int mapX = (int)(((worldPos.x - terrainPos.x) / terrainData.size.x) * terrainData.alphamapWidth);
? ? int mapZ = (int)(((worldPos.z - terrainPos.z) / terrainData.size.z) * terrainData.alphamapHeight);
? ? float[,,] splatmapData = terrainData.GetAlphamaps(mapX, mapZ, 15, 15);
? ? for(var z = 0; z < 15; z++)
? ? {
? ? ? ? for(var x = 0; x < 15; x++)
? ? ? ? {
? ? ? ? ? ? // This ofcourse would be more efficient if you do this only once
? ? ? ? ? ? // e.g. in Awake since the enum won't change on runtime
? ? ? ? ? ? var values = (TerrainLAyer[])Enum.GetValues(typeof(TerrainLayer));
? ? ? ? ? ? // Iterate through the enum and?
? ? ? ? ? ? for(var l = 0; l < values.Length; l++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? // set all layers to 0 except the toLayer
? ? ? ? ? ? ? ? splatmapData[x, z, l] = values[l] == toLayer ? 1 : 0;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? terrainData.SetAlphamaps (mapX, mapZ, splatmapData);
? ? terrain.Flush ();
}
現(xiàn)在你可以簡單地稱它為例如
ChangeTexture(somePosition, TerrainLayer.Green);
- 1 回答
- 0 關(guān)注
- 234 瀏覽
添加回答
舉報