forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avatar-iconer.js
198 lines (167 loc) · 5.22 KB
/
avatar-iconer.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
import * as THREE from 'three';
import metaversefile from 'metaversefile'
import {emotions} from './src/components/general/character/Emotions';
import offscreenEngineManager from './offscreen-engine-manager.js';
const allEmotions = [''].concat(emotions);
class AvatarIconer extends EventTarget {
constructor(player, {
width = 150,
height = 150,
} = {}) {
super();
this.player = player;
this.width = width;
this.height = height;
this.emotionCanvases = [];
this.emotion = '';
this.lastRenderedEmotion = null;
this.enabled = false;
this.canvases = [];
const avatarchange = e => {
this.renderAvatarApp(e.app);
};
player.addEventListener('avatarchange', avatarchange);
const actionupdate = e => {
this.updateEmotionFromActions();
};
player.addEventListener('actionadd', actionupdate);
player.addEventListener('actionremove', actionupdate);
this.cleanup = () => {
player.removeEventListener('avatarchange', avatarchange);
player.removeEventListener('actionadd', actionupdate);
player.removeEventListener('actionremove', actionupdate);
};
this.getEmotionCanvases = offscreenEngineManager.createFunction([
`\
import * as THREE from 'three';
import metaversefile from './metaversefile-api.js';
import npcManager from './npc-manager.js';
import {screenshotPlayer} from './avatar-screenshotter.js';
import {emotions} from './src/components/general/character/Emotions.jsx';
const allEmotions = [''].concat(emotions);
const cameraOffset = new THREE.Vector3(0, 0.05, -0.35);
`,
async function(start_url, width, height) {
const player = await npcManager.createNpcAsync({
name: 'avatar-iconer-npc',
avatarUrl: start_url,
detached: true,
});
const emotionCanvases = await Promise.all(allEmotions.map(async emotion => {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
await screenshotPlayer({
player,
canvas,
cameraOffset,
emotion,
});
const imageBitmap = await createImageBitmap(canvas);
return imageBitmap;
}));
player.destroy();
return emotionCanvases;
}
]);
const avatarApp = player.getAvatarApp();
this.renderAvatarApp(avatarApp);
}
async renderAvatarApp(srcAvatarApp) {
const lastEnabled = this.enabled;
if (srcAvatarApp) {
const start_url = srcAvatarApp.contentId;
this.emotionCanvases = await this.getEmotionCanvases([start_url, this.width, this.height]);
this.enabled = true;
} else {
this.emotionCanvases.length = 0;
this.enabled = false;
}
this.lastRenderedEmotion = null;
if (lastEnabled !== this.enabled) {
this.dispatchEvent(new MessageEvent('enabledchange', {
data: {
enabled: this.enabled,
},
}));
}
}
addCanvas(canvas) {
canvas.ctx = canvas.getContext('2d');
this.canvases.push(canvas);
}
updateEmotionFromActions() {
const emotion = (() => {
const faceposeAction = this.player.getAction('facepose');
if (faceposeAction) {
return faceposeAction.emotion;
}
const hurtAction = this.player.getAction('hurt');
if (hurtAction) {
return 'sorrow';
}
const useAction = this.player.getAction('use');
if (useAction) {
if (
useAction.animation === 'eat' ||
useAction.animation === 'drink'
) {
return 'joy';
}
if (
useAction.behavior === 'sword' ||
useAction.ik === 'pistol' ||
useAction.ik === 'bow'
) {
return 'angry';
}
}
const jumpAction = this.player.getAction('jump');
if (jumpAction) {
return 'fun';
}
const narutoRunAction = this.player.getAction('narutoRun');
if (narutoRunAction) {
return 'angry';
}
const danceAction = this.player.getAction('dance');
if (danceAction) {
return 'joy';
}
return '';
})();
this.emotion = emotion;
}
update() {
if (this.emotion !== this.lastRenderedEmotion) {
const emotionIndex = allEmotions.indexOf(this.emotion);
if (emotionIndex !== -1) {
const sourceCanvas = this.emotionCanvases[emotionIndex];
if (sourceCanvas) {
for (const dstCanvas of this.canvases) {
const {ctx} = dstCanvas;
ctx.clearRect(0, 0, dstCanvas.width, dstCanvas.height);
ctx.drawImage(
sourceCanvas,
0,
0,
this.width,
this.height,
0,
0,
dstCanvas.width,
dstCanvas.height
);
}
}
}
this.lastRenderedEmotion = this.emotion;
}
}
destroy() {
this.cleanup();
}
}
export {
AvatarIconer,
};