2
所以我一直在嘗試使用代碼生成地形,並且遇到了問題。當我分配heightmapResolution時,我的地形的大小增加了兩倍的冪。爲什麼分配heightmapResolution會增加我的地形的大小?
例如,如果我的heightmapResolution是513,然後通過16倍我的地形尺寸增大如果我的heightmapResolution是257,然後通過8
一個因子我的地形尺寸增大據我所知(我也讀過),heightmapResolution不應該影響地形的大小。
我必須強調,尺寸增加只發生在X軸和Z軸上。身高保持不變。
/// <summary>
/// Gets the terrain data either from memory if its loaded, from disk if it isnt, or generates a new one of neither of those are available.
/// </summary>
/// <value>The terrain data.</value>
public static TerrainData terrainData {
get {
TerrainData terrainData = new TerrainData();
if(isTerrainLoaded){
//Debug.Log("terrainData is already loaded, returning it!");
return(loadedTerrainData);
}
else if(Resources.Load("terrainData") != null && !generateNewTerrain){
terrainData = Resources.Load("terrainData") as TerrainData;
Debug.Log("Read terrainData from disk!");
}
else{
Debug.Log("No terrainData found on disk, generating a new random one...");
terrainData.size = new Vector3(width, height - sandBaseHeight, length);
Debug.Log ("Original terrain size: " + terrainData.size);
terrainData.heightmapResolution = heightmapResolution;
Debug.Log ("Bigger terrain size: " + terrainData.size);
terrainData.baseMapResolution = baseMapResolution;
terrainData.SetDetailResolution((int)detailResolution.x, (int)detailResolution.y); //(int) because data is stored in a Vector2 which is float
terrainData.alphamapResolution = aplhaMapResolution;
float[,] heights = new float[terrainData.heightmapWidth,terrainData.heightmapHeight];
Vector2 randomLocation; //for perlin noise
randomLocation.x = Random.Range(-10000,10000); //that is hopefully enough
randomLocation.y = Random.Range(-10000,10000);
for (int x = 0; x < terrainData.heightmapWidth; x++) {
for (int z = 0; z < terrainData.heightmapWidth; z++) {
heights[x,z] = Mathf.PerlinNoise(randomLocation.x + (float)x/scale, randomLocation.y + (float)z/scale);
}
}
terrainData.SetHeights (0, 0, heights);
terrainData.splatPrototypes = splatTextures;
AssetDatabase.CreateAsset(terrainData,"Assets/Resources/terrainData.asset");
}
loadedTerrainData = terrainData;
return(terrainData);
}
}
所以這是產生我terrainData歐元區。 Debug.Log(「更大的地形大小:」+ terrainData.size)中已經可以看到地形大小的變化。在分配heightmapResolution之後立即執行。
任何想法爲什麼發生這種情況?
你使用過調試器嗎? – Varaquilex
我想通了,並在下面發佈我的答案。我確實使用過調試器,只是嘗試了不同的東西,直到它以某種方式工作。 – Pizmovc
我猜這是因爲Unity在設置heightmapResolution時嘗試重新調整地形以保持輸出的實際分辨率? (只是一個想法) – vatbub