Skip to content

Instantly share code, notes, and snippets.

@matkoniecz
Created March 18, 2020 20:58
Show Gist options
  • Save matkoniecz/f895ee1bdb35608998f20194e1ff01fe to your computer and use it in GitHub Desktop.
Save matkoniecz/f895ee1bdb35608998f20194e1ff01fe to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapMaker : MonoBehaviour
{
int size = 400;
GameObject[,] pixels;
float[,] heightmap;
int iteration = 0;
float maxDistance(int iteration) {
return 50 - iteration;
}
float neighborCount(int iteration) {
return 4;
}
float height(int x, int y){
x = (x + size)%size;
y = (y + size)%size;
return heightmap[x,y];
}
void makeAction(){
distortRandomly(100);
if (iteration == 0) {
distortRandomly(6000f);
} else if(iteration == 4 || iteration == 9) {
zoomIn(3);
distortRandomly(1000);
} else if(iteration <= 16) {
distortRandomlyExtremeParts(1000, 1000);
averageNearby();
}
}
void zoomIn(int multiplier) {
for(int x = size-1; x >= 0; x--) {
for(int y = size-1; y >= 0; y--) {
heightmap[x,y] = heightmap[x/multiplier,y/multiplier];
}
}
}
void averageNearby() {
for(int x = 0; x < size; x++) {
for(int y = 0; y < size; y++) {
heightmap[x,y] = (height(x, y) + height(x, y+1) + height(x+1, y) + height(x-1, y) + height(x, y-1))/5f + Random.Range(-1f, 1f);
pixels[x,y].GetComponent<Renderer>().material.color = heightToColor(heightmap[x,y]);
}
}
}
void distortRandomly(float amplitude) {
for(int x = 0; x < size; x++) {
for(int y = 0; y < size; y++) {
heightmap[x,y] += Random.Range(-amplitude, amplitude);
}
}
}
void distortRandomlyExtremeParts(float amplitude, float extreme) {
for(int x = 0; x < size; x++) {
for(int y = 0; y < size; y++) {
if(heightmap[x,y] < -extreme || heightmap[x,y] > extreme) {
heightmap[x,y] += Random.Range(-amplitude, amplitude);
}
}
}
}
// Start is called before the first frame update
void Start()
{
pixels = new GameObject[size, size];
heightmap = new float[size, size];
for(int x = 0; x < size; x++) {
for(int y = 0; y < size; y++) {
heightmap[x,y] = 0;
}
}
float _ = 1;
float tileSize = 0.025f;
for(int x = 0; x < size; x++) {
for(int y = 0; y < size; y++) {
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Quad);
go.GetComponent<Renderer>().material.color = heightToColor(heightmap[x,y]);
go.transform.position = new Vector3(x*tileSize, y*tileSize, _);
go.transform.localScale = new Vector3(tileSize, tileSize, _);
pixels[x,y] = go;
}
}
}
Color heightToColor(float height){
if(height <= 0) {
float deepness = -height;
return MixColors(Color.black, deepness, Color.blue, 500);
}
float snowLine = 1000;
if(height < snowLine) {
return MixColors(Color.green, 500, Color.gray, height);
}
Color borderline = MixColors(Color.green, 100, Color.gray, snowLine);
return MixColors(borderline, 100, Color.white, height-snowLine);
}
public Color MixColors(Color a, float aRatio, Color b, float bRatio) {
float rPart = (a.r*aRatio + b.r*bRatio)/(aRatio + bRatio);
float gPart = (a.g*aRatio + b.g*bRatio)/(aRatio + bRatio);
float bPart = (a.b*aRatio + b.b*bRatio)/(aRatio + bRatio);
float aPart = (a.a*aRatio + b.a*bRatio)/(aRatio + bRatio);
return new Color(rPart, gPart, bPart, aPart);
}
void FixedUpdate()
{
Debug.Log("iteration=" + iteration + "time="+Time.time);
if(iteration < Time.time*10) {
makeAction();
iteration++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment