c# - 在多个 Unity Terrain 对象中使用 Perlin Noise
问题描述
我有一个类项目,我们应该在其中使用 Unity Terrain 3D 对象并创建一个 3x3 平滑生成的地形。为此,我们被告知要创建一个在 8 个基本方向上具有相邻地形的中央地形。我已经让 Perlin Noise 通过这种方法工作
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;
}
此代码允许我在第一个地形对象中生成平滑的地形,但是当我尝试输入偏移值以便边缘可以对齐时,它们没有相同的值。
我将不胜感激有关此问题的任何帮助,因为我尝试了很多不同的解决方案,但都没有奏效
更新:我能够通过一个相当简单的解决方案来解决这个问题,即我需要使用我的分辨率作为偏移量而不是地形之间的距离
解决方案
我需要将 OffsetX 和 OffsetZ 设置为等于它们各自的分辨率位置,而不是它们的统一位置。
例如,我的地形是 100x100,所以我根据其位置将偏移设置为 100 或 -100,但我需要使用 128 或 -128 以使其与分辨率保持一致
推荐阅读
- c# - Visual Studio 2019 中的希腊字母
- sql - 查找缺失和无序的记录
- reactjs - 如何在 React 16 中找到导致更新的触发器?
- state - xstate中的嵌套属性分配?
- ms-word - Word VSTO 加载项:在屏幕上获取工作区坐标/矩形
- ios - Cordova - window.open 在 IOS 上不起作用
- ansible - 使用循环重用 Ansible 编写的任务
- jira - 如何使用 jira rest api 获取特定用户的所有打开和关闭的问题计数
- c++ - Visual Studio 根本不会因异常(断言)而停止
- php - Php Unset 不删除数组元素