Unity code, but even so...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Terrain : MonoBehaviour
{
//static members
public const int HEIGHT_MAX = 10;
public const int LENGTH = 30;
//instance members
int[] terrainPoints = new int[Terrain.LENGTH];
GameObject[] terrainGOs = new GameObject[Terrain.LENGTH];
public int scanRadius = 1;
void Start ()
{
Camera.main.transform.position = new Vector3(0,0,-50); //zoom out a bit
for (int i = 0; i < terrainPoints.Length; i++)
{
terrainPoints[i] = Random.Range(0, Terrain.HEIGHT_MAX);
terrainGOs[i] = GameObject.CreatePrimitive(PrimitiveType.Cube);
terrainGOs[i].transform.position = new Vector3(i, terrainPoints[i], 0);
}
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
Smoothe();
}
void Smoothe()
{
for (int selfIndex = scanRadius; selfIndex < terrainPoints.Length - scanRadius; selfIndex++)
{
int self = terrainPoints[selfIndex];
//scan a local subset *including* self
int sum = 0;
int count = 0;
for (int neighbourIndex = selfIndex - scanRadius;
neighbourIndex < selfIndex + scanRadius + 1;
neighbourIndex++)
{
int neighbour = terrainPoints[neighbourIndex];
sum += neighbour;
count++;
}
self = sum / count; //get the average over the sample diameter.
terrainPoints[selfIndex] = self;
terrainGOs[selfIndex].transform.position = new Vector3(selfIndex, self, 0);
}
}
}
Save as Terrain.cs
, drop on any GameObject in the scene. Spacebar Smoothe()
s the noise generated in Start()
, you can do this repeatedly. You can use scanRadius=1
, but the larger you make it via the inspector (e.g. 2, 3 and upwards) the more rapidly it will smoothe.