-
-
Save todorok1/f3475f6cbcc6b7fd1cb20e755cddf14b to your computer and use it in GitHub Desktop.
エディタ上でパーリンノイズによる地形を生成するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <Summary> | |
/// エディタ上でフィールド用のオブジェクトを生成するメソッドです。 | |
/// </Summary> | |
void GenerateFieldOnEditor(FieldGenerator generator) | |
{ | |
for (float z = 0f; z < generator.fieldSizeZ; z++) | |
{ | |
for (float x = 0f; x < generator.fieldSizeX; x++) | |
{ | |
// パーリンノイズの座標を指定して値を取得します。 | |
float xValue = generator.xOrigin + x * generator.scale; | |
float yValue = generator.yOrigin + z * generator.scale; | |
float perlinValue = Mathf.PerlinNoise(xValue, yValue); | |
float height = generator.fieldHeight * perlinValue; | |
// 位置のVector3を作成してオブジェクトをインスタンス化します。 | |
Vector3 pos = new Vector3(x, height, z); | |
InstantiateFieldParts(pos, generator); | |
} | |
} | |
} | |
/// <Summary> | |
/// オブジェクトをインスタンス化するメソッドです。 | |
/// </Summary> | |
void InstantiateFieldParts(Vector3 pos, FieldGenerator generator) | |
{ | |
// オブジェクトをインスタンス化します。 | |
GameObject obj = Instantiate(generator.fieldParts, Vector3.zero, Quaternion.identity); | |
// オブジェクトのTransformを設定します。 | |
obj.transform.SetParent(generator.fieldParent); | |
obj.transform.localPosition = pos; | |
// マテリアルをランダムにセットします。(検証用の処理) | |
int matIndex = Random.Range(0, generator.matList.Count); | |
obj.GetComponent<MeshRenderer>().material = generator.matList[matIndex]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment