简体   繁体   English

在多个 Unity Terrain 对象中使用 Perlin Noise

[英]Using Perlin Noise across multiple Unity Terrain objects

I have a class project in which we are supposed to use Unities Terrain 3D objects and create a 3x3 smoothly generated terrain.我有一个班级项目,我们应该在其中使用 Unity Terrain 3D 对象并创建一个 3x3 平滑生成的地形。 For this we have been told to create a central Terrain the has adjacent terrains in the 8 cardinal directions.为此,我们被告知要创建一个在 8 个主要方向上具有相邻地形的中央地形。 I have gotten the Perlin Noise to work through this method我已经让柏林噪声通过这种方法工作

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;
}

This code allows me to Generate a smooth terrain in the first Terrain object but when I try entering the offset values so that the edges can line-up they do not have the same values.这段代码允许我在第一个 Terrain 对象中生成平滑的地形,但是当我尝试输入偏移值以便边缘可以排列时,它们没有相同的值。

I would appreciate any assistance on this issue as I have tried a lot of different solutions, none of which are working我将不胜感激对此问题的任何帮助,因为我尝试了很多不同的解决方案,但都没有奏效

Update: I was able to solve the problem with a rather simple solution of the fact that I needed to use my resolution as the offset not the distance between the terrains更新:我能够通过一个相当简单的解决方案来解决这个问题,因为我需要使用我的分辨率作为偏移量而不是地形之间的距离

I needed to set the OffsetX and OffsetZ equal to that of their respective resolution positions instead of their unity positions.我需要将 OffsetX 和 OffsetZ 设置为等于它们各自的分辨率位置而不是它们的统一位置。

For example my terrains are 100x100 so I was setting offset to 100 or -100 depending on its location but instead I needed to use 128 or -128 to keep it in line with the resolution例如,我的地形是 100x100,所以我根据它的位置将偏移设置为 100 或 -100,但我需要使用 128 或 -128 以使其与分辨率保持一致

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM