【发布时间】:2020-05-04 14:38:15
【问题描述】:
我正在使用 Unity 进行一个项目,该项目需要生成一些简单的 3D 山脉/山丘。由于我的要求是创建一个“简单”的形状,我似乎没有找到答案,我想也许我可以从这里得到一些帮助。无论如何,this 是柏林噪声的正常输出,虽然它很平滑,但输出仍然很复杂,有很多山丘/山脉。我正在寻找类似 @987654322@ 的东西。我需要确保输出图像的边界周围没有任何高度。我想你已经明白了。祝你有美好的一天!
这是我现在使用的在线教程中的代码:
using UnityEngine;
public class PerlinNoise : MonoBehaviour
{
private static int width = 256;
private static int height = 128;
public float scale = 20f;
public float offsetX = 100f;
public float offsetY = 100f;
private int xcont = 0, ycont = 0;
public float[,] array = new float[width,height];
private void Start()
{
offsetX = Random.Range(0f, 99999f);
offsetY = Random.Range(0f, 99999f);
}
void Update()
{
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = GenerateTexture();
}
Texture2D GenerateTexture()
{
Texture2D texture = new Texture2D(width, height);
//GENERATE A PERLIN NOISE MAP FOR THE TEXTURE
for(int x=0;x<width;x++)
{
for(int y=0;y<height;y++)
{
Color color = CalculateColor(x,y);
texture.SetPixel(x, y, color);
}
}
texture.Apply();
return texture;
}
Color CalculateColor(int x, int y)
{
float xCoord = (float)x / width * scale + offsetX;
float yCoord = (float)y / height * scale + offsetY;
float sample = Mathf.PerlinNoise(xCoord,yCoord);
if (xcont == width - 1)
{
xcont = 0;
ycont++;
}
else xcont++;
if (ycont == height - 1 ) ycont = 0;
array[xcont,ycont] = sample;
return new Color(sample, sample, sample);
}
}
【问题讨论】:
标签: c# unity3d random shapes perlin-noise