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

Animations | PORTAL-716 #105

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 22 additions & 33 deletions src/App/components/Nova.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,50 @@
import React, { useMemo } from 'react';
import React, { useMemo, useState, FC } from 'react';
import { StatsGl } from '@react-three/drei';

import { Avatar, CAMERA } from 'src/components/Avatar';
import { emotions } from 'src/services/Stories.service';
import { SettingsPanel } from './SettingsPanel';

const idleUrl = 'https://readyplayerme-assets.s3.amazonaws.com/animations/nova-male-idle.glb';
const victoryUrl = 'https://readyplayerme-assets.s3.amazonaws.com/animations/nova-victory-03.glb';
const getAnimation = (name: string) => `https://readyplayerme-assets.s3.amazonaws.com/animations/${name}.glb`;
const getAnimationNexus = (name: string) =>
`https://readyplayerme-assets.s3.amazonaws.com/nexus/animations/${name}.glb`;

const models: Record<string, string> = {
one: 'https://avatars.readyplayer.me/673dc84d6874800e1db46095.glb?meshCompression=true&textureQuality=low&meshLOD=1&morphTargetsGroup=Basic expressions',
two: 'https://avatars.readyplayer.me/673f109553f9ed9312fafe70.glb?meshCompression=true&textureQuality=low&meshLOD=1&morphTargetsGroup=Basic expressions',
three:
'https://avatars.readyplayer.me/6745ca78c6e2d65e7b99d8e1.glb?meshCompression=true&textureQuality=low&meshLOD=1&morphTargetsGroup=Basic expressions',
four: 'https://avatars.readyplayer.me/6745df2bd7ae3ba1d340d019.glb?meshCompression=true&textureQuality=low&meshLOD=1&morphTargetsGroup=Basic expressions'
};
const modelSrc = 'https://avatars.readyplayer.dev/673b3cf27b275e2ca5be2800.glb';

export const AvatarNova: React.FC = () => {
const [activeAnimation, setActiveAnimation] = React.useState<string>('idle');
const [modelSrc, setModelSrc] = React.useState<string>(models.one);
export const AvatarNova: FC = () => {
const [activeAnimation, setActiveAnimation] = useState<string>('idle');

const animations = useMemo(
() => ({
ericjansenrpm marked this conversation as resolved.
Show resolved Hide resolved
idle: idleUrl,
victory: victoryUrl
idle: getAnimation('nova-male-idle'),
victory: getAnimation('nova-victory-03'),
idle5: getAnimationNexus('compressed_idle_nova_5'),
idle53: getAnimationNexus('compressed_idle_nova_53'),
idle7: getAnimationNexus('compressed_idle_nova_7'),
idle80: getAnimationNexus('compressed_idle_nova_80'),
top: getAnimation('novamale_changetopwear_v001'),
bottom: getAnimation('novamale_changebottomwear_01_v001'),
foot: getAnimation('NovaMale_ChangeFootwear_v002')
}),
[]
);

return (
<>
<SettingsPanel>
<button type="button" onClick={() => setActiveAnimation('idle')}>
Set idle animation
</button>
<button type="button" onClick={() => setActiveAnimation('victory')}>
Set victory animation
</button>
<button type="button" onClick={() => setModelSrc(models.one)}>
Set Model1
</button>
<button type="button" onClick={() => setModelSrc(models.two)}>
Set Model2
</button>
<button type="button" onClick={() => setModelSrc(models.three)}>
Set Model3
</button>
<button type="button" onClick={() => setModelSrc(models.four)}>
Set Model4
</button>
{Object.keys(animations).map((name) => (
<button key={name} type="button" onClick={() => setActiveAnimation(name)}>
Set {name} animation
</button>
))}
</SettingsPanel>
<Avatar
modelSrc={modelSrc}
emotion={emotions.smile}
animations={animations}
activeAnimation={activeAnimation}
shadows
onAnimationEnd={(action) => action}
style={{ background: 'rgb(9,20,26)' }}
fov={45}
cameraInitialDistance={CAMERA.CONTROLS.FULL_BODY.MAX_DISTANCE}
Expand Down
16 changes: 12 additions & 4 deletions src/components/Avatar/Avatar.component.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Suspense, FC, useMemo, CSSProperties, ReactNode, useEffect } from 'react';
import { Vector3 } from 'three';
import { AnimationAction, Vector3 } from 'three';
import { ContactShadows } from '@react-three/drei';
import { CameraControls } from 'src/components/Scene/CameraControls.component';
import { Environment } from 'src/components/Scene/Environment.component';
Expand Down Expand Up @@ -163,6 +163,8 @@ export interface AvatarProps extends LightingProps, EnvironmentProps, Omit<BaseM
* Control properties of materials.
*/
materialConfig?: MaterialConfiguration;
onAnimationEnd?: (action: AnimationAction) => void;
idleAnimation?: string;
}

/**
Expand Down Expand Up @@ -208,6 +210,8 @@ const Avatar: FC<AvatarProps> = ({
lightTarget,
fov = 50,
animationConfig,
onAnimationEnd,
idleAnimation,
materialConfig
}) => {
const setSpawnState = useSetAtom(spawnState);
Expand All @@ -230,7 +234,9 @@ const Avatar: FC<AvatarProps> = ({
activeAnimation={activeAnimation}
scale={scale}
onLoaded={onLoaded}
idleAnimation={idleAnimation}
bloom={effects?.bloom}
onAnimationEnd={onAnimationEnd}
animationConfig={animationConfig}
materialConfig={materialConfig}
/>
Expand Down Expand Up @@ -303,10 +309,12 @@ const Avatar: FC<AvatarProps> = ({
onLoaded,
emotion,
effects?.bloom,
idleRotation,
headMovement,
materialConfig,
idleAnimation,
onAnimationEnd,
animationConfig,
materialConfig
idleRotation,
headMovement
]);

useEffect(() => triggerCallback(onLoading), [modelSrc, animationSrc, onLoading]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { FC, useEffect, useRef } from 'react';
import { useFrame } from '@react-three/fiber';
import { AnimationAction, AnimationMixer } from 'three';
import { AnimationAction, AnimationMixer, LoopOnce, LoopRepeat } from 'three';

import { Model } from 'src/components/Models/Model';
import { AnimationConfiguration, BaseModelProps } from 'src/types';
Expand All @@ -15,6 +15,8 @@ export interface MultipleAnimationModelProps extends BaseModelProps {
scale?: number;
emotion?: Emotion;
animationConfig?: AnimationConfiguration;
onAnimationEnd?: (action: AnimationAction) => void;
idleAnimation?: string;
}

export const MultipleAnimationModel: FC<MultipleAnimationModelProps> = ({
Expand All @@ -27,7 +29,9 @@ export const MultipleAnimationModel: FC<MultipleAnimationModelProps> = ({
emotion,
bloom,
animationConfig,
materialConfig
materialConfig,
onAnimationEnd,
idleAnimation = 'idle'
}) => {
const mixerRef = useRef<AnimationMixer | null>(null);
const activeActionRef = useRef<AnimationAction | null>(null);
Expand All @@ -43,7 +47,6 @@ export const MultipleAnimationModel: FC<MultipleAnimationModelProps> = ({

if (activeActionRef.current) {
const newAction = mixer.clipAction(activeActionRef.current.getClip());

newAction.play();
mixer.update(animationTimeRef.current);

Expand All @@ -64,19 +67,47 @@ export const MultipleAnimationModel: FC<MultipleAnimationModelProps> = ({
const newClip = loadedAnimations[activeAnimation];

if (!newClip || !mixer) return;
if (prevAction && prevAction.getClip().name === newClip.name) return;
if (prevAction && prevAction.getClip().name === newClip.name && activeAnimation !== idleAnimation) return;

const newAction = mixer.clipAction(newClip);
activeActionRef.current = newAction;
if (activeAnimation === idleAnimation) {
ericjansenrpm marked this conversation as resolved.
Show resolved Hide resolved
newAction.setLoop(LoopRepeat, Infinity);
} else {
newAction.setLoop(LoopOnce, 1);
newAction.clampWhenFinished = true;
}

const handleAnimationEnd = (event: { action: AnimationAction }) => {
if (event.action === newAction) {
onAnimationEnd?.(newAction);
if (activeAnimation !== idleAnimation) {
const idleClip = loadedAnimations[idleAnimation];
if (idleClip) {
const idleAction = mixer.clipAction(idleClip);
Jarvv marked this conversation as resolved.
Show resolved Hide resolved
idleAction.reset().setLoop(LoopRepeat, Infinity).fadeIn(0.5).play();
activeActionRef.current = idleAction;
}
}
}
};

mixer.addEventListener('finished', handleAnimationEnd);

if (prevAction) {
prevAction.fadeOut(0.5);
newAction.reset().fadeIn(0.5);
} else {
newAction.reset();
newAction.crossFadeFrom(prevAction, animationConfig?.crossfadeDuration ?? 0.5, true);
}

newAction.play();
mixer.update(0);
}, [activeAnimation, loadedAnimations, animationConfig]);
activeActionRef.current = newAction;

// eslint-disable-next-line consistent-return
return () => {
mixer.removeEventListener('finished', handleAnimationEnd);
};
}, [activeAnimation, loadedAnimations, animationConfig, onAnimationEnd, idleAnimation]);

useFrame((state, delta) => {
mixerRef.current?.update(delta);
Expand Down
Loading