-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
439 lines (358 loc) · 16.1 KB
/
index.html
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!DOCTYPE html>
<html lang="en">
<head>
<title>t</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<!-- <link type="text/css" rel="stylesheet" href="main.css"> -->
<style>
html, body {
margin: 0;
}
#overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
color: #FFFFFF;
padding: 0 1em;
}
</style>
</head>
<body>
<div id="overlay">
<h1>Prototype Viewer MediaWiki 3D</h1>
<p>GLTF and STL formats are supported</p>
<select id="selection" onchange="loadSelection()" placeholder="Please choose a model">
<option value="" disabled selected>Please choose a model</option>
<optgroup label="STL">
<option value="stl_lion.stl">Lion Cologne Zoo - https://codeforcologne.github.io/Denkmal-4D-Koeln/
</option>
<option value="ganesha_stl.stl">Ganesha Cologne Zoo - https://codeforcologne.github.io/Denkmal-4D-Koeln/
</option>
<option value="3D_Model_Hydria.stl">Hydria -https://commons.wikimedia.org/wiki/File:3D_Model_Hydria.stl
</option>
<option value="3D_model_of_an_African_elephant.stl">African_elephant
-https://commons.wikimedia.org/wiki/File:3D_model_of_an_African_elephant.stl
</option>
</optgroup>
<optgroup label="GLB">
<option value="3Ddata.glb">Lion Cologne Zoo - https://codeforcologne.github.io/Denkmal-4D-Koeln/
</option>
<option value="ganesha.glb">Ganesha Cologne Zoo - https://codeforcologne.github.io/Denkmal-4D-Koeln/
</option>
<option value="Avocado.glb">Avocado - KhronosGroup Models Testing</option>
<option value="BarramundiFish.glb">BarramundiFish - KhronosGroup Models Testing</option>
<option value="LightsPunctualLamp.glb">LightsPunctualLamp - KhronosGroup Models Testing</option>
<option value="Duck.glb">Duck - KhronosGroup Models Testing</option>
<option value="NodePerformanceTest.glb">Wait a little bit! NodePerformanceTest - KhronosGroup Models Testing
</option>
<option value="NodePerformanceTest_opt.glb">Wait a little bit! NodePerformanceTest - Optimized with
gltf-transform -
KhronosGroup Models Testing
</option>
<option value="grinkopf.glb">Wait a little bit! Grinkopf Cologne -
https://codeforcologne.github.io/Denkmal-4D-Koeln/</option>
<option value="grinkopf_opt.glb">Grinkopf Cologne - Optimized with gltf-transform
https://codeforcologne.github.io/Denkmal-4D-Koeln/</option>
<option value="coffeemat.glb">coffeemat with KTX2 cpmpression - KhronosGroup Models Testing</option>
</optgroup>
</select>
<button id="uploadButton">Upload a file</button>
<p>Only simple GLB Models and KTX2 commpersion is supported</p>
</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"
integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script src='./lib/three/three_all_in.js'></script>
<script>
document.getElementById('uploadButton').addEventListener('click', function () {
loadFileViaFileAPI((contents, fileName) => {
if (fileName.toLowerCase().endsWith('.stl')) {
modeltype = 'stl';
TD.init(contents, 'browser');
} else if (fileName.toLowerCase().endsWith('.glb')) {
modeltype = 'glb';
TD.init(contents, 'browser');
} else {
console.error('Unsupported file type');
}
});
});
function loadFileViaFileAPI(callback) {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.stl,.glb'; // Adjust file types as needed
input.onchange = function (event) {
const file = event.target.files[0];
if (!file) {
console.log('No file selected');
return;
}
const reader = new FileReader();
reader.onload = function (e) {
const contents = e.target.result;
callback(contents, file.name);
};
reader.onerror = function (e) {
console.error('Error reading file:', e);
};
reader.readAsDataURL(file);
};
input.click();
}
function loadSelection() {
var selection = document.getElementById("selection").value;
if (selection.indexOf('glb') > -1) {
modeltype = 'glb';
TD.init(selection, 'server');
}
if (selection.indexOf('stl') > -1) {
modeltype = 'stl';
TD.init(selection, 'server');
}
}
/*
window.THREE = require( './lib/three/three.js' );
*/
// let modeltype = 'glb';
let modeltype = 'stl';
function ThreeD(viewer) {
THREE.Cache.enabled = true;
this.viewer = viewer;
// this.progressBar = viewer.ui.panel.progressBar;
// this.$container = viewer.ui.canvas.$imageDiv;
}
const TD = ThreeD.prototype;
TD.init = function (model, source) {
this.modeltype = modeltype;
if (document.getElementById('container')) {
this.scene.clear();
const child = document.getElementById('container');
document.body.removeChild(child);
}
container = document.createElement('div');
container.id = 'container';
document.body.appendChild(container);
const dimensions = this.getDimensions();
// renderer
this.renderer = new THREE.WebGLRenderer({ antialias: true });
this.renderer.setClearColor(0x222222);
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(dimensions.width, dimensions.height);
this.renderer.shadowMap.enabled = true;
document.getElementById('container').appendChild(this.renderer.domElement);
this.manager = new THREE.LoadingManager();
// camera
this.camera = new THREE.PerspectiveCamera(60, dimensions.ratio);
// controls
this.controls = new THREE.TrackballControls(this.camera, this.renderer.domElement);
this.controls.rotateSpeed = 2;
this.controls.zoomSpeed = 2;
this.controls.panSpeed = 0.5;
this.controls.addEventListener('change', this.render.bind(this));
this.controls.addEventListener('start', this.controlsStart.bind(this));
this.controls.addEventListener('end', this.controlsEnd.bind(this));
// Scene and RoomEnvironment
const environment = new THREE.RoomEnvironment(this.renderer);
const pmremGenerator = new THREE.PMREMGenerator(this.renderer);
this.scene = new THREE.Scene();
this.scene.environment = pmremGenerator.fromScene(environment).texture;
environment.dispose();
TD.load(this.modeltype, model, source);
};
TD.stageContent = function (object) {
// Ensure all transform matrices are up to date.
object.updateMatrixWorld();
// Compute axis-aligned bounding box (AABB) and center (x, y, z) in
// world space. Size, defined here as a cheap approximation of
// model's maximum span in any direction, is computed from the AABB.
const box = new THREE.Box3().setFromObject(object);
const center = box.getCenter(new THREE.Vector3());
const size = box.getSize(new THREE.Vector3()).length();
// Center content at the origin, (0, 0, 0).
object.position.x -= center.x;
object.position.y -= center.y;
object.position.z -= center.z;
// In glTF, +Y=Up is required. In STL it's unclear, but for backward-
// compatibility we keep +Z=Up. Because some three.js features assume
// +Y=Up, apply a +Z to +Y conversion for STL.
if (this.modeltype === 'stl') {
const parent = new THREE.Object3D();
parent.rotation.set(- Math.PI / 2, 0, 0);
parent.add(object);
object = parent;
}
// Prevent controls from dollying out farther than 10x size.
this.controls.maxDistance = size * 10;
// Constrain camera near and far planes to factors of size. Planes
// should fit the content tightly enough that depth buffer
// precision is utilized well, but not so tight that the model
// clips the near/far planes easily while interacting with controls.
this.camera.near = size / 100;
this.camera.far = size * 100;
this.camera.updateProjectionMatrix();
// Default viewing angle is arbitrary and subjective, some models
// may benefit from a more top-down or front-facing perspective. To
// split the difference somewhat, we use a diagonal. Comparisons:
// sketchfab.com: ( 0, -distance, 0 )
// viewstl.com: ( 0, 0 distance )
// openjscad.org: ( 0, -distance, distance )
// thingiverse.com: ( -distance, -distance, distance )
this.camera.position.x += size * 0.75;
this.camera.position.y += size * 0.5;
this.camera.position.z += size * 0.75;
// Rotate the camera to look at the object's center, (0, 0, 0).
this.camera.lookAt(object.position);
// Add content to scene.
this.scene.add(object);
};
// STL Material wird Geometry hinzugefügt
TD.geometryToObject = function (geometry) {
const vertexColors = geometry.hasAttribute('color');
const material = new THREE.MeshStandardMaterial(
{ color: 0xf0ebe8, flatShading: true, side: THREE.DoubleSide, vertexColors }
);
return new THREE.Mesh(geometry, material);
};
TD.glb = function (data) {
return data
}
TD.render = function () {
this.renderer.render(this.scene, this.camera);
};
TD.animate = function () {
requestAnimationFrame(this.animate.bind(this));
this.controls.update();
};
TD.onWindowResize = function () {
const dimensions = this.getDimensions();
this.camera.aspect = dimensions.width / dimensions.height;
this.camera.updateProjectionMatrix();
this.renderer.setSize(dimensions.width, dimensions.height);
this.controls.handleResize();
this.render(this.renderer, this.scene, this.camera);
};
// MMV stuff
TD.unload = function () {
// 3D files are wrapped inside a new parent class, where the '3D' badge
// is also attached to
// we don't want to keep that wrapper class around (could cause unexpected
// results), and definitely want that '3D' badge gone...
const $threedParent = this.$container.parent('.mw-3d-wrapper');
$threedParent.replaceWith(this.$container);
};
TD.load = function (extension, url, source) {
// Abort any loading that might still be happening
if (this.promise) {
this.promise.reject();
}
this.promise = this.loadFile(extension, url, source);
//this.progressBar.jumpTo(0);
// this.progressBar.animateTo(5);
this.promise.then((object) => {
delete this.promise;
// this.progressBar.hide();
const content = this.modeltype === 'glb' ? object.scene : object;
// STL contains a single mesh, GLB may contain more. Traverse
// to handle both cases.
content.traverse(function (node) {
if (node.isMesh) {
node.castShadow = true;
node.receiveShadow = true;
}
});
this.stageContent(content);
container.appendChild(this.renderer.domElement);
TD.animate();
// mw.threed.base.wrap(this.$container);
}).progress((progress) => {
// this.progressBar.animateTo(progress);
}).fail(( /* error */) => {
// this.progressBar.hide();
delete this.promise;
});
};
TD.loadFile = function (extension, url, source) {
const deferred = $.Deferred();
let loader;
if (extension === 'stl') {
if (source === 'server') {
loader = new THREE.STLLoader(this.manager).setPath('models/');
} else {
loader = new THREE.STLLoader(this.manager);
}
}
if (extension === 'glb') {
const ktx2Loader = new THREE.KTX2Loader()
.setTranscoderPath('lib/three/')
.detectSupport(this.renderer);
if (source === 'server') {
loader = new THREE.GLTFLoader(this.manager).setPath('models/');
} else {
loader = new THREE.GLTFLoader(this.manager);
}
loader.setKTX2Loader(ktx2Loader);
loader.setMeshoptDecoder(THREE.MeshoptDecoder);
}
const request = loader.load(url, (data) => {
let object = data;
if (extension === 'stl') {
object = this.geometryToObject(data);
}
if (extension === 'glb') {
object = this.glb(data);
}
deferred.resolve(object);
}, (progress) => {
deferred.notify((progress.loaded / progress.total) * 100);
}, (error) => {
deferred.reject(error);
});
deferred.fail(() => {
if (request && request.readyState !== 4) {
request.abort();
}
});
return deferred.promise();
};
TD.controlsStart = function () {
$(this.renderer.domElement).addClass('mw-mmv-canvas-mousedown');
};
TD.controlsEnd = function () {
$(this.renderer.domElement).removeClass('mw-mmv-canvas-mousedown');
};
TD.getDimensions = function () {
const width = $(window).width(),
// height = this.viewer.ui.canvas.$imageWrapper.height();
height = $(window).height();
return { width: width, height: height, ratio: width / height };
};
// MMV stuff
// unload when switching images or cleaning up MMV altogether
/*
$(document).on('mmv-hash mmv-cleanup-overlay', () => {
if (singleton) {
singleton.unload();
}
});
*/
/*
if (!singleton) {
singleton = new ThreeD(viewer);
}
*/
/*
singleton.init();
singleton.animate();
singleton.load('stl', e.imageInfo.url);
*/
if (modeltype === 'glb') {
TD.init('ganesha.glb', 'server');
}
if (modeltype === 'stl') {
TD.init('3D_Model_Hydria.stl', 'server');
}
// mw.mmv.ThreeD = ThreeD;
</script>
</body>
</html>