繁体   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