Skip to content

Instantly share code, notes, and snippets.

@augustoerico
Last active February 10, 2018 16:00
Show Gist options
  • Save augustoerico/471bf36554efb62079bccc632234a933 to your computer and use it in GitHub Desktop.
Save augustoerico/471bf36554efb62079bccc632234a933 to your computer and use it in GitHub Desktop.
Population density generator using Perlin Noise for a 1000 u² terrain and 100 u³ blocs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Generator : MonoBehaviour {
public int height = 1000;
public int width = 1000;
public float noiseScale = 500.0f;
public int footprint = 100;
float seed = 0;
List<GameObject> objects = new List<GameObject> ();
void Start () {
seed = Random.Range (0, 100);
}
void Update () {
bool update = Input.GetKeyDown ("space");
if (update) {
this.Generate ();
}
}
void Generate () {
foreach (GameObject o in objects) {
Destroy (o);
}
for (int h = 0; h < height; h += footprint) {
for (int w = 0; w < width; w += footprint) {
float buildingHeight = Mathf.PerlinNoise (w/noiseScale + seed, h/noiseScale + seed) * 10;
GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
cube.transform.localScale += new Vector3 (footprint, buildingHeight * footprint, footprint);
cube.transform.position = new Vector3 (h, 0, w);
objects.Add (cube);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment