Skip to content

Commit

Permalink
feat/ambient occlusion (#48)
Browse files Browse the repository at this point in the history
* feat(ambient-occlusion): add ambient occlusion under the effects with default values
  • Loading branch information
blanxii authored Sep 4, 2023
1 parent 15a0a2f commit 9333c1d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/App/App.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ function App() {
onLoaded={() => console.log('female avatar loaded')}

Check warning on line 25 in src/App/App.component.tsx

View workflow job for this annotation

GitHub Actions / Linting

Unexpected console statement
fov={45}
ambientLightIntensity={0}
effects={{
ambientOcclusion: true
}}
>
<StatsGl />
<EnvironmentModel environment="spaceStation" scale={1} />
Expand Down
41 changes: 36 additions & 5 deletions src/components/Avatar/Avatar.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ import React, { Suspense, FC, useMemo, CSSProperties, ReactNode, useEffect } fro
import { Vector3 } from 'three';
import { CameraLighting } from 'src/components/Scene/CameraLighting.component';
import { Environment } from 'src/components/Scene/Environment.component';
import { LightingProps, BaseModelProps, EnvironmentProps, BloomConfiguration, SpawnState } from 'src/types';
import {
LightingProps,
BaseModelProps,
EnvironmentProps,
BloomConfiguration,
SpawnState,
EffectConfiguration
} from 'src/types';
import { BaseCanvas } from 'src/components/BaseCanvas';
import { AnimationModel, HalfBodyModel, StaticModel, PoseModel } from 'src/components/Models';
import {AnimationModel, HalfBodyModel, StaticModel, PoseModel} from 'src/components/Models';
import { isValidFormat, triggerCallback } from 'src/services';
import { Dpr } from '@react-three/fiber';
import { EffectComposer } from '@react-three/postprocessing';
import {EffectComposer, SSAO} 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 Shadow from 'src/components/Shadow/Shadow.component';
import Loader from 'src/components/Loader';
import Bloom from 'src/components/Bloom/Bloom.component';
import {BlendFunction} from "postprocessing";
import { spawnState } from '../../state/spawnAtom';

export const CAMERA = {
Expand Down Expand Up @@ -135,7 +143,13 @@ export interface AvatarProps extends LightingProps, EnvironmentProps, Omit<BaseM
* Field of view of the camera.
*/
fov?: number;

/**
* Control some effects like post-processing effects
*/
effects?: EffectConfiguration;
/**
* Use any three.js(fiber, post-processing) compatible components to render in the scene.
*/
children?: ReactNode;
}

Expand Down Expand Up @@ -177,6 +191,7 @@ const Avatar: FC<AvatarProps> = ({
onLoadedEffect,
onLoadedAnimation,
children,
effects,
fov = 50
}) => {
const setSpawnState = useSetAtom(spawnState);
Expand Down Expand Up @@ -262,14 +277,30 @@ const Avatar: FC<AvatarProps> = ({
{background?.src && <Box {...background} />}
{capture && <Capture {...capture} />}
{style?.background && <BackgroundColor color={style.background as string} />}
<EffectComposer disableNormalPass>
<EffectComposer multisampling={0} autoClear={false}>
<Bloom
luminanceThreshold={bloom?.luminanceThreshold}
luminanceSmoothing={bloom?.luminanceSmoothing}
intensity={bloom?.intensity}
kernelSize={bloom?.kernelSize}
mipmapBlur={bloom?.mipmapBlur}
/>
<>
{effects?.ambientOcclusion && (
<SSAO
blendFunction={BlendFunction.MULTIPLY}
distanceScaling={false}
radius={0.09}
bias={0.02}
intensity={3}
samples={20}
worldDistanceThreshold={24}
worldDistanceFalloff={0}
worldProximityThreshold={0}
worldProximityFalloff={6}
/>
)}
</>
</EffectComposer>
</BaseCanvas>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Avatar/Examples.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Posing.argTypes = {
export const environmentModel: StoryFn<typeof Avatar> = (

Check warning on line 140 in src/components/Avatar/Examples.stories.tsx

View workflow job for this annotation

GitHub Actions / Linting

The story should use PascalCase notation: environmentModel
args: AvatarProps & { environmentModel: string; environmentScale: number }
) => (
<Avatar {...args}>
<Avatar {...args} effects={{ ambientOcclusion: true }}>
<EnvironmentModel environment={args.environmentModel! as string} scale={args.environmentScale! as number} />
</Avatar>
);
Expand Down
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ export type BloomConfiguration = {
materialIntensity?: number;
};

export type EffectConfiguration = {
/**
* Enables ambient occlusion for the current scene.
*/
ambientOcclusion?: boolean
}

export interface SpawnState {
/**
* Add a custom loaded effect like particles when avatar is loaded, animate them with a custom animation.
Expand Down

0 comments on commit 9333c1d

Please sign in to comment.