-
Notifications
You must be signed in to change notification settings - Fork 18
/
land.js
194 lines (189 loc) · 6.44 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
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 frontGeometry = topGeometry.clone().applyMatrix(new THREE.Matrix4().makeRotationFromQuaternion(
new THREE.Quaternion().setFromUnitVectors(new THREE.Vector3(0, 1, 0), new THREE.Vector3(0, 0, 1))
));
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 + 0.5, h + 0.5, y + 0.5))
);
}
}
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 + 0.5, h + 0.5, y + 0.5))
);
}
}
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 + 0.5, h + 0.5, y + 0.5))
);
}
}
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 + 0.5, h + 0.5, y + 0.5))
);
}
}
}
}
}
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 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.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 = parseInt(match[1], 10);
const y1 = parseInt(match[2], 10);
const x2 = parseInt(match[3], 10);
const y2 = parseInt(match[4], 10);
result.push([x1, y1, x2, y2]);
} else if (match[5]) {
const x = parseInt(match[5], 10);
const y = parseInt(match[6], 10);
result.push([x, y, x, y]);
}
}
return result;
};