This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.html
192 lines (154 loc) · 5.25 KB
/
demo.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
<!DOCTYPE html>
<html>
<header>
<title>Unified 3D Loader Demo</title>
<style>
#container {
position: absolute;
top: 0;
left: 0;
margin: 0;
width: 100%;
height: 100%;
}
#file, #timer {
display: inline-block;
}
.controls {
background-color: #dfdfdf;
border-radius: 4px;
bottom: 15px;
left: 50%;
padding: 5px;
position: absolute;
transform: translateX(-50%);
}
html {
font-family: Arial, Helvetica, sans-serif;
}
canvas {
display: block;
}
</style>
<script type="module">
//Imports
import {FileFormats, Unified3dLoader} from './dist/es/index.js';
import 'https://unpkg.com/three@latest/build/three.min.js';
import {BufferGeometryUtils} from 'https://unpkg.com/three@latest/examples/jsm/utils/BufferGeometryUtils.js';
import {GUI} from 'https://unpkg.com/[email protected]/examples/jsm/libs/dat.gui.module.js';
import {OrbitControls} from 'https://unpkg.com/three@latest/examples/jsm/controls/OrbitControls.js';
import Stats from 'https://unpkg.com/three@latest/examples/jsm/libs/stats.module.js';
//Get the DOM elements
const container = document.getElementById('container');
const file = document.getElementById('file');
const timer = document.getElementById('timer');
//Camera setup
const camera = new THREE.PerspectiveCamera(25, container.clientWidth / container.clientHeight, 0.01, 1000);
camera.position.set(10, 10, 10);
camera.rotation.set(-Math.PI / 4, Math.PI / 5, Math.PI / 6);
//Scene setup
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x202020);
//Light setup
const light = new THREE.HemisphereLight(0xffffff, 0x505050);
light.position.set(0, 10, 0);
scene.add(light);
//Object setup
const material = new THREE.MeshPhongMaterial({
color: 0x42f5f2,
specular: 0x111111,
shininess: 200,
});
const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.Float32BufferAttribute([0, 0, 0], 3));
geometry.setAttribute('normal', new THREE.Float32BufferAttribute([0, 0, 0], 3));
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
//Renderer setup
const renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
//Helpers
const axesHelper = new THREE.AxesHelper(1);
scene.add(axesHelper);
//GUI Setup
const stats = new Stats();
const gui = new GUI();
gui.add(material, 'wireframe');
container.appendChild(stats.dom);
//Controls setup
const controls = new OrbitControls(camera, renderer.domElement);
controls.update();
//Window resize handler
window.addEventListener('resize', () =>
{
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
});
//Animation loop
const animate = () =>
{
requestAnimationFrame(animate);
//Render the scene
renderer.render(scene, camera);
stats.update();
};
animate();
//Add the change event handler
file.addEventListener('change', async () =>
{
const start = Date.now();
//Identify the file format
let fileFormat;
const extension = (/\.(.+)$/.exec(file.files[0].name)[1]).toLowerCase();
Object.values(FileFormats).forEach(format =>
{
if (extension != null && format.extensions.includes(extension))
{
fileFormat = format;
}
});
//Load and parse the file
const loader = new Unified3dLoader();
const parsed = await loader.load(await file.files[0].arrayBuffer(), fileFormat, {
index: {
normals: false,
vertices: false
}
});
console.log('Parsed', parsed);
//Triplicate each normal
const normals = [];
for (let i = 0; i < parsed[0].normals.length; i += 3)
{
const vector = [
parsed[0].normals[i],
parsed[0].normals[i + 1],
parsed[0].normals[i + 2]
];
normals.push(...vector, ...vector, ...vector);
}
const end = Date.now();
//Calculate elapsed time
const elapsed = new Date(end - start);
timer.innerText = `Parsed file in ${elapsed.valueOf()}ms`;
//Update the mesh
geometry.setAttribute('position', new THREE.Float32BufferAttribute(parsed[0].vertices, 3));
geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));
});
</script>
</header>
<body>
<div id="container"></div>
<div class="controls">
<input id="file" type="file">
<div id="timer"></div>
</div>
</body>
</html>