-
Notifications
You must be signed in to change notification settings - Fork 209
/
avatar-screenshotter.js
148 lines (134 loc) · 3.96 KB
/
avatar-screenshotter.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
import * as THREE from 'three';
import metaversefile from './metaversefile-api.js';
// import Avatar from './avatars/avatars.js';
import npcManager from './npc-manager.js';
import dioramaManager from './diorama.js';
const FPS = 60;
const _makeLights = () => {
const directionalLight = new THREE.DirectionalLight(0xFFFFFF, 5);
directionalLight.position.set(1, 1.5, -2);
directionalLight.updateMatrixWorld();
return [
directionalLight,
];
};
export const screenshotAvatarUrl = async ({
start_url,
width = 300,
height = 300,
canvas,
cameraOffset,
emotion,
}) => {
const player = await npcManager.createNpcAsync({
name: 'sceenshot-npc',
avatarUrl: start_url,
detached: true,
});
const result = await screenshotPlayer({
player,
width,
height,
canvas,
cameraOffset,
emotion,
});
player.destroy();
return result;
};
export const screenshotPlayer = async ({
player,
width = 300,
height = 300,
canvas,
cameraOffset,
emotion,
}) => {
player.position.set(0, 1.5, 0);
player.quaternion.identity();
player.scale.set(1, 1, 1);
player.updateMatrixWorld();
let now = 0;
const timeDiff = 1000/FPS;
const _setTransform = () => {
player.position.y = player.avatar.height;
player.updateMatrixWorld();
};
_setTransform();
const _initializeAnimation = () => {
player.avatar.setTopEnabled(false);
player.avatar.setHandEnabled(0, false);
player.avatar.setHandEnabled(1, false);
player.avatar.setBottomEnabled(false);
player.avatar.inputs.hmd.position.y = player.avatar.height;
player.avatar.inputs.hmd.quaternion.setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI);
player.avatar.inputs.hmd.updateMatrixWorld();
if (emotion) {
player.clearActions();
player.addAction({
type: 'facepose',
emotion,
});
}
};
const _preAnimate = () => {
for (let i = 0; i < FPS*2; i++) {
_animate(now, timeDiff);
};
};
const _updateTarget = (timestamp, timeDiff) => {
let headHeight = player.avatar.avatarHighestPos - player.avatar.avatarNeckPosition.y;
const max = headHeight > player.avatar.shoulderWidth ? headHeight : player.avatar.shoulderWidth; // check whether head width is bigger than head height
let cameraZ = max / (2 * Math.atan((Math.PI * 50) / 360));
const offset = 1. + max * 0.3;
cameraZ *= offset; //multiply offset so that avatar does't fill the icon
cameraOffset.z = -cameraZ;
cameraOffset.y = headHeight * 0.3;
target.matrixWorld.copy(player.avatar.modelBones.Head.matrixWorld)
.decompose(target.position, target.quaternion, target.scale);
target.position.set(player.position.x, target.position.y, player.position.z);
target.quaternion.copy(player.quaternion);
target.matrixWorld.compose(target.position, target.quaternion, target.scale);
};
const _animate = (timestamp, timeDiff) => {
player.updateAvatar(timestamp, timeDiff);
};
// rendering
let writeCanvas;
if (canvas) {
writeCanvas = canvas;
} else {
writeCanvas = document.createElement('canvas');
writeCanvas.width = width;
writeCanvas.height = height;
}
const localLights = _makeLights();
const objects = localLights.concat([
player.avatar.model,
]);
const target = new THREE.Object3D();
const diorama = dioramaManager.createPlayerDiorama({
// target: player,
target,
cameraOffset,
objects,
lights: false,
// label: true,
// outline: true,
// grassBackground: true,
// glyphBackground: true,
detached: true,
});
diorama.addCanvas(writeCanvas);
diorama.setClearColor(0xFFFFFF, 1);
const _render = () => {
_initializeAnimation();
_preAnimate();
_updateTarget(now, timeDiff);
diorama.update(now, timeDiff);
};
_render();
diorama.destroy();
// player.destroy();
return writeCanvas;
};