Skip to content

Instantly share code, notes, and snippets.

@todorok1
Created January 7, 2021 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todorok1/a6a953444cd9d9b7c9c74a177dc4e15d to your computer and use it in GitHub Desktop.
Save todorok1/a6a953444cd9d9b7c9c74a177dc4e15d to your computer and use it in GitHub Desktop.
TerrainのHeightMapを生成するスクリプト(エディタ拡張のクラス)
/// <Summary>
/// 設定に応じてTerrainのHeightMapを生成します。
/// </Summary>
void GenerateTerrainOnEditor(TerrainGenerator generator)
{
// Terrainの解像度を取得します。
int terrainSizeX = generator.terrainData.heightmapResolution;
int terrainSizeZ = generator.terrainData.heightmapResolution;
// Terrainにセットするハイトマップを作成します。
float[,] newHeightMap = new float[terrainSizeX, terrainSizeZ];
for (int z = 0; z < terrainSizeZ; z++)
{
for (int x = 0; x < terrainSizeX; x++)
{
// パーリンノイズの座標を指定して値を取得します。
float xValue = (generator.xOrigin + x) * generator.scale;
float yValue = (generator.yOrigin + z) * generator.scale;
float perlinValue = GetMultiplePerlinNoise(xValue, yValue, terrainSizeX, terrainSizeZ, generator);
float height = generator.heightMultiply * perlinValue;
// HeightMapに値をセットします。
newHeightMap[x, z] = height;
}
}
// 生成したHeightMapをセットします。
generator.terrainData.SetHeights(0, 0, newHeightMap);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment