Skip to content

Commit

Permalink
batchRenderer.update only if particles on screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Steedie committed Oct 28, 2024
1 parent a4a8886 commit e065660
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 31 deletions.
34 changes: 15 additions & 19 deletions src/examples/spaceshooter/effects/FXExplodeQuarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
useEffect,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react';
import { Object3D, Vector3 } from 'three';
Expand All @@ -20,39 +19,36 @@ export interface ExplodeFXHandle {
export const ExplodeFX = forwardRef<ExplodeFXHandle>((_props, ref) => {
const batchRenderer = useMemo(() => new BatchedRenderer(), []);
const [effect, setEffect] = useState<Object3D>();
const [activeExplosions, setActiveExplosions] = useState(0);
const scene = useThree((state) => state.scene);

useImperativeHandle(ref, () => ({
triggerExplosion(pos: Vector3) {
if (!effect) {
return;
}
if (!effect) {return;}

effect.position.copy(pos);
QuarksUtil.restart(effect);

setActiveExplosions((count) => count + 1);

setTimeout(() => {
setActiveExplosions((count) => Math.max(0, count - 1));
}, 3500); // match or greater than particle effect duration

addShake({
intensity: 300, // Adjust as needed
intensity: 300,
frequency: 40,
position: pos,
decay: 700, // Rate at which the shake reduces
duration: 1, // How long the shake lasts
decay: 700,
duration: 1,
});
},
}));

const clockRef = useRef(0);
useFrame((_state, delta) => {
console.time('notBeenLong');
const oldClock = clockRef.current;
clockRef.current = performance.now();
const notBeenLong = clockRef.current - oldClock < 100;
console.timeEnd('notBeenLong');
if (notBeenLong) {
return;
if (activeExplosions > 0) {
batchRenderer.update(delta);
}

console.time('explode');
batchRenderer.update(delta);
console.timeEnd('explode');
});

useEffect(() => {
Expand Down
12 changes: 8 additions & 4 deletions src/examples/spaceshooter/effects/FXRespawnQuarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,25 @@ export interface SpawnFXHandle {
export const SpawnFX = forwardRef<SpawnFXHandle>((_props, ref) => {
const batchRenderer = useMemo(() => new BatchedRenderer(), []);
const [effect, setEffect] = useState<Object3D>();
const [activeSpawns, setActiveSpawns] = useState(0);
const scene = useThree((state) => state.scene);

useImperativeHandle(ref, () => ({
triggerSpawn(pos: Vector3, parent: Object3D) {
if (!effect) {
return;
}
if (!effect) {return;}
parent.add(effect);
effect.position.copy(pos);
QuarksUtil.restart(effect);

setActiveSpawns((count) => count + 1);
setTimeout(() => setActiveSpawns((count) => Math.max(0, count - 1)), 2000);
},
}));

useFrame((_state, delta) => {
batchRenderer.update(delta);
if (activeSpawns > 0) {
batchRenderer.update(delta);
}
});

useEffect(() => {
Expand Down
12 changes: 8 additions & 4 deletions src/examples/spaceshooter/effects/FXShipImpactQuarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ export interface ShipImpactFXHandle {
export const ShipImpactFX = forwardRef<ShipImpactFXHandle>((_props, ref) => {
const batchRenderer = useMemo(() => new BatchedRenderer(), []);
const [effect, setEffect] = useState<Object3D>();
const [activeImpacts, setActiveImpacts] = useState(0);
const scene = useThree((state) => state.scene);

useImperativeHandle(ref, () => ({
triggerShipImpact(pos: Vector3) {
if (!effect) {
return;
}
if (!effect) {return;}
effect.position.copy(pos);
QuarksUtil.restart(effect);

setActiveImpacts((count) => count + 1);
setTimeout(() => setActiveImpacts((count) => Math.max(0, count - 1)), 500);
},
}));

useFrame((_state, delta) => {
batchRenderer.update(delta);
if (activeImpacts > 0) {
batchRenderer.update(delta);
}
});

useEffect(() => {
Expand Down
12 changes: 8 additions & 4 deletions src/examples/spaceshooter/effects/FXSparksQuarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ export interface SparksFXHandle {
export const SparksFX = forwardRef<SparksFXHandle>((_props, ref) => {
const batchRenderer = useMemo(() => new BatchedRenderer(), []);
const [effect, setEffect] = useState<Object3D>();
const [activeSparks, setActiveSparks] = useState(0);
const scene = useThree((state) => state.scene);

useImperativeHandle(ref, () => ({
triggerSparks(pos: Vector3) {
if (!effect) {
return;
}
if (!effect) {return;}
effect.position.copy(pos);
QuarksUtil.restart(effect);

setActiveSparks((count) => count + 1);
setTimeout(() => setActiveSparks((count) => Math.max(0, count - 1)), 500);
},
}));

useFrame((_state, delta) => {
batchRenderer.update(delta);
if (activeSparks > 0) {
batchRenderer.update(delta);
}
});

useEffect(() => {
Expand Down

0 comments on commit e065660

Please sign in to comment.