Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vertical swimming. #3251

Merged
merged 5 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions avatars/animationHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
} from './constants.js';

import {
crouchMaxTime,
defaultMaxTime,
// useMaxTime,
aimMaxTime,
// avatarInterpolationFrameRate,
Expand All @@ -53,6 +53,9 @@ const localQuaternion3 = new Quaternion();
const localQuaternion4 = new Quaternion();
const localQuaternion5 = new Quaternion();
const localQuaternion6 = new Quaternion();
const localQuaternion7 = new Quaternion();
const localQuaternion8 = new Quaternion();
const localQuaternion9 = new Quaternion();

const identityQuaternion = new Quaternion();

Expand Down Expand Up @@ -833,7 +836,7 @@ export const _applyAnimation = (avatar, now, moveFactors, timeDiffS) => {
const t2 = (now / 1000) % danceAnimation.duration;
const v2 = src2.evaluate(t2);

const danceFactorS = avatar.danceFactor / crouchMaxTime;
const danceFactorS = avatar.danceFactor / defaultMaxTime;
const f = Math.min(Math.max(danceFactorS, 0), 1);
lerpFn
.call(
Expand Down Expand Up @@ -864,7 +867,7 @@ export const _applyAnimation = (avatar, now, moveFactors, timeDiffS) => {
const t2 = Math.min(emoteTime / 1000, emoteAnimation.duration);
const v2 = src2.evaluate(t2);

const emoteFactorS = avatar.emoteFactor / crouchMaxTime;
const emoteFactorS = avatar.emoteFactor / defaultMaxTime;
const f = Math.min(Math.max(emoteFactorS, 0), 1);
lerpFn
.call(
Expand Down Expand Up @@ -1225,9 +1228,10 @@ export const _applyAnimation = (avatar, now, moveFactors, timeDiffS) => {
animationTrackName: k,
dst,
isPosition,
boneName,
isFirstBone,
} = spec;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like VSCode auto formatting.

I know we have a bunch of red squigglies, but can we remove these / paste them back in for the sake of making a really simple PR and then we can do formatting separately?

if (avatar.swimState) {
const swimTimeS = avatar.swimTime / 1000;
const movementsTimeS = avatar.movementsTime / 1000;
Expand All @@ -1253,25 +1257,46 @@ export const _applyAnimation = (avatar, now, moveFactors, timeDiffS) => {
localQuaternion2.fromArray(v2);
localQuaternion3.fromArray(v3);
localQuaternion4.fromArray(v4);
localQuaternion5.fromArray([0,0,0,1]);
localQuaternion6.fromArray([1,0,0,0]);

// // can't use idleWalkFactor & walkRunFactor here, otherwise "Impulsive breaststroke swim animation" will turn into "freestyle animation" when speed is fast,
// // and will turn into "floating" a little when speed is slow.
// localQuaternion3.slerp(localQuaternion4, walkRunFactor);
// localQuaternion2.slerp(localQuaternion3, idleWalkFactor);
localQuaternion3.slerp(localQuaternion4, avatar.sprintFactor);
localQuaternion2.slerp(localQuaternion3, avatar.movementsTransitionFactor);
if(!avatar.swimmingOnSurfaceState || (avatar.swimmingOnSurfaceState && avatar.horizontalMovementsTransitionFactor > 0)) {
localQuaternion3.slerp(localQuaternion4, avatar.sprintFactor);
localQuaternion2.slerp(localQuaternion3, avatar.movementsTransitionFactor);
}

if(boneName === 'Hips') {
if(avatar.swimUpFactor > 0 && avatar.horizontalMovementsTransitionFactor === 0) {
localQuaternion2.slerp(localQuaternion5, avatar.swimUpFactor * avatar.surfaceFactor);
} else if(avatar.swimDownFactor > 0 && avatar.horizontalMovementsTransitionFactor === 0) {
localQuaternion2.slerp(localQuaternion6, avatar.swimDownFactor);
} else {
const fU = avatar.swimUpFactor / (avatar.swimUpFactor + avatar.horizontalMovementsTransitionFactor) * avatar.surfaceFactor || 0;
const fD = avatar.swimDownFactor / (avatar.swimDownFactor + avatar.horizontalMovementsTransitionFactor) * avatar.surfaceFactor || 0;
localQuaternion2.slerp(localQuaternion5, fU);
localQuaternion2.slerp(localQuaternion6, fD);
}
}
dst.slerp(localQuaternion2, f);

} else {
const liftSwims = 0.05; // lift swims height, prevent head sink in water
localVector2.fromArray(v2);
localVector3.fromArray(v3);
localVector3.y += 0.21; // align Swimming.fbx's height to freestyle.fbx
localVector3.y += liftSwims;
localVector4.fromArray(v4);
localVector4.y += liftSwims;
// localVector3.lerp(localVector4, walkRunFactor);
// localVector2.lerp(localVector3, idleWalkFactor);
localVector3.lerp(localVector4, avatar.sprintFactor);
localVector2.lerp(localVector3, avatar.movementsTransitionFactor);
if(!avatar.swimmingOnSurfaceState || (avatar.swimmingOnSurfaceState && avatar.horizontalMovementsTransitionFactor > 0)) {
const liftSwims = 0.05; // lift swims height, prevent head sink in water
localVector3.fromArray(v3);
localVector3.y += 0.21; // align Swimming.fbx's height to freestyle.fbx
localVector3.y += liftSwims;
localVector4.fromArray(v4);
localVector4.y += liftSwims;
// localVector3.lerp(localVector4, walkRunFactor);
// localVector2.lerp(localVector3, idleWalkFactor);
localVector3.lerp(localVector4, avatar.sprintFactor);
localVector2.lerp(localVector3, avatar.movementsTransitionFactor);
}
dst.lerp(localVector2, f);
}
}
Expand Down
14 changes: 9 additions & 5 deletions avatars/avatars.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '../util.js';
// import Simplex from '../simplex-noise.js';
import {
crouchMaxTime,
defaultMaxTime,
// useMaxTime,
aimMaxTime,
aimTransitionMaxTime,
Expand Down Expand Up @@ -936,7 +936,7 @@ class Avatar {
this.aimLeftFactorReverse = 1;
// this.throwState = null;
// this.throwTime = 0;
this.crouchTime = crouchMaxTime;
this.crouchTime = defaultMaxTime;
this.sitTarget = new THREE.Object3D();
this.fakeSpeechValue = 0;
this.fakeSpeechSmoothed = 0;
Expand Down Expand Up @@ -1493,14 +1493,18 @@ class Avatar {
const moveFactors = {};
moveFactors.idleWalkFactor = Math.min(Math.max((currentSpeed - idleFactorSpeed) / (walkFactorSpeed - idleFactorSpeed), 0), 1);
moveFactors.walkRunFactor = Math.min(Math.max((currentSpeed - walkFactorSpeed) / (runFactorSpeed - walkFactorSpeed), 0), 1);
moveFactors.crouchFactor = Math.min(Math.max(1 - (this.crouchTime / crouchMaxTime), 0), 1);
moveFactors.crouchFactor = Math.min(Math.max(1 - (this.crouchTime / defaultMaxTime), 0), 1);
// console.log('current speed', currentSpeed, idleWalkFactor, walkRunFactor);
this.aimRightFactor = this.aimRightTransitionTime / aimTransitionMaxTime;
this.aimRightFactorReverse = 1 - this.aimRightFactor;
this.aimLeftFactor = this.aimLeftTransitionTime / aimTransitionMaxTime;
this.aimLeftFactorReverse = 1 - this.aimLeftFactor;
this.movementsTransitionFactor = Math.min(Math.max(this.movementsTransitionTime / crouchMaxTime, 0), 1);
this.sprintFactor = Math.min(Math.max(this.sprintTime / crouchMaxTime, 0), 1);
this.movementsTransitionFactor = Math.min(Math.max(this.movementsTransitionTime / defaultMaxTime, 0), 1);
this.horizontalMovementsTransitionFactor = Math.min(Math.max(this.horizontalMovementsTransitionTime / defaultMaxTime, 0), 1);
this.sprintFactor = Math.min(Math.max(this.sprintTime / defaultMaxTime, 0), 1);
this.swimUpFactor = Math.min(Math.max(this.swimUpTime / defaultMaxTime, 0), 1);
this.swimDownFactor = Math.min(Math.max(this.swimDownTime / defaultMaxTime, 0), 1);
this.surfaceFactor = Math.min(Math.max(this.swimmingOnSurfaceTime / defaultMaxTime, 0), 1);

const _updateHmdPosition = () => {
const currentPosition = this.inputs.hmd.position;
Expand Down
31 changes: 23 additions & 8 deletions character-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
avatarMapName,
appsMapName,
playersMapName,
crouchMaxTime,
defaultMaxTime,
activateMaxTime,
// useMaxTime,
aimTransitionMaxTime,
Expand Down Expand Up @@ -909,7 +909,7 @@ class InterpolatedPlayer extends StatePlayer {
};
this.actionBinaryTimeStepsArray = Object.keys(this.actionBinaryTimeSteps).map(k => this.actionBinaryTimeSteps[k]);
this.actionInterpolants = {
crouch: new BiActionInterpolant(() => this.actionBinaryInterpolants.crouch.get(), 0, crouchMaxTime),
crouch: new BiActionInterpolant(() => this.actionBinaryInterpolants.crouch.get(), 0, defaultMaxTime),
activate: new UniActionInterpolant(() => this.actionBinaryInterpolants.activate.get(), 0, activateMaxTime),
use: new InfiniteActionInterpolant(() => this.actionBinaryInterpolants.use.get(), 0),
pickUp: new InfiniteActionInterpolant(() => this.actionBinaryInterpolants.pickUp.get(), 0),
Expand Down Expand Up @@ -961,7 +961,7 @@ class UninterpolatedPlayer extends StatePlayer {
}
static init() {
this.actionInterpolants = {
crouch: new BiActionInterpolant(() => this.hasAction('crouch'), 0, crouchMaxTime),
crouch: new BiActionInterpolant(() => this.hasAction('crouch'), 0, defaultMaxTime),
activate: new UniActionInterpolant(() => this.hasAction('activate'), 0, activateMaxTime),
use: new InfiniteActionInterpolant(() => this.hasAction('use'), 0),
pickUp: new InfiniteActionInterpolant(() => this.hasAction('pickUp'), 0),
Expand All @@ -973,20 +973,35 @@ class UninterpolatedPlayer extends StatePlayer {
fly: new InfiniteActionInterpolant(() => this.hasAction('fly'), 0),
swim: new InfiniteActionInterpolant(() => this.hasAction('swim'), 0),
jump: new InfiniteActionInterpolant(() => this.hasAction('jump'), 0),
dance: new BiActionInterpolant(() => this.hasAction('dance'), 0, crouchMaxTime),
emote: new BiActionInterpolant(() => this.hasAction('emote'), 0, crouchMaxTime),
dance: new BiActionInterpolant(() => this.hasAction('dance'), 0, defaultMaxTime),
emote: new BiActionInterpolant(() => this.hasAction('emote'), 0, defaultMaxTime),
movements: new InfiniteActionInterpolant(() => {
const ioManager = metaversefile.useIoManager();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably move the useIoManager call up to the top of the function and then re-use it

Copy link
Contributor Author

@patriboz patriboz Jul 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to move it up to the top of the function, but when doing so, metaversefile somehow is only an empty EventTarget object and useIoManager() isn't defined. So I'll keep it as it is for now.

return ioManager.keys.up || ioManager.keys.down || ioManager.keys.left || ioManager.keys.right;
return ioManager.keys.up || ioManager.keys.down || ioManager.keys.left || ioManager.keys.right || ioManager.keys.space || ioManager.keys.ctrl;
}, 0),
movementsTransition: new BiActionInterpolant(() => {
const ioManager = metaversefile.useIoManager();
return ioManager.keys.up || ioManager.keys.down || ioManager.keys.left || ioManager.keys.right || ioManager.keys.space || ioManager.keys.ctrl;
}, 0, defaultMaxTime),
horizontalMovementsTransition: new BiActionInterpolant(() => {
const ioManager = metaversefile.useIoManager();
return ioManager.keys.up || ioManager.keys.down || ioManager.keys.left || ioManager.keys.right;
}, 0, crouchMaxTime),
}, 0, defaultMaxTime),
sprint: new BiActionInterpolant(() => {
const ioManager = metaversefile.useIoManager();
return ioManager.keys.shift;
}, 0, crouchMaxTime),
}, 0, defaultMaxTime),
swimUp: new BiActionInterpolant(() => {
const ioManager = metaversefile.useIoManager();
return ioManager.keys.space;
}, 0, defaultMaxTime),
swimDown: new BiActionInterpolant(() => {
const ioManager = metaversefile.useIoManager();
return ioManager.keys.ctrl;
}, 0, defaultMaxTime),
surface: new BiActionInterpolant(() => {
return !this.avatar.swimmingOnSurfaceState;
}, 0, defaultMaxTime),
// throw: new UniActionInterpolant(() => this.hasAction('throw'), 0, throwMaxTime),
// chargeJump: new InfiniteActionInterpolant(() => this.hasAction('chargeJump'), 0),
// standCharge: new InfiniteActionInterpolant(() => this.hasAction('standCharge'), 0),
Expand Down
4 changes: 2 additions & 2 deletions character-sfx.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
narutoRunTimeFactor,
} from './avatars/constants.js';
import {
crouchMaxTime,
defaultMaxTime,
eatFrameIndices,
drinkFrameIndices,
} from './constants.js';
Expand Down Expand Up @@ -111,7 +111,7 @@ class CharacterSfx {

const idleWalkFactor = Math.min(Math.max((currentSpeed - idleFactorSpeed) / (walkFactorSpeed - idleFactorSpeed), 0), 1);
const walkRunFactor = Math.min(Math.max((currentSpeed - walkFactorSpeed) / (runFactorSpeed - walkFactorSpeed), 0), 1);
const crouchFactor = Math.min(Math.max(1 - (this.player.avatar.crouchTime / crouchMaxTime), 0), 1);
const crouchFactor = Math.min(Math.max(1 - (this.player.avatar.crouchTime / defaultMaxTime), 0), 1);

const soundFiles = sounds.getSoundFiles();
// const soundFileAudioBuffer = sounds.getSoundFileAudioBuffer();
Expand Down
2 changes: 1 addition & 1 deletion constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const metaverseProfileDefinition = `kjzl6cwe1jw145wm7u2sy1wpa33hglvmuy6th

export const audioTimeoutTime = 10 * 1000;

export const crouchMaxTime = 200;
export const defaultMaxTime = 200;
export const activateMaxTime = 750;
export const useMaxTime = 750;
export const aimMaxTime = 1000;
Expand Down
8 changes: 8 additions & 0 deletions player-avatar-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export function applyPlayerActionsToAvatar(player, rig) {
const jumpAction = player.getAction('jump');
const flyAction = player.getAction('fly');
const swimAction = player.getAction('swim');
const swimUpAction = player.getAction('swimUp');
const swimDownAction = player.getAction('swimDown');
const swimmingOnSurface = player.getAction('swim') ? player.getAction('swim').onSurface : false;
const useAction = player.getAction('use');
const pickUpAction = player.getAction('pickUp');
const narutoRunAction = player.getAction('narutoRun');
Expand Down Expand Up @@ -104,6 +107,11 @@ export function applyPlayerActionsToAvatar(player, rig) {
rig.activateTime = player.actionInterpolants.activate.get();
rig.swimState = !!swimAction;
rig.swimTime = swimAction ? player.actionInterpolants.swim.get() : -1;
rig.swimUpTime = player.actionInterpolants.swimUp.get();
rig.swimDownTime = player.actionInterpolants.swimDown.get();
rig.horizontalMovementsTransitionTime = player.actionInterpolants.horizontalMovementsTransition.get();
rig.swimmingOnSurfaceState = !!swimmingOnSurface;
rig.swimmingOnSurfaceTime = player.actionInterpolants.surface.get();

const _handleUse = () => {
if (useAction?.animation) {
Expand Down
3 changes: 0 additions & 3 deletions scenes/water.scn
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
"fog": {
"fogType": "exp",
"args": [[0, 5, 10], 0]
},
"ssr": {

}
}
},
Expand Down