-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathland.js
304 lines (299 loc) · 9.75 KB
/
land.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* const topGeometry = new THREE.BoxBufferGeometry(0.01, 1, 0.01);
const leftGeometry = topGeometry.clone().applyMatrix(new THREE.Matrix4().makeRotationFromQuaternion(
new THREE.Quaternion().setFromUnitVectors(new THREE.Vector3(0, 1, 0), new THREE.Vector3(-1, 0, 0))
));
const boxGeometry = THREE.BufferGeometryUtils.mergeBufferGeometries([
topGeometry.clone().applyMatrix(new THREE.Matrix4().makeTranslation(-0.5, 0, -0.5)),
topGeometry.clone().applyMatrix(new THREE.Matrix4().makeTranslation(0.5, 0, -0.5)),
leftGeometry.clone().applyMatrix(new THREE.Matrix4().makeTranslation(0, 0.5, -0.5)),
leftGeometry.clone().applyMatrix(new THREE.Matrix4().makeTranslation(0, -0.5, -0.5)),
]);
THREE.Guardian = function Guardian(extents, distanceFactor, color) {
const _makeGeometry = () => {
const pixels = {};
for (let i = 0; i < extents.length; i++) {
const [x1, y1, x2, y2] = extents[i];
for (let x = x1; x < x2; x++) {
for (let y = y1; y < y2; y++) {
pixels[`${x}:${y}`] = true;
}
}
}
const boxGeometries = [];
for (let i = 0; i < extents.length; i++) {
const [x1, y1, x2, y2] = extents[i];
for (let x = x1; x < x2; x++) {
for (let y = y1; y < y2; y++) {
const hasCoords = {
left: pixels[`${x-1}:${y}`],
right: pixels[`${x+1}:${y}`],
up: pixels[`${x}:${y-1}`],
down: pixels[`${x}:${y+1}`],
};
if (!hasCoords.up) {
for (let h = 0; h <= 2; h++) {
boxGeometries.push(boxGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeTranslation(x, h + 0.5, y))
);
}
}
if (!hasCoords.down) {
for (let h = 0; h <= 2; h++) {
boxGeometries.push(boxGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeRotationFromQuaternion(new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI)))
.applyMatrix(new THREE.Matrix4().makeTranslation(x, h + 0.5, y))
);
}
}
if (!hasCoords.left) {
for (let h = 0; h <= 2; h++) {
boxGeometries.push(boxGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeRotationFromQuaternion(new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI/2)))
.applyMatrix(new THREE.Matrix4().makeTranslation(x, h + 0.5, y))
);
}
}
if (!hasCoords.right) {
for (let h = 0; h <= 2; h++) {
boxGeometries.push(boxGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeRotationFromQuaternion(new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), -Math.PI/2)))
.applyMatrix(new THREE.Matrix4().makeTranslation(x, h + 0.5, y))
);
}
}
}
}
}
return THREE.BufferGeometryUtils.mergeBufferGeometries(boxGeometries);
};
const geometry = _makeGeometry();
const gridVsh = `
varying vec3 vWorldPos;
// varying vec2 vUv;
varying float vDepth;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.);
// vUv = uv;
vWorldPos = abs(position);
vDepth = gl_Position.z / ${distanceFactor.toFixed(8)};
}
`;
const gridFsh = `
// uniform sampler2D uTex;
uniform vec3 uColor;
uniform float uAnimation;
varying vec3 vWorldPos;
varying float vDepth;
void main() {
gl_FragColor = vec4(uColor, (1.0-vDepth)*uAnimation);
}
`;
const material = new THREE.ShaderMaterial({
uniforms: {
uColor: {
type: 'c',
value: new THREE.Color(color),
},
uAnimation: {
type: 'f',
value: 1,
},
},
vertexShader: gridVsh,
fragmentShader: gridFsh,
transparent: true,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.frustumCulled = false;
mesh.setColor = c => {
mesh.material.uniforms.uColor.value.setHex(c);
};
return mesh;
}; */
const wallGeometry = (() => {
const panelGeometries = [];
for (let x = -8; x <= 8; x++) {
panelGeometries.push(
new THREE.BoxBufferGeometry(0.01, 2, 0.01)
.applyMatrix(new THREE.Matrix4().makeTranslation(x, 1, -8))
);
}
for (let h = 0; h <= 2; h++) {
panelGeometries.push(
new THREE.BoxBufferGeometry(16, 0.01, 0.01)
.applyMatrix(new THREE.Matrix4().makeTranslation(0, h, -8))
);
}
return THREE.BufferGeometryUtils.mergeBufferGeometries(panelGeometries);
})();
const topWallGeometry = wallGeometry.clone()
// .applyMatrix(new THREE.Matrix4().makeTranslation(-0.5, 0, -0.5));
const leftWallGeometry = wallGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeRotationAxis(new THREE.Vector3(0, 1, 0), Math.PI/2))
// .applyMatrix(new THREE.Matrix4().makeTranslation(-0.5, 0, -0.5));
const rightWallGeometry = wallGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeRotationAxis(new THREE.Vector3(0, 1, 0), -Math.PI/2))
// .applyMatrix(new THREE.Matrix4().makeTranslation(-0.5, 0, -0.5));
const bottomWallGeometry = wallGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeRotationAxis(new THREE.Vector3(0, 1, 0), Math.PI))
// .applyMatrix(new THREE.Matrix4().makeTranslation(-0.5, 0, -0.5));
const distanceFactor = 64;
THREE.Parcel = function Guardian(extents, color) {
const geometry = (() => {
const geometries = [];
const [[x1, y1, x2, y2]] = extents;
const ax1 = (x1 + 8)/16;
const ay1 = (y1 + 8)/16;
const ax2 = (x2 + 8)/16;
const ay2 = (y2 + 8)/16;
for (let x = ax1; x < ax2; x++) {
geometries.push(
topWallGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeTranslation(x*16, 0, ay1*16))
);
geometries.push(
bottomWallGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeTranslation(x*16, 0, (ay2-1)*16))
);
}
for (let y = ay1; y < ay2; y++) {
geometries.push(
leftWallGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeTranslation(ax1*16, 0, y*16))
);
geometries.push(
rightWallGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeTranslation((ax2-1)*16, 0, y*16))
);
}
return THREE.BufferGeometryUtils.mergeBufferGeometries(geometries);
})();
const gridVsh = `
// varying vec3 vWorldPos;
// varying vec2 vUv;
varying float vDepth;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.);
// vUv = uv;
// vWorldPos = abs(position);
vDepth = gl_Position.z / ${distanceFactor.toFixed(8)};
}
`;
const gridFsh = `
// uniform sampler2D uTex;
uniform vec3 uColor;
// uniform float uAnimation;
// varying vec3 vWorldPos;
varying float vDepth;
void main() {
gl_FragColor = vec4(uColor, (1.0-vDepth));
}
`;
const material = new THREE.ShaderMaterial({
uniforms: {
uColor: {
type: 'c',
value: new THREE.Color(color),
},
},
vertexShader: gridVsh,
fragmentShader: gridFsh,
transparent: true,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.frustumCulled = false;
mesh.setColor = c => {
mesh.material.uniforms.uColor.value.setHex(c);
};
return mesh;
};
/* const planeGeometry = new THREE.PlaneBufferGeometry(1, 1)
.applyMatrix(new THREE.Matrix4().makeRotationFromQuaternion(new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1, 0, 0), -Math.PI/2)))
.applyMatrix(new THREE.Matrix4().makeTranslation(0.5, 0.01, 0.5));
THREE.Land = function Land(extents, color) {
const _makeGeometry = () => {
const geometries = [];
for (let i = 0; i < extents.length; i++) {
const [x1, y1, x2, y2] = extents[i];
for (let x = x1; x < x2; x++) {
for (let y = y1; y < y2; y++) {
geometries.push(
planeGeometry.clone()
.applyMatrix(new THREE.Matrix4().makeTranslation(x, 0, y))
);
}
}
}
return THREE.BufferGeometryUtils.mergeBufferGeometries(geometries);
};
const geometry = _makeGeometry();
const baseVsh = `
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.);
}
`;
const baseFsh = `
uniform vec3 uColor;
uniform float uAnimation;
void main() {
gl_FragColor = vec4(uColor, uAnimation*0.5);
}
`;
const material = new THREE.ShaderMaterial({
uniforms: {
uColor: {
type: 'c',
value: new THREE.Color(color),
},
uAnimation: {
type: 'f',
value: 1,
},
},
vertexShader: baseVsh,
fragmentShader: baseFsh,
side: THREE.DoubleSide,
transparent: true,
});
const mesh = new THREE.Mesh(geometry, material);
mesh.frustumCulled = false;
mesh.setColor = c => {
mesh.material.uniforms.uColor.value.setHex(c);
};
return mesh;
}; */
THREE.Land = {};
THREE.Land.parseExtents = s => {
const regex = /(?:\[(-?[0-9]+)\s+(-?[0-9]+)\s+(-?[0-9]+)\s+(-?[0-9]+)\]|(-?[0-9]+)\s+(-?[0-9]+))\s*/g;
const result = [];
let match;
while (match = regex.exec(s)) {
if (match[1]) {
const x1 = parseFloat(match[1]);
const y1 = parseFloat(match[2]);
const x2 = parseFloat(match[3]);
const y2 = parseFloat(match[4]);
result.push([x1, y1, x2, y2]);
} else if (match[5]) {
const x = parseFloat(match[5]);
const y = parseFloat(match[6]);
result.push([x, y, x, y]);
}
}
return result;
};
THREE.Land.serializeExtents = extents => {
let result = '';
for (let i = 0; i < extents.length; i++) {
if (result) {
result += ' ';
}
const [x1, y1, x2, y2] = extents[i];
if (x1 !== x2 || y1 !== y2) {
result += `[${x1} ${y1} ${x2} ${y2}]`;
} else {
result += `${x1} ${y1}`;
}
}
return result;
};