Hello, I am in need of Help.
I am trying to create a generator that is using Perlin noise.
What I am trying to create are islands that are created from hexagons or cubes and not one big mesh.
I want to use Perlin noise to create more natural looking Islands.
I have tried to follow tutorials created by Sebastian Lague but I have problems understanding half of his code and his coding method.
What I want.
What I have managed to create.
My question is How do I “repair” the code so I get the result as in picture 1 ?
Here is my code.
public class Noise : MonoBehaviour
{
public int width;
public int height;
public float scale;
[Range(0, 1)]
public float persistance;
public float lacunarity;
int offsetX;
int offsetY;
public float noiseHeight;
Renderer render;
public int octaves;
private void Start()
{
offsetX = Random.Range(0, 99999);
offsetY = Random.Range(0, 99999);
}
void Update()
{
render = GetComponent<Renderer>();
render.material.mainTexture = GenerateTexture();
}
Texture2D GenerateTexture()
{
Texture2D texture = new Texture2D(width, height);
for(int x = 0 ; x < width; x++)
{
for (int y = 0; y < height; y++)
{
float amplitude = 1;
float frequency = 1;
noiseHeight = 0;
for (int i = 0; i < octaves; i++)
{
float xCoord = x / scale * frequency + offsetX;
float yCoord = y / scale * frequency + offsetY;
float sample = Mathf.PerlinNoise(xCoord, yCoord) * 2 -1;
noiseHeight += sample * amplitude;
amplitude *= persistance;
frequency *= lacunarity;
}
Color color = CalculateColor(noiseHeight);
texture.SetPixel(x, y,color);
}
}
texture.Apply();
return texture;
}
Color CalculateColor(float noiseHeight)
{
return new Color(noiseHeight, noiseHeight, noiseHeight);
}
}