forked from nytimes/aframe-loader-3dtiles-component
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (162 loc) · 5.94 KB
/
index.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
import { Loader3DTiles, PointCloudColoring } from 'three-loader-3dtiles';
import { Vector3 } from 'three';
import './textarea';
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
const POINT_CLOUD_COLORING = {
white: PointCloudColoring.White,
intensity: PointCloudColoring.Intensity,
classification: PointCloudColoring.Classification,
elevation: PointCloudColoring.Elevation,
rgb: PointCloudColoring.RGB
};
/**
* 3D Tiles component for A-Frame.
*/
AFRAME.registerComponent('loader-3dtiles', {
schema: {
url: { type: 'string' },
cameraEl: { type: 'selector' },
maximumSSE: { type: 'int', default: 16 },
maximumMem: { type: 'int', default: 32 },
distanceScale: { type: 'number', default: 1.0 },
pointcloudColoring: { type: 'string', default: 'white' },
pointcloudElevationRange: { type: 'array', default: ['0', '400'] },
wireframe: { type: 'boolean', default: false },
showStats: { type: 'boolean', default: false },
cesiumIONToken: { type: 'string' }
},
init: async function () {
this.camera = this.data.cameraEl?.object3D.children[0] ?? document.querySelector('a-scene').camera;
if (!this.camera) {
throw new Error('3D Tiles: Please add an active camera or specify the target camera via the cameraEl property');
}
const { model, runtime } = await this._initTileset();
this.runtime = runtime;
this.runtime.setElevationRange(this.data.pointcloudElevationRange.map(n => Number(n)));
this.el.setObject3D('tileset', model);
this.originalCamera = this.camera;
this.el.sceneEl.addEventListener('camera-set-active', (e) => {
// TODO: For some reason after closing the inspector this event is fired with an empty camera,
// so revert to the original camera used.
//
// TODO: Does not provide the right Inspector perspective camera
this.camera = e.detail.cameraEl.object3D.children[0] ?? this.originalCamera;
});
this.el.sceneEl.addEventListener('enter-vr', (e) => {
this.originalCamera = this.camera;
try {
this.camera = this.el.sceneEl.renderer.xr.getCamera(this.camera);
// FOV Code from https://github.com/mrdoob/three.js/issues/21869
this.el.sceneEl.renderer.xr.getSession().requestAnimationFrame((time, frame) => {
const ref = this.el.sceneEl.renderer.xr.getReferenceSpace();
const pose = frame.getViewerPose(ref);
if (pose) {
const fovi = pose.views[0].projectionMatrix[5];
this.camera.fov = Math.atan2(1, fovi) * 2 * 180 / Math.PI;
}
});
} catch (e) {
console.warn('Could not get VR camera');
}
});
this.el.sceneEl.addEventListener('exit-vr', (e) => {
this.camera = this.originalCamera;
});
if (this.data.showStats) {
this.stats = this._initStats();
}
if (THREE.Cache.enabled) {
console.warn('3D Tiles loader cannot work with THREE.Cache, disabling.');
THREE.Cache.enabled = false;
}
},
update: async function (oldData) {
if (oldData.url !== this.data.url) {
if (this.runtime) {
this.runtime.dispose();
}
const { model, runtime } = await this._initTileset();
this.el.setObject3D('tileset', model);
this.runtime = runtime;
} else if (this.runtime) {
this.runtime.setPointCloudColoring(this._resolvePointcloudColoring(this.data.pointCloudColoring));
this.runtime.setWireframe(this.data.wireframe);
this.runtime.setViewDistanceScale(this.data.distanceScale);
this.runtime.setElevationRange(this.data.pointcloudElevationRange.map(n => Number(n)));
}
if (this.data.showStats && !this.stats) {
this.stats = this._initStats();
}
if (!this.data.showStats && this.stats) {
this.el.sceneEl.removeChild(this.stats);
this.stats = null;
}
},
tick: function (t, dt) {
if (this.runtime) {
this.runtime.update(dt, this.el.sceneEl.renderer, this.camera);
if (this.stats) {
const worldPos = new Vector3();
this.camera.getWorldPosition(worldPos);
const stats = this.runtime.getStats();
this.stats.setAttribute(
'textarea',
'text',
Object.values(stats.stats).map(s => `${s.name}: ${s.count}`).join('\n')
);
const newPos = new Vector3();
newPos.copy(worldPos);
newPos.z -= 2;
this.stats.setAttribute('position', newPos);
}
}
},
remove: function () {
if (this.runtime) {
this.runtime.dispose();
}
},
_resolvePointcloudColoring () {
const pointCloudColoring = POINT_CLOUD_COLORING[this.data.pointcloudColoring];
if (!pointCloudColoring) {
console.warn('Invalid value for point cloud coloring');
return PointCloudColoring.White;
} else {
return pointCloudColoring;
}
},
_initTileset: async function () {
const pointCloudColoring = this._resolvePointcloudColoring(this.data.pointcloudColoring);
return Loader3DTiles.load({
url: this.data.url,
renderer: this.el.sceneEl.renderer,
options: {
dracoDecoderPath: 'https://unpkg.com/[email protected]/examples/js/libs/draco',
basisTranscoderPath: 'https://unpkg.com/[email protected]/examples/js/libs/basis',
cesiumIONToken: this.data.cesiumIONToken,
maximumScreenSpaceError: this.data.maximumSSE,
maximumMemoryUsage: this.data.maximumMem,
viewDistanceScale: this.data.distanceScale,
wireframe: this.data.wireframe,
pointCloudColoring: pointCloudColoring,
updateTransforms: true
}
});
},
_initStats: function () {
const stats = document.createElement('a-entity');
this.el.sceneEl.appendChild(stats);
stats.setAttribute('position', '-0.5 0 -1');
stats.setAttribute('textarea', {
cols: 30,
rows: 15,
text: '',
color: 'white',
disabledBackgroundColor: '#0c1e2c',
disabled: true
});
return stats;
}
});