forked from sidx1024/deck.gl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
348 lines (306 loc) · 9.2 KB
/
app.js
File metadata and controls
348 lines (306 loc) · 9.2 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* global document, window, navigator */
/* eslint-disable no-console */
import React, {PureComponent} from 'react';
import {render} from 'react-dom';
import DeckGL, {COORDINATE_SYSTEM, PointCloudLayer, experimental, Viewport} from 'deck.gl';
const {OrbitController} = experimental;
import {setParameters} from 'luma.gl';
import {loadLazFile, parseLazData} from './utils/laslaz-loader';
import WebVRPolyfill from 'webvr-polyfill';
import EmulatedVRDisplay from './vr/emulated-vr-display';
const DATA_REPO = 'https://raw.githubusercontent.com/uber-common/deck.gl-data/master';
const FILE_PATH = 'examples/point-cloud-laz/indoor.laz';
function normalize(points) {
let xMin = Infinity;
let yMin = Infinity;
let zMin = Infinity;
let xMax = -Infinity;
let yMax = -Infinity;
let zMax = -Infinity;
for (let i = 0; i < points.length; i++) {
xMin = Math.min(xMin, points[i].position[0]);
yMin = Math.min(yMin, points[i].position[1]);
zMin = Math.min(zMin, points[i].position[2]);
xMax = Math.max(xMax, points[i].position[0]);
yMax = Math.max(yMax, points[i].position[1]);
zMax = Math.max(zMax, points[i].position[2]);
}
const scale = Math.max(...[xMax - xMin, yMax - yMin, zMax - zMin]);
const xMid = (xMin + xMax) / 2;
const yMid = (yMin + yMax) / 2;
const zMid = (zMin + zMax) / 2;
for (let i = 0; i < points.length; i++) {
points[i].position[0] = (points[i].position[0] - xMid) / scale;
points[i].position[1] = (points[i].position[1] - yMid) / scale;
points[i].position[2] = (points[i].position[2] - zMid) / scale;
}
}
class Example extends PureComponent {
constructor(props) {
super(props);
this._onViewportChange = this._onViewportChange.bind(this);
this._onInitialized = this._onInitialized.bind(this);
this._onResize = this._onResize.bind(this);
this._onUpdate = this._onUpdate.bind(this);
this.state = {
width: 0,
height: 0,
points: [],
progress: 0,
rotating: true,
viewport: {
lookAt: [0, 0, 0],
distance: 1,
rotationX: 0,
rotationOrbit: 0,
orbitAxis: 'Y',
fov: 30,
minDistance: 0.5,
maxDistance: 3
},
vrDisplay: new EmulatedVRDisplay(),
vrEnabled: false,
emulatedPose: {
orientation: [0, 0, 0, 1],
position: [0, 0, 0]
}
};
}
componentWillMount() {
window.addEventListener('resize', this._onResize);
this._onResize();
}
componentDidMount() {
const {points} = this.state;
const skip = 10;
loadLazFile(`${DATA_REPO}/${FILE_PATH}`).then(rawData => {
parseLazData(rawData, skip, (decoder, progress) => {
for (let i = 0; i < decoder.pointsCount; i++) {
const {color, position} = decoder.getPoint(i);
points.push({color, position});
}
if (progress >= 1) {
normalize(points);
}
this.setState({points, progress});
});
});
this._initVRDisplay();
window.requestAnimationFrame(this._onUpdate);
}
componentWillUnmount() {
window.removeEventListener('resize', this._onResize);
}
_onResize() {
const size = {width: window.innerWidth, height: window.innerHeight};
this.setState(size);
const newViewport = OrbitController.getViewport(
Object.assign(this.state.viewport, size)
).fitBounds([1, 1, 1]);
this._onViewportChange(newViewport);
}
_onInitialized(gl) {
setParameters(gl, {
clearColor: [0.07, 0.14, 0.19, 1],
blendFunc: [gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA]
});
}
_onViewportChange(viewport) {
this.setState({
rotating: !viewport.isDragging,
viewport: {...this.state.viewport, ...viewport}
});
}
_onUpdate() {
const {vrEnabled} = this.state;
if (vrEnabled) {
const {vrDisplay, emulatedPose} = this.state;
// animate camera in Z-axis
// this can be removed after we have a VRController for EmulatedVRDisplay
if (vrDisplay.isEmulated) {
const {position} = emulatedPose;
position[2] += position[2] < 0.5 ? 0.001 : -0.5;
this.setState({
emulatedPose: {
...emulatedPose,
...position
}
});
}
this.forceUpdate(); // explicitly refresh state; removing this breaks frame update in VR
window.requestAnimationFrame(this._onUpdate);
return;
}
const {rotating, viewport} = this.state;
// note: when finished dragging, _onUpdate will not resume by default
// to resume rotating, explicitly call _onUpdate or requestAnimationFrame
if (!rotating) {
return;
}
this.setState({
viewport: {
...viewport,
rotationOrbit: viewport.rotationOrbit + 1
}
});
window.requestAnimationFrame(this._onUpdate);
}
_renderLazPointCloudLayer(useSmallRadius = false) {
const {points} = this.state;
if (!points || points.length === 0) {
return null;
}
return new PointCloudLayer({
id: 'laz-point-cloud-layer',
data: points,
coordinateSystem: COORDINATE_SYSTEM.IDENTITY,
getPosition: d => d.position,
getNormal: d => [0, 0.5, 0.2],
getColor: d => [255, 255, 255, 128],
radiusPixels: useSmallRadius ? 0.5 : 1
});
}
_initVRDisplay() {
const polyfill = new WebVRPolyfill({ // eslint-disable-line no-unused-vars
PROVIDE_MOBILE_VRDISPLAY: true
});
if (navigator && navigator.getVRDisplays) {
navigator.getVRDisplays().then(displays => {
const vrDisplay = displays.find(d => d.isConnected);
if (vrDisplay) {
this.setState({vrDisplay, vrEnabled: true});
}
});
}
}
_renderViewports() {
const {width, height, vrDisplay, emulatedPose} = this.state;
const frameData = vrDisplay.isEmulated ? {} : new window.VRFrameData();
const gotFrameData = vrDisplay.isEmulated
? vrDisplay.getFrameDataFromPose(frameData, emulatedPose)
: vrDisplay.getFrameData(frameData);
if (gotFrameData) {
return [
new Viewport({
x: 0,
width: width / 2,
height,
viewMatrix: frameData.leftViewMatrix,
projectionMatrix: frameData.leftProjectionMatrix
}),
new Viewport({
x: width / 2,
width: width / 2,
height,
viewMatrix: frameData.rightViewMatrix,
projectionMatrix: frameData.rightProjectionMatrix
})
];
}
return new Viewport({width, height});
}
_toggleDisplayMode() {
const {vrEnabled} = this.state;
this.setState({
vrEnabled: !vrEnabled
});
}
_renderDeckGLCanvas() {
const {width, height, viewport, vrEnabled} = this.state;
if (vrEnabled) {
return (
<DeckGL
width={width}
height={height}
viewports={this._renderViewports()}
layers={[this._renderLazPointCloudLayer(true)]}
onWebGLInitialized={this._onInitialized}
/>
);
}
const canvasProps = {width, height, ...viewport};
const glViewport = OrbitController.getViewport(canvasProps);
return (
<OrbitController
{...canvasProps}
ref={canvas => {
this._canvas = canvas;
}}
onViewportChange={this._onViewportChange}
>
<DeckGL
width={width}
height={height}
viewport={glViewport}
layers={[this._renderLazPointCloudLayer()]}
onWebGLInitialized={this._onInitialized}
/>
</OrbitController>
);
}
_renderProgressInfo() {
const progress = (this.state.progress * 100).toFixed(2);
return (
<div>
<div
style={{
position: 'absolute',
left: '8px',
bottom: '8px',
color: '#FFF',
fontSize: '15px'
}}
>
{this.state.progress < 1 ? (
<div>
<div>This example might not work on mobile devices due to browser limitations.</div>
<div>Please try checking it with a desktop machine instead.</div>
<div>{`Loading ${progress}% (laslaz loader by plas.io)`}</div>
</div>
) : (
<div>Data source: kaarta.com</div>
)}
</div>
<div
style={{
position: 'absolute',
right: '8px',
bottom: '8px',
color: '#FFF',
fontSize: '15px'
}}
>
{this.state.vrEnabled ? (
<div>
{this.state.vrDisplay.isEmulated ? (
<a onClick={this._toggleDisplayMode.bind(this)}>Exit stereoscopic view.</a>
) : (
<br />
)}
</div>
) : (
<div>
<div>No VR Device found.</div>
<a onClick={this._toggleDisplayMode.bind(this)}>Enter stereoscopic view.</a>
</div>
)}
</div>
</div>
);
}
render() {
const {width, height} = this.state;
if (width <= 0 || height <= 0) {
return null;
}
return (
<div>
{this._renderDeckGLCanvas()}
{this._renderProgressInfo()}
</div>
);
}
}
const root = document.createElement('div');
document.body.appendChild(root);
render(<Example />, root);