This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 197
/
threejs_shadows.ts
260 lines (230 loc) · 8.88 KB
/
threejs_shadows.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright (C) 2017-2019 HERE Europe B.V.
* Licensed under Apache 2.0, see full license in LICENSE
* SPDX-License-Identifier: Apache-2.0
*/
import * as THREE from "three";
import {
isValueDefinition,
StandardStyle,
Style,
StyleDeclaration,
Theme
} from "@here/harp-datasource-protocol";
import { isJsonExpr } from "@here/harp-datasource-protocol/lib/Expr";
import { GeoCoordinates } from "@here/harp-geoutils";
import { MapControls, MapControlsUI } from "@here/harp-map-controls";
import {
CopyrightElementHandler,
CopyrightInfo,
MapView,
MapViewEventNames,
ThemeLoader
} from "@here/harp-mapview";
import { APIFormat, OmvDataSource } from "@here/harp-omv-datasource";
import { GUI } from "dat.gui";
import { ShadowMapViewer } from "three/examples/jsm/utils/ShadowMapViewer";
import { accessToken } from "../config";
import { Box3, Euler } from "three";
export namespace ThreejsShadows {
function initializeMapView(id: string, theme: Theme): MapView {
const canvas = document.getElementById(id) as HTMLCanvasElement;
const map = new MapView({ canvas, theme, enableShadows: true });
map.renderLabels = false;
CopyrightElementHandler.install("copyrightNotice", map);
const mapControls = new MapControls(map);
mapControls.maxTiltAngle = 50;
const NY = new GeoCoordinates(40.707, -74.01);
map.lookAt(NY, 2000, 45, 0);
const ui = new MapControlsUI(mapControls);
canvas.parentElement!.appendChild(ui.domElement);
map.resize(window.innerWidth, window.innerHeight);
window.addEventListener("resize", () => {
map.resize(window.innerWidth, window.innerHeight);
});
addOmvDataSource(map);
map.update();
return map;
}
function addOmvDataSource(map: MapView) {
const hereCopyrightInfo: CopyrightInfo = {
id: "here.com",
year: new Date().getFullYear(),
label: "HERE",
link: "https://legal.here.com/terms"
};
const copyrights: CopyrightInfo[] = [hereCopyrightInfo];
const omvDataSource = new OmvDataSource({
baseUrl: "https://xyz.api.here.com/tiles/herebase.02",
apiFormat: APIFormat.XYZOMV,
styleSetName: "tilezen",
maxZoomLevel: 17,
authenticationCode: accessToken,
copyrightInfo: copyrights
});
const promise = map.addDataSource(omvDataSource);
const options = {
top: 5000,
left: -5000,
right: 5000,
bottom: -5000,
far: 5000,
near: 0
};
map.renderer.shadowMap.enabled = true;
map.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
let shadowMapViewerCreated = false;
const updateLightCamera = (light: THREE.DirectionalLight) => {
const lightCamera = light.shadow.camera;
const lightQuaternion = lightCamera.quaternion;
const camera = new THREE.Camera();
camera.projectionMatrixInverse.copy(
map.camera.projectionMatrixInverse
);
const w = 1;
const h = 1;
const NDCToView = (x: number, y: number, z: number) => {
return new THREE.Vector3(x, y, z)
.unproject(camera)
// .applyQuaternion(lightQuaternion)
// .sub(lightCamera.position);
}
// near
const n1 = NDCToView(-w, -h, -1);
const n2 = NDCToView(w, -h, -1)
const n3 = NDCToView(-w, h, -1)
const n4 = NDCToView(w, h, -1)
// far
const f1 = NDCToView(-w, -h, 1)
const f2 = NDCToView(w, -h, 1)
const f3 = NDCToView(-w, h, 1)
const f4 = NDCToView(w, h, 1)
console.log(new Euler().setFromQuaternion(lightQuaternion));
const frustumPoints = [n1, n2, n3, n4, f1, f2, f3, f4];
console.log(n1);
console.log(n2);
console.log(n3);
console.log(n4);
console.log(f1);
console.log(f2);
console.log(f3);
console.log(f4);
const box = new Box3();
frustumPoints.forEach(point => box.expandByPoint(point));
// options.left = box.min.x;
// options.right = box.max.x;
// options.top = box.max.y;
// options.bottom = box.min.y;
// options.near = box.min.z;
// options.far = box.max.z;
// console.log(box);
if (shadowMapViewerCreated === false) {
shadowMapViewerCreated = true;
const lightShadowMapViewer = new ShadowMapViewer(light) as any;
lightShadowMapViewer.position.x = 10;
lightShadowMapViewer.position.y = 10;
lightShadowMapViewer.size.width = 4096 / 16;
lightShadowMapViewer.size.height = 4096 / 16;
lightShadowMapViewer.update();
map.addEventListener(MapViewEventNames.AfterRender, () => {
lightShadowMapViewer.render(map.renderer);
});
}
Object.assign(light.shadow.camera, options);
light.shadow.camera.updateProjectionMatrix();
};
const updateLights = () => {
map.scene.children.forEach((obj: THREE.Object3D) => {
if ((obj as any).isDirectionalLight) {
const light = obj as THREE.DirectionalLight;
updateLightCamera(light);
}
});
map.update();
};
promise.then(updateLights);
const gui = new GUI({ width: 300 });
gui.add(options, "top", 0, 10000).onChange(updateLights);
gui.add(options, "left", -10000, 0).onChange(updateLights);
gui.add(options, "right", 0, 10000).onChange(updateLights);
gui.add(options, "bottom", -10000, 0).onChange(updateLights);
gui.add(options, "near", -1000, 100).onChange(updateLights);
gui.add(options, "far", 0, 10000).onChange(updateLights);
// const updateLightPosition = () => {
// map.scene.children.forEach((obj: THREE.Object3D) => {
// if ((obj as any).isDirectionalLight) {
// const light = obj as THREE.DirectionalLight;
// const time = Date.now() / 1000;
// light.position.set(Math.cos(time), Math.sin(time), 1);
// map.update();
// }
// });
// setTimeout(updateLightPosition, 10);
// };
// setTimeout(() => {
// updateLightPosition();
// }, 1000);
return map;
}
function patchFillStyle(styleDeclaration: StyleDeclaration) {
if (!isJsonExpr(styleDeclaration)) {
const style = styleDeclaration as Style;
if (style.technique === "fill") {
(style as any).technique = "standard";
// ((style as any) as StandardStyle).attr!.roughness = 1.0;
}
}
}
/**
* Replace all occurences of "fill" technique in the theme with "standard" technique.
* "standard" technique is using three.js MeshStandardMaterial and is needed to receive
* shadows.
* @param theme The theme to patch
*/
function patchTheme(theme: Theme) {
theme.lights = [
{
type: "ambient",
color: "#ffffff",
name: "ambientLight",
intensity: 0.9
},
{
type: "directional",
color: "#ffcccc",
name: "light1",
intensity: 1,
direction: {
x: 0,
y: 0,
z: 1
},
castShadow: true
}
];
if (theme.styles === undefined || theme.styles.tilezen === undefined) {
throw Error("Theme has no tilezen styles");
}
if (theme.definitions !== undefined) {
for (const definitionName in theme.definitions) {
if (!theme.definitions.hasOwnProperty(definitionName)) {
continue;
}
const definition = theme.definitions[definitionName];
if (!isValueDefinition(definition)) {
const styleDeclaration = definition as StyleDeclaration;
patchFillStyle(styleDeclaration);
}
}
}
theme.styles.tilezen.forEach((styleDeclaration: StyleDeclaration) => {
patchFillStyle(styleDeclaration);
});
}
ThemeLoader.load("resources/berlin_tilezen_base.json").then(
(theme: Theme) => {
patchTheme(theme);
initializeMapView("mapCanvas", theme);
}
);
}