-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathplayer-avatar-binding.js
342 lines (321 loc) · 12.9 KB
/
player-avatar-binding.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* utils to bind players to their avatars
set the avatar state from the player state */
import * as THREE from 'three';
import Avatar from './avatars/avatars.js';
import {unFrustumCull, enableShadows} from './util.js';
import {
getEyePosition,
} from './avatars/util.mjs';
import {playersManager} from './players-manager.js';
const appSymbol = 'app'; // Symbol('app');
const avatarSymbol = 'avatar'; // Symbol('avatar');
const maxMirrorDistanace = 3;
const localVector = new THREE.Vector3();
const localVector2 = new THREE.Vector3();
const localQuaternion = new THREE.Quaternion();
const localPlane = new THREE.Plane();
export function applyPlayerTransformsToAvatar(player, session, rig) {
if (!session) {
rig.inputs.hmd.position.copy(player.avatarBinding.position);
rig.inputs.hmd.quaternion.copy(player.avatarBinding.quaternion);
rig.inputs.leftGamepad.position.copy(player.leftHand.position);
rig.inputs.leftGamepad.quaternion.copy(player.leftHand.quaternion);
rig.inputs.rightGamepad.position.copy(player.rightHand.position);
rig.inputs.rightGamepad.quaternion.copy(player.rightHand.quaternion);
}
}
/* export function applyPlayerMetaTransformsToAvatar(player, session, rig) {
if (!session) {
rig.velocity.copy(player.characterPhysics.velocity);
}
} */
export function applyPlayerModesToAvatar(player, session, rig) {
for (let i = 0; i < 2; i++) {
rig.setHandEnabled(i, player.hands[i].enabled);
}
rig.setTopEnabled(
(!!session && (rig.inputs.leftGamepad.enabled || rig.inputs.rightGamepad.enabled)),
);
rig.setBottomEnabled(
(
rig.getTopEnabled() /* ||
rig.getHandEnabled(0) ||
rig.getHandEnabled(1) */
) &&
rig.velocity.length() < 0.001,
);
}
export function makeAvatar(app) {
if (app) {
const {skinnedVrm} = app;
const _getPlayerByAppInstanceId = instanceId => {
const remotePlayers = playersManager.getRemotePlayers(); // Might have to be removed too
const localPlayer = playersManager.getLocalPlayer();
const result = localPlayer.appManager.getAppByInstanceId(instanceId);
if (result) {
return localPlayer;
} else {
for (const remotePlayer in remotePlayers) {
if (remotePlayer.appManager.getAppByInstanceId(instanceId)) {
return remotePlayer;
}
}
}
}
const player = _getPlayerByAppInstanceId(app.instanceId);
if (skinnedVrm) {
const avatar = new Avatar(skinnedVrm, {
isLocalPlayer: !player || !player.isRemotePlayer,
fingers: true,
hair: true,
visemes: true,
debug: false,
});
avatar[appSymbol] = app;
unFrustumCull(app);
enableShadows(app);
return avatar;
}
}
return null;
}
export function applyPlayerActionsToAvatar(player, rig) {
const jumpAction = player.getAction('jump');
const doubleJumpAction = player.getAction('doubleJump');
const landAction = player.getAction('land');
const flyAction = player.getAction('fly');
const swimAction = player.getAction('swim');
const useAction = player.getAction('use');
const pickUpAction = player.getAction('pickUp');
const narutoRunAction = player.getAction('narutoRun');
const sitAction = player.getAction('sit');
const sitAnimation = sitAction ? sitAction.animation : '';
const danceAction = player.getAction('dance');
const danceAnimation = danceAction ? danceAction.animation : '';
const emoteAction = player.getAction('emote');
const emoteAnimation = emoteAction ? emoteAction.animation : '';
// const throwAction = player.getAction('throw');
const aimAction = player.getAction('aim');
const crouchAction = player.getAction('crouch');
const wearAction = player.getAction('wear');
// const chargeJump = player.getAction('chargeJump');
// const chargeJumpAnimation = chargeJump ? chargeJump.animation : '';
// const standCharge = player.getAction('standCharge');
// const standChargeAnimation = standCharge ? standCharge.animation : '';
const fallLoopAction = player.getAction('fallLoop');
// const fallLoopAnimation = fallLoopAction ? fallLoopAction.animation : '';
const hurtAction = player.getAction('hurt');
// const swordSideSlash = player.getAction('swordSideSlash');
// const swordSideSlashAnimation = swordSideSlash ? swordSideSlash.animation : '';
// const swordTopDownSlash = player.getAction('swordTopDownSlash');
// const swordTopDownSlashAnimation = swordTopDownSlash ? swordTopDownSlash.animation : '';
rig.jumpState = !!jumpAction;
rig.jumpTime = player.actionInterpolants.jump.get();
rig.doubleJumpState = !!doubleJumpAction;
rig.doubleJumpTime = player.actionInterpolants.doubleJump.get();
rig.landTime = player.actionInterpolants.land.get();
rig.lastLandStartTime = landAction ? landAction.time : 0;
rig.landWithMoving = landAction?.isMoving;
rig.flyState = !!flyAction;
rig.flyTime = flyAction ? player.actionInterpolants.fly.get() : -1;
rig.activateTime = player.actionInterpolants.activate.get();
rig.swimState = !!swimAction;
rig.swimTime = swimAction ? player.actionInterpolants.swim.get() : -1;
const _handleUse = () => {
if (useAction?.animation) {
rig.useAnimation = useAction.animation;
} else {
if (rig.useAnimation) {
rig.useAnimation = '';
}
}
if (useAction?.animationCombo) {
rig.useAnimationCombo = useAction.animationCombo;
} else {
if (rig.useAnimationCombo.length > 0) {
rig.useAnimationCombo = [];
}
}
if (useAction?.animationEnvelope) {
rig.useAnimationEnvelope = useAction.animationEnvelope;
} else {
if (rig.useAnimationEnvelope.length > 0) {
rig.useAnimationEnvelope = [];
}
}
rig.useAnimationIndex = useAction?.index;
rig.useTime = player.actionInterpolants.use.get();
rig.unuseTime = player.actionInterpolants.unuse.get();
if (rig.unuseTime === 0) { // this means use is active
if (useAction?.animationEnvelope) {
rig.unuseAnimation = rig.useAnimationEnvelope[2]; // the last animation in the triplet is the unuse animation
} else {
rig.unuseAnimation = null;
}
}
};
_handleUse();
const _handlePickUp = () => {
rig.pickUpState = !!pickUpAction;
rig.pickUpTime = player.actionInterpolants.pickUp.get();
};
_handlePickUp();
rig.manuallySetMouth = player.characterBehavior.manuallySetMouth;
rig.vowels[1] = player.characterBehavior.manuallySetMouth ? 0 : rig.vowels[1];
rig.vowels[2] = player.characterBehavior.manuallySetMouth ? 0 : rig.vowels[2];
rig.vowels[3] = player.characterBehavior.manuallySetMouth ? 0 : rig.vowels[3];
rig.vowels[4] = player.characterBehavior.manuallySetMouth ? 0 : rig.vowels[4];
rig.narutoRunState = !!narutoRunAction && !crouchAction;
rig.narutoRunTime = player.actionInterpolants.narutoRun.get();
rig.aimState = !!aimAction;
rig.aimTime = player.actionInterpolants.aim.get();
rig.aimRightTransitionTime = player.actionInterpolants.aimRightTransition.get();
rig.aimLeftTransitionTime = player.actionInterpolants.aimLeftTransition.get();
rig.aimAnimation = (aimAction?.playerAnimation) || '';
// rig.aimDirection.set(0, 0, -1);
// aimAction && rig.aimDirection.applyQuaternion(rig.inputs.hmd.quaternion);
rig.sitState = !!sitAction;
rig.sitAnimation = sitAnimation;
// XXX this needs to be based on the current loadout index
rig.holdState = wearAction?.holdAnimation === 'pick_up_idle';
if (rig.holdState) rig.unuseAnimation = null;
// rig.danceState = !!danceAction;
rig.danceFactor = player.actionInterpolants.dance.get();
if (danceAction) {
rig.danceAnimation = danceAnimation;
}
rig.emoteFactor = player.actionInterpolants.emote.get();
rig.emoteAnimation = emoteAnimation;
// rig.throwState = !!throwAction;
// rig.throwTime = player.actionInterpolants.throw.get();
rig.crouchTime = player.actionInterpolants.crouch.getInverse();
// rig.chargeJumpTime = player.actionInterpolants.chargeJump.get();
// rig.chargeAnimation = chargeJumpAnimation;
// rig.chargeJumpState = !!chargeJump;
// rig.standChargeTime = player.actionInterpolants.standCharge.get();
// rig.standChargeAnimation = standChargeAnimation;
// rig.standChargeState = !!standCharge;
rig.fallLoopTime = player.actionInterpolants.fallLoop.get();
rig.fallLoopFactor = player.actionInterpolants.fallLoopTransition.getNormalized();
rig.fallLoopFrom = fallLoopAction ? fallLoopAction.from : '';
// rig.fallLoopAnimation = fallLoopAnimation;
rig.fallLoopState = !!fallLoopAction;
rig.landState = !!landAction;
// rig.swordSideSlashTime = player.actionInterpolants.swordSideSlash.get();
// rig.swordSideSlashAnimation = swordSideSlashAnimation;
// rig.swordSideSlashState = !!swordSideSlash;
// rig.swordTopDownSlashTime = player.actionInterpolants.swordTopDownSlash.get();
// rig.swordTopDownSlashAnimation = swordTopDownSlashAnimation;
// rig.swordTopDownSlashState = !!swordTopDownSlash;
rig.hurtAnimation = (hurtAction?.animation) || '';
rig.hurtTime = player.actionInterpolants.hurt.get();
rig.movementsTime = player.actionInterpolants.movements.get();
rig.movementsTransitionTime = player.actionInterpolants.movementsTransition.get();
rig.sprintTime = player.actionInterpolants.sprint.get();
}
// returns whether headTarget were applied
export function applyPlayerHeadTargetToAvatar(player, rig) {
if (player.headTargetEnabled) {
rig.headTarget.copy(player.headTarget);
rig.headTargetInverted = player.headTargetInverted;
rig.headTargetEnabled = true;
return true;
} else {
rig.headTargetEnabled = false;
return false;
}
}
// returns whether eyes(eyeballs) were applied
export function applyPlayerEyesToAvatar(player, rig) {
if (player.eyeballTargetEnabled) {
rig.eyeballTarget.copy(player.eyeballTarget);
rig.eyeballTargetEnabled = true;
return true;
} else {
rig.eyeballTargetEnabled = false;
return false;
}
}
export function applyMirrorsToAvatar(player, rig, mirrors) {
rig.eyeballTargetEnabled = false;
const closestMirror = mirrors.sort((a, b) => {
const aDistance = player.position.distanceTo(a.position);
const bDistance = player.position.distanceTo(b.position);
return aDistance - bDistance;
})[0];
if (closestMirror) {
// console.log('player bind mirror', closestMirror);
const mirrorPosition = localVector2.setFromMatrixPosition(closestMirror.matrixWorld);
if (mirrorPosition.distanceTo(player.position) < maxMirrorDistanace) {
const eyePosition = getEyePosition(rig.modelBones);
localPlane
.setFromNormalAndCoplanarPoint(
localVector.set(0, 0, 1)
.applyQuaternion(localQuaternion.setFromRotationMatrix(closestMirror.matrixWorld)),
mirrorPosition,
)
.projectPoint(eyePosition, rig.eyeballTarget);
rig.eyeballTargetEnabled = true;
}
}
}
export function applyFacePoseToAvatar(player, rig) {
const facePoseActions = player.getActionsArray().filter(a => a.type === 'facepose');
if (facePoseActions.length > 0) {
player.avatar.faceposes = facePoseActions;
} else {
if (player.avatar.faceposes.length !== 0) {
player.avatar.faceposes.length = 0;
}
}
}
export function applyPlayerPoseToAvatar(player, rig) {
const poseAction = player.getAction('pose');
rig.poseAnimation = poseAction?.animation || null;
}
export function applyPlayerToAvatar(player, session, rig, mirrors) {
applyPlayerTransformsToAvatar(player, session, rig);
// applyPlayerMetaTransformsToAvatar(player, session, rig);
applyPlayerModesToAvatar(player, session, rig);
applyPlayerActionsToAvatar(player, rig);
applyPlayerHeadTargetToAvatar(player, rig);
applyPlayerEyesToAvatar(player, rig) || applyMirrorsToAvatar(player, rig, mirrors);
applyFacePoseToAvatar(player, rig);
applyPlayerPoseToAvatar(player, rig);
}
export function switchAvatar(oldAvatar, newApp) {
let result;
oldAvatar && oldAvatar[appSymbol].toggleBoneUpdates(true);
if (newApp) {
newApp.toggleBoneUpdates(true);
if (!newApp[avatarSymbol]) {
newApp[avatarSymbol] = makeAvatar(newApp);
}
result = newApp[avatarSymbol];
} else {
result = null;
}
return result;
}
/* export async function switchAvatar(oldAvatar, newApp) {
let result;
const promises = [];
if (oldAvatar) {
promises.push((async () => {
await oldAvatar[appSymbol].setSkinning(false);
})());
}
if (newApp) {
// promises.push((async () => {
newApp.toggleBoneUpdates(true);
if (!newApp[avatarSymbol]) {
newApp[avatarSymbol] = makeAvatar(newApp);
}
result = newApp[avatarSymbol];
// })());
} else {
result = null;
}
await Promise.all(promises);
return result;
} */