terrain-map
ARCHIVED - repo for blog post http://www.vogt.world/writing/procedural-terrain-generation/
git clone https://git.vogt.world/terrain-map.git
Log | Files | README.md
← Commit log
commit
message
using options for LandMap functions instead of individual params
author
Ben Vogt <[email protected]>
date
2016-05-15 15:28:59
stats
2 file(s) changed, 43 insertions(+), 28 deletions(-)
files
landmap.js
worker.js
  1diff --git a/landmap.js b/landmap.js
  2index cd628d6..64a34df 100644
  3--- a/landmap.js
  4+++ b/landmap.js
  5@@ -53,7 +53,9 @@ LandMap.prototype.set = function(which, x, y, value) {
  6   this.maps[which][(x + this.size * y)] = value;
  7 };
  8 
  9-LandMap.prototype.generate = function(deviationAmount, feature) {
 10+LandMap.prototype.generate = function(options) {
 11+  var deviationAmount = options.deviation,
 12+    feature = options.feature;
 13   var self = this;
 14 
 15   if (!(feature in self.maps)) {
 16@@ -116,7 +118,11 @@ LandMap.prototype.generate = function(deviationAmount, feature) {
 17   }
 18 };
 19 
 20-LandMap.prototype.smooth = function(amount, featureFrom, featureTo) {
 21+LandMap.prototype.smooth = function(options) {
 22+  var amount = options.amount,
 23+    featureFrom = options.from,
 24+    featureTo = options.to;
 25+
 26   if (!(featureTo in this.maps)) {
 27     this.maps[featureTo] = new Array(this.size * this.size);
 28   }
 29@@ -146,7 +152,11 @@ LandMap.prototype.smooth = function(amount, featureFrom, featureTo) {
 30   }
 31 };
 32 
 33-LandMap.prototype.combine = function(one, two, three, result) {
 34+LandMap.prototype.combine = function(options) {
 35+  var one = options.one,
 36+    two = options.two,
 37+    three = options.three,
 38+    result = options.result;
 39 
 40   function percent(value, max, min) {
 41     return value / Math.abs((max - min));
 42@@ -182,7 +192,12 @@ LandMap.prototype.combine = function(one, two, three, result) {
 43   }
 44 };
 45 
 46-LandMap.prototype.grd = function(amount, percent, featureFrom, featureTo) {
 47+LandMap.prototype.grd = function(options) {
 48+  var amount = options.amount,
 49+    percent = options.percent,
 50+    featureFrom = options.from,
 51+    featureTo = options.to;
 52+
 53   this.maps[featureTo] = new Array(this.size * this.size);
 54 
 55   for (var y = 0; y < this.size; y++) {
 56diff --git a/worker.js b/worker.js
 57index 74bd104..3fb07a7 100644
 58--- a/worker.js
 59+++ b/worker.js
 60@@ -7,10 +7,10 @@ self.addEventListener('message', function(e) {
 61       terrain = new LandMap({
 62         containerId: "container-many"
 63       });
 64-      terrain.generate(0.75, "m1");
 65-      terrain.smooth(10, "m1", "m2");
 66-      terrain.generate(0.75, "p1");
 67-      terrain.smooth(10, "p1", "p2");
 68+      terrain.generate({deviation: 0.75, feature: "m1"});
 69+      terrain.smooth({amount: 10, from: "m1", to:"m2"});
 70+      terrain.generate({deviation: 0.75, feature: "p1"});
 71+      terrain.smooth({amount: 10, from: "p1", to:"p2"});
 72       terrain.complexErosion({
 73         carryingCapacity: 1.5,
 74         depositionSpeed: 0.03,
 75@@ -20,16 +20,16 @@ self.addEventListener('message', function(e) {
 76         two: "p3"
 77       });
 78       //combining
 79-      terrain.combine("p2", "p3", "m2", "c1 from (p2,p3,m2)");
 80-      terrain.combine("p3", "p2", "m2", "c2 from (p3,p2,m2)");
 81+      terrain.combine({one:"p2", two:"p3", three:"m2", result:"c1 from (p2,p3,m2)"});
 82+      terrain.combine({one:"p3", two:"p2", three:"m2", result:"c2 from (p3,p2,m2)"});
 83       break;
 84     case 'sample':
 85       terrain = new LandMap({
 86         containerId: "container-sample"
 87       });
 88-      terrain.generate(0.75, "standard");
 89-      terrain.smooth(10, "standard", "smoothed-10");
 90-      terrain.grd(20, 0.03, "standard", "grd-20-0.03");
 91+      terrain.generate({deviation: 0.75, feature: "standard"});
 92+      terrain.smooth({amount: 10, from: "standard", to:"smoothed-10"});
 93+      terrain.grd({amount: 20, percent: 0.03, from: "standard", to: "grd-20-0.03"});
 94       terrain.complexErosion({
 95         carryingCapacity: 1.5,
 96         depositionSpeed: 0.03,
 97@@ -43,7 +43,7 @@ self.addEventListener('message', function(e) {
 98       terrain = new LandMap({
 99         containerId: "container-complexErosion"
100       });
101-      terrain.generate(0.75, "standard");
102+      terrain.generate({deviation: 0.75, feature: "standard"});
103       terrain.complexErosion({
104         carryingCapacity: 1.5,
105         depositionSpeed: 0.03,
106@@ -81,7 +81,7 @@ self.addEventListener('message', function(e) {
107       terrain = new LandMap({
108         containerId: "container-simpleErosion"
109       });
110-      terrain.generate(0.75, "standard");
111+      terrain.generate({deviation: 0.75, feature: "standard"});
112       terrain.simpleErosion({
113         carryingCapacity: 1.5,
114         depositionSpeed: 0.03,
115@@ -95,29 +95,29 @@ self.addEventListener('message', function(e) {
116       terrain = new LandMap({
117         containerId: "container-grd"
118       });
119-      terrain.generate(0.75, "standard");
120-      terrain.grd(22, 0.01, "standard", "grd-22-0.01");
121-      terrain.grd(20, 0.03, "standard", "grd-20-0.03");
122-      terrain.grd(40, 0.01, "standard", "grd-40-0.01");
123+      terrain.generate({deviation: 0.75, feature: "standard"});
124+      terrain.grd({amount: 22, percent: 0.01, from: "standard", to: "grd-22-0.01"});
125+      terrain.grd({amount: 20, percent: 0.03, from: "standard", to: "grd-20-0.03"});
126+      terrain.grd({amount: 40, percent: 0.01, from: "standard", to: "grd-40-0.01"});
127       break;
128     case 'combined':
129       terrain = new LandMap({
130         containerId: "container-combined"
131       });
132-      terrain.generate(0.75, "standard");
133-      terrain.generate(0.75, "standard-two", "DS with 0.75");
134-      terrain.smooth(10, "standard", "standard-10");
135-      terrain.smooth(20, "standard-two", "standard-two-20");
136-      terrain.combine("standard", "standard-10", "standard-two-20", "combined");
137-      terrain.combine("standard-10", "standard", "standard-two-20", "reversed");
138+      terrain.generate({deviation: 0.75, feature: "standard"});
139+      terrain.generate({deviation: 0.75, feature: "standard-two"});
140+      terrain.smooth({amount: 10, from: "standard", to:"standard-10"});
141+      terrain.smooth({amount: 20, from: "standard-two", to:"standard-two-20"});
142+      terrain.combine({one:"standard", two:"standard-10", three:"standard-two-20", result:"combined"});
143+      terrain.combine({one:"standard-10", two:"standard", three:"standard-two-20", result:"reversed"});
144       break;
145     case 'standard':
146       terrain = new LandMap({
147         containerId: "container-standard"
148       });
149-      terrain.generate(0.75, "standard");
150-      terrain.smooth(10, "standard", "smoothed-10");
151-      terrain.smooth(20, "standard", "smoothed-20");
152+      terrain.generate({deviation: 0.75, feature: "standard"});
153+      terrain.smooth({amount: 10, from: "standard", to:"standard-10"});
154+      terrain.smooth({amount: 20, from: "standard", to:"standard-20"});
155       break;
156   };
157   self.postMessage(terrain);