forked from redblobgames/mapgen4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
painting.js
215 lines (189 loc) · 7.87 KB
/
painting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* From https://www.redblobgames.com/maps/mapgen4/
* Copyright 2018 Red Blob Games <[email protected]>
* License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
*
* This module allows the user to paint constraints for the map generator
*/
'use strict';
/* global Draggable */
/*
* The painting interface uses a square array of elevations. As you
* drag the mouse it will paint filled circles into the elevation map,
* then send the elevation map to the generator to produce the output.
*/
const SimplexNoise = require('simplex-noise');
const {makeRandFloat} = require('@redblobgames/prng');
const CANVAS_SIZE = 128;
const currentStroke = {
/* elevation before the current paint stroke began */
previousElevation: new Float32Array(CANVAS_SIZE * CANVAS_SIZE),
/* how long, in milliseconds, was spent painting */
time: new Float32Array(CANVAS_SIZE * CANVAS_SIZE),
/* maximum strength applied */
strength: new Float32Array(CANVAS_SIZE * CANVAS_SIZE),
};
/* The elevation is -1.0 to 0.0 → water, 0.0 to +1.0 → land */
class Generator {
constructor () {
this.userHasPainted = false;
this.elevation = new Float32Array(CANVAS_SIZE * CANVAS_SIZE);
}
setElevationParam(elevationParam) {
if ( elevationParam.seed !== this.seed
|| elevationParam.island !== this.island) {
this.seed = elevationParam.seed;
this.island = elevationParam.island;
this.generate();
}
}
/** Use a noise function to determine the shape */
generate() {
const {elevation, island} = this;
const noise = new SimplexNoise(makeRandFloat(this.seed));
const persistence = 1/2;
const amplitudes = Array.from({length: 5}, (_, octave) => Math.pow(persistence, octave));
function fbm_noise(nx, ny) {
let sum = 0, sumOfAmplitudes = 0;
for (let octave = 0; octave < amplitudes.length; octave++) {
let frequency = 1 << octave;
sum += amplitudes[octave] * noise.noise2D(nx * frequency, ny * frequency);
sumOfAmplitudes += amplitudes[octave];
}
return sum / sumOfAmplitudes;
}
for (let y = 0; y < CANVAS_SIZE; y++) {
for (let x = 0; x < CANVAS_SIZE; x++) {
let p = y * CANVAS_SIZE + x;
let nx = 2 * x/CANVAS_SIZE - 1,
ny = 2 * y/CANVAS_SIZE - 1;
let distance = Math.max(Math.abs(nx), Math.abs(ny));
let e = 0.5 * (fbm_noise(nx, ny) + island * (0.75 - 2 * distance * distance));
if (e < -1.0) { e = -1.0; }
if (e > +1.0) { e = +1.0; }
elevation[p] = e;
if (e > 0.0) {
let m = (0.5 * noise.noise2D(nx + 30, ny + 50)
+ 0.5 * noise.noise2D(2*nx + 33, 2*ny + 55));
// TODO: make some of these into parameters
let mountain = Math.min(1.0, e * 5.0) * (1 - Math.abs(m) / 0.5);
if (mountain > 0.0) {
elevation[p] = Math.max(e, Math.min(e * 3, mountain));
}
}
}
}
this.userHasPainted = false;
}
/**
* Paint a circular region
*
* @param {{elevation: number}} tool
* @param {number} x0 - should be 0 to 1
* @param {number} y0 - should be 0 to 1
* @param {{innerRadius: number, outerRadius: number, rate: number}} size
* @param {number} deltaTimeInMs
*/
paintAt(tool, x0, y0, size, deltaTimeInMs) {
let {elevation} = this;
/* This has two effects: first time you click the mouse it has a
* strong effect, and it also limits the amount in case you
* pause */
deltaTimeInMs = Math.min(100, deltaTimeInMs);
let newElevation = tool.elevation;
let {innerRadius, outerRadius, rate} = size;
let xc = (x0 * CANVAS_SIZE) | 0, yc = (y0 * CANVAS_SIZE) | 0;
let top = Math.max(0, yc - outerRadius),
bottom = Math.min(CANVAS_SIZE-1, yc + outerRadius);
for (let y = top; y <= bottom; y++) {
let s = Math.sqrt(outerRadius * outerRadius - (y - yc) * (y - yc)) | 0;
let left = Math.max(0, xc - s),
right = Math.min(CANVAS_SIZE-1, xc + s);
for (let x = left; x <= right; x++) {
let p = y * CANVAS_SIZE + x;
let distance = Math.sqrt((x - xc) * (x - xc) + (y - yc) * (y - yc));
let strength = 1.0 - Math.min(1, Math.max(0, (distance - innerRadius) / (outerRadius - innerRadius)));
let factor = rate/1000 * deltaTimeInMs;
currentStroke.time[p] += strength * factor;
if (strength > currentStroke.strength[p]) {
currentStroke.strength[p] = (1 - factor) * currentStroke.strength[p] + factor * strength;
}
let mix = currentStroke.strength[p] * Math.min(1, currentStroke.time[p]);
elevation[p] = (1 - mix) * currentStroke.previousElevation[p] + mix * newElevation;
}
}
this.userHasPainted = true;
}
}
let heightMap = new Generator();
document.getElementById('button-reset').addEventListener('click', () => {
heightMap.generate();
exports.onUpdate();
});
const SIZES = {
// rate is effect per second
small: {key: '1', rate: 8, innerRadius: 2, outerRadius: 6},
medium: {key: '2', rate: 5, innerRadius: 5, outerRadius: 10},
large: {key: '3', rate: 3, innerRadius: 10, outerRadius: 16},
};
const TOOLS = {
ocean: {elevation: -0.25},
shallow: {elevation: -0.05},
valley: {elevation: +0.05},
mountain: {elevation: +1.0},
};
let currentTool = 'mountain';
let currentSize = 'small';
function displayCurrentTool() {
const className = 'current-control';
for (let c of document.querySelectorAll("."+className)) {
c.classList.remove(className);
}
document.getElementById(currentTool).classList.add(className);
document.getElementById(currentSize).classList.add(className);
}
/** @type {[string, string, function][]} */
const controls = [
['1', "small", () => { currentSize = 'small'; }],
['2', "medium", () => { currentSize = 'medium'; }],
['3', "large", () => { currentSize = 'large'; }],
['q', "ocean", () => { currentTool = 'ocean'; }],
['w', "shallow", () => { currentTool = 'shallow'; }],
['e', "valley", () => { currentTool = 'valley'; }],
['r', "mountain", () => { currentTool = 'mountain'; }],
];
window.addEventListener('keydown', e => {
for (let control of controls) {
if (e.key === control[0]) { control[2](); displayCurrentTool(); }
}
});
for (let control of controls) {
document.getElementById(control[1]).addEventListener('click', () => { control[2](); displayCurrentTool(); } );
}
displayCurrentTool();
const output = document.getElementById('mapgen4');
new Draggable({
el: output,
start(event) {
this.timestamp = Date.now();
currentStroke.time.fill(0);
currentStroke.strength.fill(0);
currentStroke.previousElevation.set(heightMap.elevation);
this.drag(event);
},
drag(event) {
const nowMs = Date.now();
let coords = [event.x / output.clientWidth,
event.y / output.clientHeight];
coords = exports.screenToWorldCoords(coords);
heightMap.paintAt(TOOLS[currentTool], coords[0], coords[1], SIZES[currentSize], nowMs - this.timestamp);
this.timestamp = nowMs;
exports.onUpdate();
},
});
exports.screenToWorldCoords = coords => coords;
exports.onUpdate = () => {};
exports.size = CANVAS_SIZE;
exports.constraints = heightMap.elevation;
exports.setElevationParam = elevationParam => heightMap.setElevationParam(elevationParam);
exports.userHasPainted = () => heightMap.userHasPainted;