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
Saving and displaying algorithm options
author
Ben Vogt <[email protected]>
date
2016-05-15 15:59:40
stats
3 file(s) changed, 131 insertions(+), 13 deletions(-)
files
landmap.js
main.js
worker.js
  1diff --git a/landmap.js b/landmap.js
  2index 64a34df..a7eb733 100644
  3--- a/landmap.js
  4+++ b/landmap.js
  5@@ -39,6 +39,7 @@ function LandMap(options) {
  6   this.size = Math.pow(2, level) + 1;
  7   this.max = this.size - 1;
  8   this.maps = options.maps || {};
  9+  this.meta = options.meta || {}
 10 }
 11 
 12 LandMap.prototype.get = function(which, x, y) {
 13@@ -56,6 +57,7 @@ LandMap.prototype.set = function(which, x, y, value) {
 14 LandMap.prototype.generate = function(options) {
 15   var deviationAmount = options.deviation,
 16     feature = options.feature;
 17+  this.meta[options.feature] = options;
 18   var self = this;
 19 
 20   if (!(feature in self.maps)) {
 21@@ -123,6 +125,8 @@ LandMap.prototype.smooth = function(options) {
 22     featureFrom = options.from,
 23     featureTo = options.to;
 24 
 25+  this.meta[featureTo] = options;
 26+
 27   if (!(featureTo in this.maps)) {
 28     this.maps[featureTo] = new Array(this.size * this.size);
 29   }
 30@@ -158,6 +162,8 @@ LandMap.prototype.combine = function(options) {
 31     three = options.three,
 32     result = options.result;
 33 
 34+  this.meta[result] = options;
 35+
 36   function percent(value, max, min) {
 37     return value / Math.abs((max - min));
 38   }
 39@@ -198,6 +204,8 @@ LandMap.prototype.grd = function(options) {
 40     featureFrom = options.from,
 41     featureTo = options.to;
 42 
 43+  this.meta[featureTo] = options;
 44+
 45   this.maps[featureTo] = new Array(this.size * this.size);
 46 
 47   for (var y = 0; y < this.size; y++) {
 48@@ -283,6 +291,8 @@ LandMap.prototype.simpleErosion = function(options) {
 49   var one = options.one;
 50   var two = options.two;
 51 
 52+  this.meta[two] = options;
 53+
 54   var HeightMap = new Array(this.size * this.size);
 55   for (var y = 0; y < this.size; y++) {
 56     for (var x = 0; x < this.size; x++) {
 57@@ -389,6 +399,8 @@ LandMap.prototype.complexErosion = function(options) {
 58   var one = options.one;
 59   var two = options.two;
 60 
 61+  this.meta[two] = options;
 62+
 63   var HeightMap = new Array(this.size * this.size);
 64   for (var y = 0; y < this.size; y++) {
 65     for (var x = 0; x < this.size; x++) {
 66@@ -493,7 +505,7 @@ LandMap.prototype.draw = function() {
 67   var html = '<div class="row">';
 68   var featureCount = 0;
 69   for (feature in this.maps) {
 70-    html += '<div class="four columns"><strong>' + feature + '</strong><div class="box"><span id="' + this.containerId + feature + '"></span><span id="' + feature + '-description"></span></div></div>';
 71+    html += '<div class="four columns"><strong>' + feature + '</strong><br><div class="box"><span id="' + this.containerId + feature + '"></span><pre>' + JSON.stringify(this.meta[feature], null, 2) + '</pre></div></div>';
 72     featureCount++;
 73     if (featureCount == 3) {
 74       html += '</div><div class="row">'
 75diff --git a/main.js b/main.js
 76index 99f1bf4..ca7dcba 100644
 77--- a/main.js
 78+++ b/main.js
 79@@ -7,7 +7,7 @@ $(document).ready(function() {
 80 
 81   $("button").click(function(event) {
 82     spinner("container-" + event.target.id);
 83-    var worker = new Worker('worker.js?salt='+(+(new Date())));//salting to bust cache
 84+    var worker = new Worker('worker.js?salt=' + (+(new Date()))); //salting to bust cache
 85     worker.postMessage(event.target.id);
 86     worker.addEventListener('message', function(e) {
 87       var terrain = new LandMap(e.data);
 88diff --git a/worker.js b/worker.js
 89index 3fb07a7..867ce7d 100644
 90--- a/worker.js
 91+++ b/worker.js
 92@@ -7,10 +7,24 @@ self.addEventListener('message', function(e) {
 93       terrain = new LandMap({
 94         containerId: "container-many"
 95       });
 96-      terrain.generate({deviation: 0.75, feature: "m1"});
 97-      terrain.smooth({amount: 10, from: "m1", to:"m2"});
 98-      terrain.generate({deviation: 0.75, feature: "p1"});
 99-      terrain.smooth({amount: 10, from: "p1", to:"p2"});
100+      terrain.generate({
101+        deviation: 0.75,
102+        feature: "m1"
103+      });
104+      terrain.smooth({
105+        amount: 10,
106+        from: "m1",
107+        to: "m2"
108+      });
109+      terrain.generate({
110+        deviation: 0.75,
111+        feature: "p1"
112+      });
113+      terrain.smooth({
114+        amount: 10,
115+        from: "p1",
116+        to: "p2"
117+      });
118       terrain.complexErosion({
119         carryingCapacity: 1.5,
120         depositionSpeed: 0.03,
121@@ -20,16 +34,38 @@ self.addEventListener('message', function(e) {
122         two: "p3"
123       });
124       //combining
125-      terrain.combine({one:"p2", two:"p3", three:"m2", result:"c1 from (p2,p3,m2)"});
126-      terrain.combine({one:"p3", two:"p2", three:"m2", result:"c2 from (p3,p2,m2)"});
127+      terrain.combine({
128+        one: "p2",
129+        two: "p3",
130+        three: "m2",
131+        result: "c1 from (p2,p3,m2)"
132+      });
133+      terrain.combine({
134+        one: "p3",
135+        two: "p2",
136+        three: "m2",
137+        result: "c2 from (p3,p2,m2)"
138+      });
139       break;
140     case 'sample':
141       terrain = new LandMap({
142         containerId: "container-sample"
143       });
144-      terrain.generate({deviation: 0.75, feature: "standard"});
145-      terrain.smooth({amount: 10, from: "standard", to:"smoothed-10"});
146-      terrain.grd({amount: 20, percent: 0.03, from: "standard", to: "grd-20-0.03"});
147+      terrain.generate({
148+        deviation: 0.75,
149+        feature: "standard"
150+      });
151+      terrain.smooth({
152+        amount: 10,
153+        from: "standard",
154+        to: "smoothed-10"
155+      });
156+      terrain.grd({
157+        amount: 20,
158+        percent: 0.03,
159+        from: "standard",
160+        to: "grd-20-0.03"
161+      });
162       terrain.complexErosion({
163         carryingCapacity: 1.5,
164         depositionSpeed: 0.03,
165@@ -43,7 +79,10 @@ self.addEventListener('message', function(e) {
166       terrain = new LandMap({
167         containerId: "container-complexErosion"
168       });
169-      terrain.generate({deviation: 0.75, feature: "standard"});
170+      terrain.generate({
171+        deviation: 0.75,
172+        feature: "standard"
173+      });
174       terrain.complexErosion({
175         carryingCapacity: 1.5,
176         depositionSpeed: 0.03,
177@@ -81,7 +120,10 @@ self.addEventListener('message', function(e) {
178       terrain = new LandMap({
179         containerId: "container-simpleErosion"
180       });
181-      terrain.generate({deviation: 0.75, feature: "standard"});
182+      terrain.generate({
183+        deviation: 0.75,
184+        feature: "standard"
185+      });
186       terrain.simpleErosion({
187         carryingCapacity: 1.5,
188         depositionSpeed: 0.03,
189@@ -95,29 +137,82 @@ self.addEventListener('message', function(e) {
190       terrain = new LandMap({
191         containerId: "container-grd"
192       });
193-      terrain.generate({deviation: 0.75, feature: "standard"});
194-      terrain.grd({amount: 22, percent: 0.01, from: "standard", to: "grd-22-0.01"});
195-      terrain.grd({amount: 20, percent: 0.03, from: "standard", to: "grd-20-0.03"});
196-      terrain.grd({amount: 40, percent: 0.01, from: "standard", to: "grd-40-0.01"});
197+      terrain.generate({
198+        deviation: 0.75,
199+        feature: "standard"
200+      });
201+      terrain.grd({
202+        amount: 22,
203+        percent: 0.01,
204+        from: "standard",
205+        to: "grd-22-0.01"
206+      });
207+      terrain.grd({
208+        amount: 20,
209+        percent: 0.03,
210+        from: "standard",
211+        to: "grd-20-0.03"
212+      });
213+      terrain.grd({
214+        amount: 40,
215+        percent: 0.01,
216+        from: "standard",
217+        to: "grd-40-0.01"
218+      });
219       break;
220     case 'combined':
221       terrain = new LandMap({
222         containerId: "container-combined"
223       });
224-      terrain.generate({deviation: 0.75, feature: "standard"});
225-      terrain.generate({deviation: 0.75, feature: "standard-two"});
226-      terrain.smooth({amount: 10, from: "standard", to:"standard-10"});
227-      terrain.smooth({amount: 20, from: "standard-two", to:"standard-two-20"});
228-      terrain.combine({one:"standard", two:"standard-10", three:"standard-two-20", result:"combined"});
229-      terrain.combine({one:"standard-10", two:"standard", three:"standard-two-20", result:"reversed"});
230+      terrain.generate({
231+        deviation: 0.75,
232+        feature: "standard"
233+      });
234+      terrain.generate({
235+        deviation: 0.75,
236+        feature: "standard-two"
237+      });
238+      terrain.smooth({
239+        amount: 10,
240+        from: "standard",
241+        to: "standard-10"
242+      });
243+      terrain.smooth({
244+        amount: 20,
245+        from: "standard-two",
246+        to: "standard-two-20"
247+      });
248+      terrain.combine({
249+        one: "standard",
250+        two: "standard-10",
251+        three: "standard-two-20",
252+        result: "combined"
253+      });
254+      terrain.combine({
255+        one: "standard-10",
256+        two: "standard",
257+        three: "standard-two-20",
258+        result: "reversed"
259+      });
260       break;
261     case 'standard':
262       terrain = new LandMap({
263         containerId: "container-standard"
264       });
265-      terrain.generate({deviation: 0.75, feature: "standard"});
266-      terrain.smooth({amount: 10, from: "standard", to:"standard-10"});
267-      terrain.smooth({amount: 20, from: "standard", to:"standard-20"});
268+      terrain.generate({
269+        deviation: 0.75,
270+        feature: "standard"
271+      });
272+      terrain.smooth({
273+        amount: 10,
274+        from: "standard",
275+        to: "standard-10"
276+      });
277+      terrain.smooth({
278+        amount: 20,
279+        from: "standard",
280+        to: "standard-20"
281+      });
282       break;
283   };
284   self.postMessage(terrain);