-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
198 lines (141 loc) · 4.8 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
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
import {
TilesRenderer,
} from '3d-tiles-renderer';
import {
Scene,
DirectionalLight,
AmbientLight,
WebGLRenderer,
PerspectiveCamera,
Group,
} from 'three';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { BatchedTileManager } from './BatchedTileManager.js';
let camera, scene, renderer, tiles, controls, batchObject;
let offsetParent;
let infoEl;
let averageTime = 0;
let timeSamples = 0;
const MAX_TILES = 800;
const params = {
useBatchedMesh: true,
errorTarget: 5,
minCacheSize: 600,
coloredTiles: false,
};
init();
function init() {
scene = new Scene();
// primary camera view
renderer = new WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0xbbbbbb );
document.body.appendChild( renderer.domElement );
renderer.setAnimationLoop( animate );
// create workspace
camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 4000 );
camera.position.set( 10, 2, 10 );
scene.add( camera );
// lights
const dirLight = new DirectionalLight( 0xffffff );
dirLight.position.set( 1, 2, 3 );
scene.add( dirLight );
const ambLight = new AmbientLight( 0xffffff, 0.2 );
scene.add( ambLight );
// parent for centering the tileset
offsetParent = new Group();
offsetParent.rotation.x = Math.PI / 2;
offsetParent.position.y = 32;
scene.add( offsetParent );
batchObject = new BatchedTileManager( renderer, MAX_TILES, 1800, 9000 );
tiles = new TilesRenderer( 'https://raw.githubusercontent.com/NASA-AMMOS/3DTilesSampleData/master/msl-dingo-gap/0528_0260184_to_s64o256_colorize/0528_0260184_to_s64o256_colorize/0528_0260184_to_s64o256_colorize_tileset.json' );
tiles.onLoadModel = scene => {
const [ mesh ] = scene.children;
mesh.updateMatrix();
batchObject.addMesh( mesh );
};
tiles.onDisposeModel = scene => {
const [ mesh ] = scene.children;
batchObject.removeMesh( mesh );
};
tiles.onTileVisibilityChange = ( scene, tile, value ) => {
const [ mesh ] = scene.children;
batchObject.setVisible( mesh, value );
};
tiles.group.visible = ! params.useBatchedMesh;
batchObject.mesh.visible = params.useBatchedMesh;
offsetParent.position.y = 0;
offsetParent.add( batchObject.mesh, tiles.group );
window.TILES = tiles;
window.BATCH = batchObject;
// We set camera for tileset
tiles.setCamera( camera );
tiles.setResolutionFromRenderer( camera, renderer );
tiles.lruCache.maxSize = MAX_TILES;
tiles.lruCache.minSize = 100;
controls = new OrbitControls( camera, renderer.domElement );
onWindowResize();
window.addEventListener( 'resize', onWindowResize, false );
infoEl = document.getElementById( 'info' );
// GUI
const gui = new GUI();
gui.width = 300;
gui.add( params, 'useBatchedMesh' ).onChange( v => {
if ( v ) {
tiles.group.visible = false;
batchObject.mesh.visible = true;
} else {
tiles.group.visible = true;
batchObject.mesh.visible = false;
}
timeSamples = 0;
} );
gui.add( params, 'minCacheSize', 0, MAX_TILES, 1 );
gui.add( params, 'errorTarget' , 0, 20, 0.1 );
gui.add( params, 'coloredTiles' ).onChange( v => {
const material = batchObject.mesh.material;
if ( v ) {
material.defines.COLOR_ONLY = '';
} else {
delete material.defines.COLOR_ONLY;
}
material.vertexColors = v;
material.needsUpdate = true;
} );
gui.open();
}
function onWindowResize() {
camera.updateProjectionMatrix();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
function animate() {
tiles.lruCache.minSize = params.minCacheSize;
tiles.errorTarget = params.errorTarget;
camera.updateMatrixWorld();
tiles.group.updateMatrixWorld();
tiles.update();
const start = window.performance.now();
renderer.render( scene, camera );
const delta = window.performance.now() - start;
averageTime += ( delta - averageTime ) / ( timeSamples + 1 );
if ( timeSamples < 60 ) {
timeSamples ++;
}
const totalLoaded =
tiles.lruCache.itemList.length -
tiles.parseQueue.items.length -
tiles.downloadQueue.items.length;
const { calls, triangles } = renderer.info.render;
infoEl.innerText =
`tiles loaded : ${ totalLoaded }\n` +
`tiles displayed : ${ tiles.visibleTiles.size }\n` +
`draw calls : ${ calls }\n` +
`triangles : ${ triangles.toLocaleString() }\n` +
`cpu render time : ${ averageTime.toFixed( 2 ) }ms`;
window.info = renderer.info;
}