-
Notifications
You must be signed in to change notification settings - Fork 2
/
TerrainGenerator.cs
37 lines (28 loc) · 1.5 KB
/
TerrainGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using SiliconStudio.Core.Mathematics;
namespace EndlessVoxelExample.World
{
internal static class TerrainGenerator
{
private const int MaxTerrainHeight = 20;
private const int NoiseScale = 30;
private static readonly Random Random = new Random();
private static readonly OpenSimplexNoise SimplexNoise = new OpenSimplexNoise(Random.Next());
public static int GetTerrainHeight(int worldX, int worldZ)
{
double lowerNoiseX = (double)worldX / NoiseScale;
double lowerNoiseY = (double)worldZ / NoiseScale;
double lowerLeft = SimplexNoise.Evaluate(lowerNoiseX, lowerNoiseY);
double upperLeft = SimplexNoise.Evaluate(lowerNoiseX, lowerNoiseY + 1);
double lowerRight = SimplexNoise.Evaluate(lowerNoiseX + 1, lowerNoiseY);
double upperRight = SimplexNoise.Evaluate(lowerNoiseX + 1, lowerNoiseY + 1);
double rightAmount = (worldX - lowerNoiseX * NoiseScale) / NoiseScale;
double forwardAmount = (worldZ - lowerNoiseY * NoiseScale) / NoiseScale;
double totalAmount = (rightAmount + forwardAmount) / 2;
double firstDiagonal = MathUtil.Lerp(lowerLeft, upperRight, totalAmount);
double secondDiagonal = MathUtil.Lerp(upperLeft, lowerRight, totalAmount);
double value = (firstDiagonal + secondDiagonal) / 2;
return (int) (MaxTerrainHeight * value);
}
}
}