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

fix(performance): ACT-1219 performance optimisations #109

Merged
merged 11 commits into from
Jan 2, 2025
3 changes: 3 additions & 0 deletions src/App/components/Nova.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export const AvatarNova: FC = () => {
style={{ background: 'rgb(9,20,26)' }}
fov={45}
cameraInitialDistance={CAMERA.CONTROLS.FULL_BODY.MAX_DISTANCE}
effects={{
ambientOcclusion: false
}}
>
<StatsGl />
</Avatar>
Expand Down
36 changes: 15 additions & 21 deletions src/components/Avatar/Avatar.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ import {
EffectConfiguration,
LightingProps,
MaterialConfiguration,
AnimationsT
AnimationsT,
CanvasConfiguration
} from 'src/types';
import { BaseCanvas } from 'src/components/BaseCanvas';
import { AnimationModel, HalfBodyModel, StaticModel, PoseModel, MultipleAnimationModel } from 'src/components/Models';
import { isValidFormat, triggerCallback } from 'src/services';
import { Dpr } from '@react-three/fiber';
import { BrightnessContrast, EffectComposer, HueSaturation, SSAO, Vignette } from '@react-three/postprocessing';
import { EffectComposer, HueSaturation, N8AO, Vignette } from '@react-three/postprocessing';
import { Provider, useSetAtom } from 'jotai';
import Capture, { CaptureType } from 'src/components/Capture/Capture.component';
import { Box, Background } from 'src/components/Background/Box/Box.component';
import { BackgroundColor } from 'src/components/Background';
import Loader from 'src/components/Loader';
import Bloom from 'src/components/Bloom/Bloom.component';
import { BlendFunction } from 'postprocessing';

import Lights from 'src/components/Lights/Lights.component';
import { spawnState } from 'src/state/spawnAtom';

Expand Down Expand Up @@ -162,6 +163,10 @@ export interface AvatarProps extends LightingProps, EnvironmentProps, Omit<BaseM
onAnimationEnd?: (action: AnimationAction) => void;
controlsMinDistance?: number;
controlsMaxDistance?: number;
/**
* Control properties of the BaseCanvas.
*/
canvasConfig?: CanvasConfiguration;
}

/**
Expand Down Expand Up @@ -209,7 +214,8 @@ const Avatar: FC<AvatarProps> = ({
onAnimationEnd,
materialConfig,
controlsMinDistance,
controlsMaxDistance
controlsMaxDistance,
canvasConfig
}) => {
const setSpawnState = useSetAtom(spawnState);

Expand Down Expand Up @@ -336,6 +342,7 @@ const Avatar: FC<AvatarProps> = ({
style={style}
dpr={dpr}
className={className}
canvasConfig={canvasConfig}
>
<Environment environment={environment} enablePostProcessing={enablePostProcessing} />
<CameraControls
Expand All @@ -348,28 +355,15 @@ const Avatar: FC<AvatarProps> = ({
/>
{AvatarModel}
{children}
{shadows && <ContactShadows position={[0, 0, 0]} opacity={2} scale={10} blur={1.0} far={1.0} />}
{shadows && <ContactShadows position={[0, 0, 0]} opacity={2} scale={10} blur={1.0} far={1.0} resolution={256} />}
{background?.src && <Box {...background} />}
{capture && <Capture {...capture} />}
{background?.color && <BackgroundColor color={background.color} />}
{enablePostProcessing && (
<EffectComposer autoClear multisampling={4} enableNormalPass={effects?.ambientOcclusion}>
<EffectComposer autoClear enableNormalPass={effects?.ambientOcclusion}>
<>
{effects?.ambientOcclusion && (
<SSAO
blendFunction={BlendFunction.MULTIPLY}
distanceScaling={false}
radius={0.08}
bias={0.01}
intensity={3}
samples={31}
worldDistanceThreshold={24}
worldDistanceFalloff={0}
worldProximityThreshold={0}
worldProximityFalloff={6}
fade={0.02}
rings={8}
/>
<N8AO quality="performance" aoRadius={5} distanceFalloff={0.25} intensity={3} screenSpaceRadius halfRes />
Jarvv marked this conversation as resolved.
Show resolved Hide resolved
)}
{effects?.bloom && (
<Bloom
Expand All @@ -381,7 +375,7 @@ const Avatar: FC<AvatarProps> = ({
/>
)}
{effects?.vignette && <Vignette eskil={false} offset={0.5} darkness={0.5} />}
<BrightnessContrast brightness={0.025} contrast={0.25} />
Jarvv marked this conversation as resolved.
Show resolved Hide resolved

<HueSaturation hue={0} saturation={-0.2} />
</>
</EffectComposer>
Expand Down
10 changes: 6 additions & 4 deletions src/components/BaseCanvas/BaseCanvas.component.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { ReactNode, FC, CSSProperties } from 'react';
import { Canvas, Dpr } from '@react-three/fiber';
import { ACESFilmicToneMapping, Vector3 } from 'three';
import { CameraProps } from 'src/types';
import { CameraProps, CanvasConfiguration } from 'src/types';
import { hasWindow } from 'src/services/Client.service';
import styles from './BaseCanvas.module.scss';

Expand All @@ -12,6 +12,7 @@ interface BaseCanvasProps extends CameraProps {
dpr?: Dpr;
className?: string;
enablePostProcessing?: boolean;
canvasConfig?: CanvasConfiguration;
}

const BASE_DPR = hasWindow ? window.devicePixelRatio : 1;
Expand All @@ -22,16 +23,17 @@ export const BaseCanvas: FC<BaseCanvasProps> = ({
fov = 50,
position = new Vector3(0, 0, 5),
style,
dpr = [BASE_DPR * 0.5, 2],
className
dpr = [BASE_DPR * 0.5, BASE_DPR * 0.75],
Jarvv marked this conversation as resolved.
Show resolved Hide resolved
className,
canvasConfig = { alpha: true }
}) => (
<Canvas
key={fov}
className={`${styles['base-canvas']} ${className ?? ''}`}
shadows="soft"
gl={{
preserveDrawingBuffer: true,
alpha: true,
alpha: canvasConfig.alpha,
toneMapping: ACESFilmicToneMapping,
toneMappingExposure: 1.8
}}
Expand Down
3 changes: 0 additions & 3 deletions src/components/Lights/Lights.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ const Lights: FC<LightingProps> = (lightingProps) => {
angle={LIGHT_CONFIG.keyLightAngle}
color={keyLightColor}
intensity={keyLightIntensity}
castShadow
/>
{/* Lift light that creates soft light on body and shoes. */}
<spotLight
Expand All @@ -93,7 +92,6 @@ const Lights: FC<LightingProps> = (lightingProps) => {
angle={LIGHT_CONFIG.keyLightAngle}
color={keyLightColor}
intensity={keyLightIntensity * 0.25}
castShadow
/>
{/* Silhouette light on arms and legs. */}
<spotLight
Expand All @@ -102,7 +100,6 @@ const Lights: FC<LightingProps> = (lightingProps) => {
angle={LIGHT_CONFIG.silhouetteLightAngle}
color={keyLightColor}
intensity={keyLightIntensity * 0.25}
castShadow
Jarvv marked this conversation as resolved.
Show resolved Hide resolved
/>
</group>
);
Expand Down
4 changes: 1 addition & 3 deletions src/components/Models/Model/Model.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ export const Model: FC<ModelProps> = ({

if ((node as Mesh).isMesh) {
node.castShadow = true;
}

if (node.type === 'SkinnedMesh') {
Jarvv marked this conversation as resolved.
Show resolved Hide resolved
node.receiveShadow = true;
node.frustumCulled = false;
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/components/Shadow/Shadow.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ const Shadow: FC = () => (
</group>
);

export default Shadow;
export default Shadow;
4 changes: 4 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,7 @@ export type AnimationsT = Record<
fadeTime?: number;
}
>;

export type CanvasConfiguration = {
alpha?: boolean
}
Loading