Skip to content

Instantly share code, notes, and snippets.

@lycoris102
Created December 1, 2018 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lycoris102/db8f0b8e0f01b5d6c8a559d1c0015629 to your computer and use it in GitHub Desktop.
Save lycoris102/db8f0b8e0f01b5d6c8a559d1c0015629 to your computer and use it in GitHub Desktop.
using Unicessing;
public class PerlinNoiseTerrain : UGraphics
{
int cols, rows;
int scale = 20;
int w = 1200;
int h = 1600;
float[,] terrain;
float flying = 0;
protected override void Setup()
{
size(600, 600, P3D);
cols = w / scale;
rows = w / scale;
terrain = new float[cols, rows];
lights();
}
private void noiseSpace(float flying)
{
float yoff = flying;
for (int y = 0; y < rows; y++)
{
float xoff = 0;
for (int x = 0; x < cols; x++)
{
terrain[x, y] = map(noise(xoff, yoff), 0, 1, -100, 100);
xoff += 0.1f;
}
yoff += 0.1f;
}
}
protected override void Draw()
{
flying -= 0.01f;
noiseSpace(flying);
background(0);
stroke(255);
noFill();
translate(width / 2, height / 2 + 50);
float xrot = map(mouseY, 0, height, PI / 6, PI / 2);
rotateX(xrot);
float yrot = map(mouseX, 0, width, -PI / 3, PI / 3);
rotateY(yrot);
translate(-w / 2, -h / 2);
for (int y = 0; y < rows - 1; y++)
{
beginShape(TRIANGLE_STRIP);
for (int x = 0; x < cols; x++)
{
vertex(x * scale, y * scale, terrain[x, y]);
vertex(x * scale, (y + 1) * scale, terrain[x, y + 1]);
}
endShape();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment