簡體   English   中英

在多個 Unity Terrain 對象中使用 Perlin Noise

[英]Using Perlin Noise across multiple Unity Terrain objects

我有一個班級項目,我們應該在其中使用 Unity Terrain 3D 對象並創建一個 3x3 平滑生成的地形。 為此,我們被告知要創建一個在 8 個主要方向上具有相鄰地形的中央地形。 我已經讓柏林噪聲通過這種方法工作

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TerrainNoiseGeneration : MonoBehaviour
{

private TerrainData myTerrainData;
public Vector3 worldSize;
public int resolution = 129;
private float userInput = (float)4.2;
public float offsetX;
public float offsetZ;

// Start is called before the first frame update
void Start()
{
    myTerrainData = gameObject.GetComponent<TerrainCollider>().terrainData;
    worldSize = new Vector3(100, 50, 100);
    myTerrainData.size = worldSize;
    myTerrainData.heightmapResolution = resolution;
    float[,] heightArray = new float[resolution, resolution];
    heightArray = PerlinNoise(userInput, offsetX, offsetZ);
    myTerrainData.SetHeights(0, 0, heightArray);
}

// Update is called once per frame
void Update()
{
    float[,] heightArray = new float[resolution, resolution];
    heightArray = PerlinNoise(userInput, offsetX, offsetZ);
    myTerrainData.SetHeights(0, 0, heightArray);
}

float[,] PerlinNoise(float userInput, float offsetX, float offsetZ)
{
    float[,] heights = new float[resolution, resolution];

    for (int z = 0; z < resolution; z++)
    {
        for (int x = 0; x < resolution; x++)
        {
            float nx = (x + offsetX) / resolution * userInput;
            float ny = (z + offsetZ) / resolution * userInput;
            heights[z, x] = Mathf.PerlinNoise(nx, ny);
        }
    }

    return heights;
}

這段代碼允許我在第一個 Terrain 對象中生成平滑的地形,但是當我嘗試輸入偏移值以便邊緣可以排列時,它們沒有相同的值。

我將不勝感激對此問題的任何幫助,因為我嘗試了很多不同的解決方案,但都沒有奏效

更新:我能夠通過一個相當簡單的解決方案來解決這個問題,因為我需要使用我的分辨率作為偏移量而不是地形之間的距離

我需要將 OffsetX 和 OffsetZ 設置為等於它們各自的分辨率位置而不是它們的統一位置。

例如,我的地形是 100x100,所以我根據它的位置將偏移設置為 100 或 -100,但我需要使用 128 或 -128 以使其與分辨率保持一致

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM