Skip to content

Commit 7d1f049

Browse files
committed
✨ feat: add vrm loading dom
1 parent 6429601 commit 7d1f049

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed

src/features/AgentViewer/index.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,25 @@ function AgentViewer(props: Props) {
3232
(canvas: HTMLCanvasElement) => {
3333
if (canvas) {
3434
viewer.setup(canvas);
35-
fetchModelUrl(agent.agentId, agent.meta.model!).then((modelUrl) => {
35+
fetchModelUrl(agent.agentId, agent.meta.model!).then(async (modelUrl) => {
3636
if (modelUrl) {
37-
viewer.loadVrm(modelUrl);
37+
// add loading dom
38+
const agentViewer = document.querySelector('#agent-viewer')!;
39+
const loadingScreen = document.createElement('div');
40+
loadingScreen.setAttribute('id', 'loading-screen');
41+
const loader = document.createElement('div');
42+
loader.setAttribute('id', 'loader');
43+
loadingScreen.append(loader);
44+
agentViewer.append(loadingScreen);
45+
46+
// load
47+
await viewer.loadVrm(modelUrl);
48+
49+
// remove loading dom
50+
loadingScreen.classList.add('fade-out');
51+
loadingScreen.addEventListener('transitionend', () => {
52+
loadingScreen.remove();
53+
});
3854
}
3955
});
4056

@@ -90,6 +106,7 @@ function AgentViewer(props: Props) {
90106
<div
91107
ref={ref}
92108
className={classNames(styles.viewer, className)}
109+
id="agent-viewer"
93110
style={{ height, width, ...style }}
94111
>
95112
<ToolBar className={styles.toolbar} viewer={viewer} />

src/features/AgentViewer/style.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,99 @@ export const useStyles = createStyles(({ css, token }) => ({
66
width: 100%;
77
height: 100%;
88
min-height: 0;
9+
10+
#loading-screen {
11+
position: absolute;
12+
top: 0;
13+
left: 0;
14+
15+
width: 100%;
16+
height: 100%;
17+
18+
opacity: 1;
19+
background-color: #000;
20+
21+
transition: 1s opacity;
22+
}
23+
24+
#loading-screen.fade-out {
25+
opacity: 0;
26+
}
27+
28+
#loader {
29+
position: relative;
30+
top: 50%;
31+
left: 50%;
32+
33+
display: block;
34+
35+
width: 150px;
36+
height: 150px;
37+
margin: -75px 0 0 -75px;
38+
39+
border: 3px solid transparent;
40+
border-top-color: #9370db;
41+
border-radius: 50%;
42+
43+
animation: spin 2s linear infinite;
44+
animation: spin 2s linear infinite;
45+
}
46+
47+
#loader::before {
48+
content: '';
49+
50+
position: absolute;
51+
inset: 5px;
52+
53+
border: 3px solid transparent;
54+
border-top-color: #ba55d3;
55+
border-radius: 50%;
56+
57+
animation: spin 3s linear infinite;
58+
animation: spin 3s linear infinite;
59+
}
60+
61+
#loader::after {
62+
content: '';
63+
64+
position: absolute;
65+
inset: 15px;
66+
67+
border: 3px solid transparent;
68+
border-top-color: #f0f;
69+
border-radius: 50%;
70+
71+
animation: spin 1.5s linear infinite;
72+
animation: spin 1.5s linear infinite;
73+
}
74+
75+
@keyframes spin {
76+
0% {
77+
transform: rotate(0deg);
78+
transform: rotate(0deg);
79+
transform: rotate(0deg);
80+
}
81+
82+
100% {
83+
transform: rotate(360deg);
84+
transform: rotate(360deg);
85+
transform: rotate(360deg);
86+
}
87+
}
88+
89+
@keyframes spin {
90+
0% {
91+
transform: rotate(0deg);
92+
transform: rotate(0deg);
93+
transform: rotate(0deg);
94+
}
95+
96+
100% {
97+
transform: rotate(360deg);
98+
transform: rotate(360deg);
99+
transform: rotate(360deg);
100+
}
101+
}
9102
`,
10103
toolbar: css`
11104
position: absolute;

src/libs/vrmViewer/model.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { VRM, VRMLoaderPlugin, VRMUtils } from '@pixiv/three-vrm';
22
import * as THREE from 'three';
3-
import { AnimationAction, AnimationClip } from 'three';
3+
import { AnimationAction, AnimationClip, LoadingManager } from 'three';
44
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
55
import { LoopOnce } from 'three/src/constants';
66

@@ -50,7 +50,6 @@ export class Model {
5050
autoUpdateHumanBones: true,
5151
}),
5252
);
53-
5453
const gltf = await loader.loadAsync(url);
5554

5655
// 提升性能

0 commit comments

Comments
 (0)