Skip to content

Commit

Permalink
Naming
Browse files Browse the repository at this point in the history
  • Loading branch information
hmans committed Aug 27, 2022
1 parent aa353e9 commit 334f5fd
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/vfx-composer-r3f/src/Emitter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export const Emitter = forwardRef<Object3D, EmitterProps>(
) => {
const origin = useRef<Object3D>(null!)
const particlesFromContext = useParticlesContext()
const acc = useRef(0)
const remaining = useRef(limit)
const queuedParticles = useRef(0)
const remainingParticles = useRef(limit)

if (rate === Infinity && limit === Infinity) {
throw new Error(
Expand All @@ -51,32 +51,35 @@ export const Emitter = forwardRef<Object3D, EmitterProps>(

const emit = useCallback(
(dt: number) => {
if (remaining.current <= 0) return
if (remainingParticles.current <= 0) return

/* Grab a reference to the particles mesh */
const particles = particlesProp?.current || particlesFromContext
if (!particles) return

/* Increase the accumulated number of particles we're supposed to emit. */
if (rate === Infinity) {
acc.current = Infinity
queuedParticles.current = Infinity
} else {
acc.current += dt * rate
queuedParticles.current += dt * rate
}

/* Is it time to emit? */
if (acc.current >= 1 || rate === Infinity) {
if (queuedParticles.current >= 1 || rate === Infinity) {
/* Determine the amount of particles to emit. Don't go over the number of
remaining particles. */
const amount = Math.min(Math.trunc(acc.current), remaining.current)
const amount = Math.min(
Math.trunc(queuedParticles.current),
remainingParticles.current
)

/* Emit! */
particlesMatrix.copy(particles.matrixWorld).invert()
particles.emit(amount, emitterSetup)

/* Update the remaining number of particles, and the accumulator. */
acc.current -= amount
remaining.current -= amount
queuedParticles.current -= amount
remainingParticles.current -= amount
}
},
[particlesProp, particlesFromContext, emitterSetup]
Expand Down

0 comments on commit 334f5fd

Please sign in to comment.