-
Notifications
You must be signed in to change notification settings - Fork 92
/
geometry.ts
245 lines (218 loc) · 9.56 KB
/
geometry.ts
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* From http://www.redblobgames.com/maps/magpen4/
* Copyright 2017 Red Blob Games <[email protected]>
* License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
*/
import {vec2} from 'gl-matrix';
import Map from "./map.ts";
import type {Mesh} from "./types.d.ts";
/**
* Fill a buffer with data from the mesh.
*/
function setMeshGeometry(mesh: Mesh, P: Float32Array) {
let {numRegions, numTriangles} = mesh;
if (P.length !== 2 * (numRegions + numTriangles)) { throw "wrong size"; }
let p = 0;
for (let r = 0; r < numRegions; r++) {
P[p++] = mesh.x_of_r(r);
P[p++] = mesh.y_of_r(r);
}
for (let t = 0; t < numTriangles; t++) {
P[p++] = mesh.x_of_t(t);
P[p++] = mesh.y_of_t(t);
}
};
/**
* Fill an indexed buffer with data from the map.
*/
function setMapGeometry(map: Map, I: Int32Array, P: Float32Array) {
// TODO: V should probably depend on the slope, or elevation, or maybe it should be 0.95 in mountainous areas and 0.99 elsewhere
const V = 0.95; // reduce elevation in valleys
let {mesh, flow_s, elevation_r, elevation_t, rainfall_r} = map;
let {numSolidSides, numRegions, numTriangles, is_boundary_t} = mesh;
if (I.length !== 3 * numSolidSides) { throw "wrong size"; }
if (P.length !== 2 * (numRegions + numTriangles)) { throw "wrong size"; }
let p = 0;
for (let r = 0; r < numRegions; r++) {
P[p++] = elevation_r[r];
P[p++] = rainfall_r[r];
}
for (let t = 0; t < numTriangles; t++) {
P[p++] = V * elevation_t[t];
let s0 = 3*t;
let r1 = mesh.r_begin_s(s0),
r2 = mesh.r_begin_s(s0+1),
r3 = mesh.r_begin_s(s0+2);
P[p++] = 1/3 * (rainfall_r[r1] + rainfall_r[r2] + rainfall_r[r3]);
}
let i = 0;
for (let s = 0; s < numSolidSides; s++) {
let s_opposite = mesh.s_opposite_s(s),
r1 = mesh.r_begin_s(s),
r2 = mesh.r_begin_s(s_opposite),
t1 = mesh.t_inner_s(s),
t2 = mesh.t_inner_s(s_opposite);
// Each quadrilateral is turned into two triangles, so each
// half-edge gets turned into one. There are two ways to fold
// a quadrilateral. This is usually a nuisance but in this
// case it's a feature. See the explanation here
// https://www.redblobgames.com/x/1725-procedural-elevation/#rendering
let is_valley = false;
if (elevation_r[r1] < 0.0 || elevation_r[r2] < 0.0) is_valley = true;
if (flow_s[s] > 0 || flow_s[s_opposite] > 0) is_valley = true;
if (is_boundary_t[t1] || is_boundary_t[t2]) is_valley = false;
if (is_valley) {
// It's a coastal or river edge, forming a valley
I[i++] = r1; I[i++] = numRegions+t2; I[i++] = numRegions+t1;
} else {
// It's a ridge
I[i++] = r1; I[i++] = r2; I[i++] = numRegions+t1;
}
}
if (I.length !== i) { throw "wrong size"; }
if (P.length !== p) { throw "wrong size"; }
};
/**
* Create a bitmap that will be used for texture mapping
* BEND textures will be ordered: {blank side, input side, output side}
* FORK textures will be ordered: {passive input side, active input side, output side}
*
* Cols will be the input flow rate
* Rows will be the output flow rate
*/
function assignTextureCoordinates(spacing: number, numSizes: number, textureSize: number) {
/* create (numSizes+1)^2 size combinations, each with two triangles */
function UV(x: number, y: number) {
return {xy: [x, y], uv: [(x+0.5)/textureSize, (y+0.5)/textureSize]};
}
let triangles: any = [[]];
let width = Math.floor((textureSize - 2*spacing) / (2*numSizes+3)) - spacing,
height = Math.floor((textureSize - 2*spacing) / (numSizes+1)) - spacing;
for (let row = 0; row <= numSizes; row++) {
triangles[row] = [];
for (let col = 0; col <= numSizes; col++) {
let baseX = spacing + (2 * spacing + 2 * width) * col,
baseY = spacing + (spacing + height) * row;
triangles[row][col] = [
[UV(baseX + width, baseY),
UV(baseX, baseY + height),
UV(baseX + 2*width, baseY + height)],
[UV(baseX + 2*width + spacing, baseY + height),
UV(baseX + 3*width + spacing, baseY),
UV(baseX + width + spacing, baseY)]
];
}
}
return triangles;
}
// TODO: turn this into an object :-/
const riverTextureSpacing = 20; // TODO: should depend on river size
const numRiverSizes = 24; // NOTE: too high and rivers are low quality; too low and there's not enough variation
const riverTextureSize = 2048;
const riverMaximumFractionOfWidth = 0.5;
const riverTexturePositions = assignTextureCoordinates(riverTextureSpacing, numRiverSizes, riverTextureSize);
function createRiverBitmap() {
let canvas = document.createElement('canvas');
canvas.width = canvas.height = riverTextureSize;
let ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
function lineWidth(i: number) {
const spriteSize = riverTexturePositions[0][1][0][0].xy[0] - riverTexturePositions[0][0][0][0].xy[0];
return i / numRiverSizes * spriteSize * riverMaximumFractionOfWidth;
}
ctx.lineCap = "round";
for (let row = 0; row <= numRiverSizes; row++) {
for (let col = 0; col <= numRiverSizes; col++) {
for (let type = 0; type < 2; type++) {
let pos = riverTexturePositions[row][col][type];
ctx.save();
ctx.beginPath();
ctx.rect(pos[1].xy[0] - riverTextureSpacing/2, pos[0].xy[1] - riverTextureSpacing/2,
pos[2].xy[0] - pos[1].xy[0] + riverTextureSpacing, pos[2].xy[1] - pos[0].xy[1] + riverTextureSpacing);
// ctx.clip(); // TODO: to make this work right, the spacing needs to vary based on the river size, I think
let center = [(pos[0].xy[0] + pos[1].xy[0] + pos[2].xy[0]) / 3,
(pos[0].xy[1] + pos[1].xy[1] + pos[2].xy[1]) / 3];
let midpoint12 = vec2.lerp(vec2.create(), pos[1].xy, pos[2].xy, 0.5);
let midpoint20 = vec2.lerp(vec2.create(), pos[2].xy, pos[0].xy, 0.5);
ctx.strokeStyle = "hsl(200,50%,35%)";
if (type === 1) {
// TODO: river delta/fork sprite
} else {
if (col > 0) {
ctx.lineWidth = Math.min(lineWidth(col), lineWidth(row));
ctx.beginPath();
ctx.moveTo(midpoint12[0], midpoint12[1]);
ctx.quadraticCurveTo(center[0], center[1], midpoint20[0], midpoint20[1]);
ctx.stroke();
} else {
ctx.lineWidth = lineWidth(row);
ctx.beginPath();
ctx.moveTo(center[0], center[1]);
ctx.lineTo(midpoint20[0], midpoint20[1]);
ctx.stroke();
}
}
ctx.restore();
}
}
}
return canvas;
};
export function clamp(x: number, lo: number, hi: number): number {
if (x < lo) { x = lo; }
if (x > hi) { x = hi; }
return x;
}
/**
* Fill a buffer with river geometry
*/
function setRiverTextures(map: Map, spacing: number, riversParam: any, P: Float32Array): number {
const MIN_FLOW = Math.exp(riversParam.lg_min_flow);
const RIVER_WIDTH = Math.exp(riversParam.lg_river_width);
let {mesh, s_downslope_t, flow_s} = map;
let {numSolidTriangles, length_s} = mesh;
function riverSize(s: number, flow: number): number {
// TODO: performance: build a table of flow to width
if (s < 0) { return 1; }
let width = Math.sqrt(flow - MIN_FLOW) * spacing * RIVER_WIDTH;
let size = Math.ceil(width * numRiverSizes / length_s[s]);
return clamp(size, 1, numRiverSizes);
}
let p = 0;
for (let t = 0; t < numSolidTriangles; t++) {
let s_out = s_downslope_t[t];
let outflow = flow_s[s_out];
if (s_out < 0 || outflow < MIN_FLOW) continue;
let r1 = mesh.r_begin_s(3*t ),
r2 = mesh.r_begin_s(3*t + 1),
r3 = mesh.r_begin_s(3*t + 2);
let in1_s = mesh.s_next_s(s_out);
let in2_s = mesh.s_next_s(in1_s);
let in1_flow = flow_s[mesh.s_opposite_s(in1_s)];
let in2_flow = flow_s[mesh.s_opposite_s(in2_s)];
let textureRow = riverSize(s_out, outflow);
function add(r: number, c: number, i: number, j: number, k: number) {
const T = riverTexturePositions[r][c][0];
P[p ] = mesh.x_of_r(r1);
P[p + 1] = mesh.y_of_r(r1);
P[p + 4] = mesh.x_of_r(r2);
P[p + 5] = mesh.y_of_r(r2);
P[p + 8] = mesh.x_of_r(r3);
P[p + 9] = mesh.y_of_r(r3);
P[p + 4*(s_out - 3*t) + 2] = T[i].uv[0];
P[p + 4*(s_out - 3*t) + 3] = T[i].uv[1];
P[p + 4*(in1_s - 3*t) + 2] = T[j].uv[0];
P[p + 4*(in1_s - 3*t) + 3] = T[j].uv[1];
P[p + 4*(in2_s - 3*t) + 2] = T[k].uv[0];
P[p + 4*(in2_s - 3*t) + 3] = T[k].uv[1];
p += 12;
}
if (in1_flow >= MIN_FLOW) {
add(textureRow, riverSize(in1_s, in1_flow), 0, 2, 1);
}
if (in2_flow >= MIN_FLOW) {
add(textureRow, riverSize(in2_s, in2_flow), 2, 1, 0);
}
}
return p / 12;
};
export default {setMeshGeometry, createRiverBitmap, setMapGeometry, setRiverTextures};