forked from enjikaka/obj-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
222 lines (168 loc) · 5.43 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import 'https://unpkg.com/[email protected]/build/three.js?module';
import 'https://unpkg.com/[email protected]/examples/js/loaders/LoaderSupport.js?module';
import 'https://unpkg.com/[email protected]/examples/js/loaders/MTLLoader.js?module';
import 'https://unpkg.com/[email protected]/examples/js/loaders/OBJLoader2.js?module';
import 'https://unpkg.com/[email protected]/examples/js/controls/OrbitControls.js?module';
// @ts-ignore
const THREE = window.THREE;
function loadMtl (objLoader, src) {
if (src) {
return new Promise(resolve => {
objLoader.loadMtl(src, null, materials => resolve(materials));
});
}
return Promise.resolve(undefined);
}
function loadObj (objLoader, src) {
return new Promise(resolve => {
objLoader.load(src, event => resolve(event.detail.loaderRootNode), null, null, null, false);
});
}
function computeGroupCenter (group) {
const bbox = new THREE.Box3().setFromObject(group);
return bbox.getCenter(group.position);
}
class ObjViewer extends HTMLElement {
constructor () {
super();
this._aspectRatio = 1;
}
get aspectRatio () {
return this._aspectRatio;
}
get canvas () {
return this.sDOM.querySelector('canvas');
}
get cameraDefaults () {
return {
posCamera: new THREE.Vector3(0.0, 100.0, 200.0),
posCameraTarget: this.cameraTarget
};
}
get cameraTarget () {
return new THREE.Vector3(0.0, 0.0, 0.0);
}
resetCamera () {
this.camera.position.copy(this.cameraDefaults.posCamera);
this.cameraTarget.copy(this.cameraDefaults.posCameraTarget);
this.updateCamera();
}
updateCamera () {
this.camera.aspect = this.aspectRatio;
this.camera.lookAt(this.cameraTarget);
this.camera.updateProjectionMatrix();
}
initGL ({
displayGrid = false
}) {
this.renderer = new THREE.WebGLRenderer({
canvas: this.canvas,
antialias: true,
autoClear: true,
powerPreference: 'high-performance'
});
this.renderer.setClearColor(0x050505);
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera(this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far);
this.resetCamera();
this.controls = new THREE.OrbitControls(this.camera, this.wrapperElement);
const ambientLight = new THREE.AmbientLight(0x404040);
const directionalLight1 = new THREE.DirectionalLight(0xC0C090);
const directionalLight2 = new THREE.DirectionalLight(0xC0C090);
directionalLight1.position.set(-100, -50, 100);
directionalLight2.position.set(100, 50, -100);
this.scene.add(directionalLight1);
this.scene.add(directionalLight2);
this.scene.add(ambientLight);
if (displayGrid) {
const gridHelper = new THREE.GridHelper(1200, 60, 0xFF4444, 0x404040);
this.scene.add(gridHelper);
}
}
recalcAspectRatio () {
this._aspectRatio = (this.canvas.offsetHeight === 0) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
}
resizeDisplayGL () {
this.recalcAspectRatio();
this.renderer.setSize(this.canvas.offsetWidth * window.devicePixelRatio, this.canvas.offsetHeight * window.devicePixelRatio, false);
this.updateCamera();
}
async loadModel (objSource, mtlSource) {
const objLoader = new THREE.OBJLoader2();
const modelName = objSource.split('/').pop().split('.')[0];
const materials = await loadMtl(objLoader, mtlSource);
objLoader.setModelName(modelName);
if (materials) {
objLoader.setMaterials(materials);
}
objLoader.setLogging(false, false);
const rootNode = await loadObj(objLoader, objSource);
const center = computeGroupCenter(rootNode);
// Centers the model in the view
center.multiplyScalar(-1);
this.camera.lookAt(center);
this.scene.add(rootNode);
}
render () {
if (!this.renderer.autoClear) {
this.renderer.clear();
}
this.controls.update();
this.renderer.render(this.scene, this.camera);
}
startRenderLoop () {
this.render();
requestAnimationFrame(() => this.startRenderLoop());
}
async connectedCallback () {
this.sDOM = this.attachShadow({ mode: 'closed' });
this.sDOM.innerHTML = `
<style>
:host {
width: 100%;
height: 100%;
display: block;
contain: strict;
overflow: hidden;
}
.wrapper,
canvas {
width: 100%;
height: 100%;
}
.loading-message {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 1em;
color: white;
font-family: sans-serif;
opacity: 1;
transition: opacity 250ms ease;
}
.hidden {
opacity: 0;
pointer-events: none;
}
</style>
<div class="wrapper">
<p class="loading-message">Loading model...</p>
<canvas></canvas>
</div>
`;
this.wrapperElement = this.sDOM.querySelector('.wrapper');
const displayGrid = this.hasAttribute('display-grid');
this.initGL({ displayGrid });
this.resizeDisplayGL();
const objSource = this.getAttribute('obj-source');
const mtlSource = this.getAttribute('mtl-source');
await this.loadModel(objSource, mtlSource);
this.sDOM.querySelector('.loading-message').classList.add('hidden');
this.startRenderLoop();
// @ts-ignore
const ro = new ResizeObserver(() => this.resizeDisplayGL());
ro.observe(this);
}
}
customElements.define('obj-viewer', ObjViewer);