-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
222 lines (181 loc) · 8.28 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Three.js - Meta Avatars</title>
<style>
body { margin: 0; min-width: 100vw; min-height: 100vh; text-align: center;}
#container {
position: absolute;
width: 100%;
margin-top: 5em;
text-align: center;
}
#notice {
margin: 0 auto;
padding: 1em;
width: 500px;
border: 2px dashed lightgray;
border-radius: 10px;
color: lightgray;
display: inline-block;
}
</style>
<script type="importmap">
{
"imports": {
"three": "./libs/three/build/three.module.js",
"three/addons/": "./libs/three/examples/jsm/",
"three-meta-avatar-gltf-plugins": "./js/three-meta-avatar-gltf-plugins.esm.min.js"
}
}
</script>
</head>
<body>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { ZSTDDecoder } from 'three/addons/libs/zstddec.module.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import { FBAvatarTweaks, FBMeshQuantization, FBTextureKTX2 } from 'three-meta-avatar-gltf-plugins';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.setPixelRatio(1.0);
document.body.appendChild( renderer.domElement );
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
window.addEventListener('resize', onWindowResize);
onWindowResize();
// Drag and drop handler
window.addEventListener('dragover', function(event) {
event.preventDefault();
});
let zstdDecoder = null;
window.addEventListener('drop', async function(event) {
event.preventDefault();
const files = event.dataTransfer.files;
if(!files || !files[0]) {
return;
}
// Hide message
document.getElementById('notice').style.display = 'none';
const blob = new Blob([files[0]], { type: "application/octet-stream" });
const arrayBuffer = await blob.arrayBuffer();
// Check if the array buffer is compressed
const magicNumber = new Uint8Array(arrayBuffer.slice(0, 4));
if(magicNumber[0] == 0x28 && magicNumber[1] == 0xb5 && magicNumber[2] == 0x2f && magicNumber[3] == 0xfd) {
if(!zstdDecoder) {
zstdDecoder = new ZSTDDecoder()
await zstdDecoder.init();
}
const decoded = zstdDecoder.decode(new Uint8Array(arrayBuffer));
const url = URL.createObjectURL(new Blob([decoded]));
load(url);
} else {
// Normal glb file
const url = URL.createObjectURL(blob);
load(url);
}
});
// Grid helper
const gridHelper = new THREE.GridHelper( 10, 10 );
scene.add( gridHelper );
// Orbit controls
camera.position.z = 2;
camera.position.y = 1.5;
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1.5, 0);
controls.update();
// Light
const color = 0xFFFFFF;
const light = new THREE.AmbientLight(color, 1);
scene.add(light);
const dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
dirLight.position.set( 1, 4, 1 );
scene.add(dirLight);
// GUI
let gui = null;
// Container for the avatar
const avatarNode = new THREE.Object3D();
let avatarMesh = null;
scene.add(avatarNode);
// Load avatar
const gltfLoader = new GLTFLoader();
gltfLoader.register((_) => new FBAvatarTweaks());
gltfLoader.register((_) => new FBMeshQuantization());
gltfLoader.register((parser) => new FBTextureKTX2(parser));
function load(url) {
gltfLoader.load(url,
function(gltf) {
// Remove any previously loaded avatar
avatarNode.clear();
// Determine if it's an older (pre Connect 2024) or newer avatar
const newRootNode = gltf.scene.getObjectByName("root_joint");
const oldRootNode = gltf.scene.getObjectByName("Root_jnt");
const isNewAvatar = !!newRootNode;
// Needed since all meshes are skinned
avatarNode.add( newRootNode ?? oldRootNode );
const avatarMeshes = Object.fromEntries(
( isNewAvatar ? gltf.scene.children : gltf.scene.children[0].children )
.filter( x => x.isMesh )
.map( x => [ x.name, x ] )
);
// Pick out a single child to use
avatarMesh = null;
changeMesh( Object.keys(avatarMeshes)[0] );
// Add a GUI
function createGui() {
gui?.destroy();
gui = new GUI();
const settings = { mesh: avatarMesh.name }
// Meshes
gui.add( settings, 'mesh', avatarMeshes )
.onChange( mesh => changeMesh( mesh.name ) );
// Morph targets
if ( avatarMesh.morphTargetDictionary ) {
const morphTargetFolder = gui.addFolder('Morph Targets').close();
const influences = avatarMesh.morphTargetInfluences;
for ( const [ key, value ] of Object.entries( avatarMesh.morphTargetDictionary ) ) {
morphTargetFolder.add( influences, value, 0, 1, 0.01 )
.name( key )
.listen( influences );
}
}
}
function changeMesh( meshName ) {
if ( meshName === avatarMesh?.name ) {
return;
}
if ( avatarMesh ) {
avatarNode.remove( avatarMesh );
}
avatarMesh = avatarMeshes[ meshName ];
avatarNode.add( avatarMesh );
createGui();
}
},
function(xhr) {},
function(error) {
console.error(error);
});
}
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}
animate();
</script>
<div id="container">
<div id="notice">
<h1>Drag and drop an avatar .glb file (compressed or uncompressed)</h1>
</div>
</div>
</body>
</html>