为什么分配 heightmapResolution 会增加地形的大小?
Why does assigning heightmapResolution increase the size of my terrain?
所以我一直在尝试使用代码生成地形,但遇到了一个问题。当我分配 heightmapResolution 时,我的地形大小增加了 2 的某个幂。
例如,如果我的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);
}
}
这就是生成我的地形数据的集团。 Debug.Log ("Bigger terrain size: " + terrainData.size) 中已经可以看到地形大小的变化;在我分配 heightmapResolution 后立即。
关于为什么会发生这种情况有什么想法吗?
我想通了。
我必须在设置地形大小之前设置高度图分辨率。
这是按预期工作的最终代码。
terrainData.heightmapResolution = heightmapResolution;
terrainData.size = new Vector3(width, height - sandBaseHeight, length);
terrainData.baseMapResolution = baseMapResolution;
terrainData.SetDetailResolution((int)detailResolution.x, (int)detailResolution.y);
terrainData.alphamapResolution = aplhaMapResolution;
Debug.Log ("Terrain size: " + terrainData.size); //Correct terrain size
希望这对其他人也有帮助!
所以我一直在尝试使用代码生成地形,但遇到了一个问题。当我分配 heightmapResolution 时,我的地形大小增加了 2 的某个幂。
例如,如果我的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);
}
}
这就是生成我的地形数据的集团。 Debug.Log ("Bigger terrain size: " + terrainData.size) 中已经可以看到地形大小的变化;在我分配 heightmapResolution 后立即。
关于为什么会发生这种情况有什么想法吗?
我想通了。
我必须在设置地形大小之前设置高度图分辨率。 这是按预期工作的最终代码。
terrainData.heightmapResolution = heightmapResolution;
terrainData.size = new Vector3(width, height - sandBaseHeight, length);
terrainData.baseMapResolution = baseMapResolution;
terrainData.SetDetailResolution((int)detailResolution.x, (int)detailResolution.y);
terrainData.alphamapResolution = aplhaMapResolution;
Debug.Log ("Terrain size: " + terrainData.size); //Correct terrain size
希望这对其他人也有帮助!