亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 SetAlphamaps 設置為某一特定紋理?

如何將 SetAlphamaps 設置為某一特定紋理?

PHP
不負相思意 2024-01-21 10:15:39
我想用特定的紋理改變我的地形的紋理。我對設置 splatmapdata 感到困惑,任何人都可以幫助我嗎?private void ChangeTexture(Vector3 WorldPos){    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(3, 3, 15, 15);    terrainData.SetAlphamaps (mapX, mapZ, splatmapData);    terrain.Flush ();}
查看完整描述

1 回答

?
慕田峪9158850

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);


查看完整回答
反對 回復 2024-01-21
  • 1 回答
  • 0 關注
  • 252 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號