-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
220 lines (187 loc) · 5.55 KB
/
app.js
File metadata and controls
220 lines (187 loc) · 5.55 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
/* global document, window,*/
/* eslint-disable no-console */
import React, {PureComponent} from 'react';
import {render} from 'react-dom';
import DeckGL, {PointCloudLayer, COORDINATE_SYSTEM} from 'deck.gl';
import {setParameters} from 'luma.gl';
import {
OrbitController,
loadLazFile, parseLazData
} from './utils';
const FILE_PATH = '/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,
rotationY: 0,
fov: 30,
minDistance: 0.5,
maxDistance: 3
}
};
}
componentWillMount() {
window.addEventListener('resize', this._onResize);
this._onResize();
}
componentDidMount() {
this._canvas.fitBounds([-0.5, -0.5, -0.5], [0.5, 0.5, 0.5]);
const {points} = this.state;
const skip = 10;
loadLazFile(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});
});
});
window.requestAnimationFrame(this._onUpdate);
}
componentWillUnmount() {
window.removeEventListener('resize', this._onResize);
}
_onResize() {
const {innerWidth: width, innerHeight: height} = window;
this.setState({width, height});
}
_onInitialized(gl) {
setParameters(gl, {
clearColor: [0.07, 0.14, 0.19, 1],
depthTest: true,
depthFunc: gl.LEQUAL
});
}
_onViewportChange(viewport) {
this.setState({
rotating: !viewport.isDragging,
viewport: {...this.state.viewport, ...viewport}
});
}
_onUpdate() {
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,
rotationY: viewport.rotationY + 1
}
});
window.requestAnimationFrame(this._onUpdate);
}
_renderLazPointCloudLayer() {
const {points} = this.state;
if (!points || points.length === 0) {
return null;
}
return new PointCloudLayer({
id: 'laz-point-cloud-layer',
data: points,
projectionMode: COORDINATE_SYSTEM.IDENTITY,
getPosition: d => d.position,
getNormal: d => [0, 0.5, 0.2],
getColor: d => [255, 255, 255, 128],
radiusPixels: 1
});
}
_renderDeckGLCanvas() {
const {width, height, viewport} = this.state;
const canvasProps = {width, height, ...viewport};
const glViewport = OrbitController.getViewport(canvasProps);
return width && height && (
<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>
);
}
render() {
const {width, height} = this.state;
return width && height && <div>
{this._renderDeckGLCanvas()}
{this._renderProgressInfo()}
</div>;
}
}
const root = document.createElement('div');
document.body.appendChild(root);
render(<Example />, root);