Skip to content

Commit

Permalink
Lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
hmans committed Jun 9, 2022
1 parent 81332ae commit 793bed5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-chicken-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vfx": patch
---

**New:** `<Lifetime seconds={...}>` will give its children the specified lifetime and then unmount them.
51 changes: 30 additions & 21 deletions apps/examples/src/effects/Explosion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { CameraShake } from "@react-three/drei"
import { GroupProps } from "@react-three/fiber"
import { between, plusMinus, power } from "randomish"
import { Color, MeshStandardMaterial, Vector3 } from "three"
import { Delay, Emitter, MeshParticles, ParticlesMaterial, Repeat } from "vfx"
import {
Delay,
Emitter,
Lifetime,
MeshParticles,
ParticlesMaterial,
Repeat
} from "vfx"

const gravity = new Vector3(0, -20, 0)
const direction = new Vector3()
Expand Down Expand Up @@ -145,30 +152,32 @@ const SmokeCloud = () => (

const Explosion = (props: GroupProps) => (
<group {...props}>
<SmokeRing />

<Delay seconds={0.1}>
<Fireball />

<CameraShake
maxYaw={0.05}
maxPitch={0.05}
maxRoll={0.05}
yawFrequency={10}
pitchFrequency={20}
rollFrequency={2}
decayRate={2.5}
decay
/>

<Delay seconds={0.2}>
<Dirt />
<Lifetime seconds={3}>
<SmokeRing />

<Delay seconds={0.1}>
<Fireball />

<CameraShake
maxYaw={0.05}
maxPitch={0.05}
maxRoll={0.05}
yawFrequency={10}
pitchFrequency={20}
rollFrequency={2}
decayRate={2.5}
decay
/>

<Delay seconds={0.2}>
<SmokeCloud />
<Dirt />

<Delay seconds={0.2}>
<SmokeCloud />
</Delay>
</Delay>
</Delay>
</Delay>
</Lifetime>
</group>
)

Expand Down
6 changes: 1 addition & 5 deletions packages/vfx/src/utilities/Delay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ type DelayProps = {
onComplete?: () => void
}

export const Delay: FC<DelayProps> = ({
seconds = 0,
onComplete,
children
}) => {
export const Delay: FC<DelayProps> = ({ seconds, onComplete, children }) => {
const ready = useDelay(seconds, onComplete)
return ready ? <>{children}</> : null
}
Expand Down
14 changes: 14 additions & 0 deletions packages/vfx/src/utilities/Lifetime.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { FC } from "react"
import { ReactNode } from "react"
import { useDelay } from "./Delay"

type LifetimeProps = {
children?: ReactNode
seconds: number
}

export const Lifetime: FC<LifetimeProps> = ({ children, seconds }) => {
const ready = useDelay(seconds)

return ready ? null : <>{children}</>
}
1 change: 1 addition & 0 deletions packages/vfx/src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./Delay"
export * from "./Lifetime"
export * from "./Repeat"

0 comments on commit 793bed5

Please sign in to comment.