1 回答

TA貢獻1794條經驗 獲得超7個贊
返回的數據由GetAlphamaps
返回的數組是三維的 - 前兩個維度表示地圖上的 x 和 y 坐標,而第三個維度表示應用 alphamap 的 splatmap 紋理。
或者簡單來說就是“float[x, y, l]
哪里”
x
= 寬度(以像素為單位)y
= 高度(以像素為單位)l
= 紋理層
假設您想將其設置為該像素坐標處的某個紋理,您要做的就是
將紋理層的權重設置為
1
將所有其他層權重設置為
0
假設您有 3 層,并且您希望第二層(=索引1
)是完全加權的紋理:
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為各層實現一個,比如
public enum TerrainLayer
{
? ? Default = 0,
?
? ? Green,
? ? Red
}
因此您可以簡單地將相應的圖層索引作為參數傳遞 - 比傳遞值本身更安全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 ();
}
現在你可以簡單地稱它為例如
ChangeTexture(somePosition, TerrainLayer.Green);
- 1 回答
- 0 關注
- 252 瀏覽
添加回答
舉報