Something went wrong. Try again.
Starship Simulator Controls thoriumsim.com
Something went wrong. Try again.
1.5 kB · 70 lines
TSX
at develop
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import { useFrame, type ThreeElements } from "@react-three/fiber";import { useRef } from "react";import type { Group } from "three";
import type { CellProps } from "./types";import { useParticles } from "./useParticles";
export function SpriteCell({ birthRatePerSecond = 1, birthRateVariance = 0, initialCount = 0, lifeInSeconds = 1, lifeVariance = 0, speed = 1, speedVariance = 0, colorOverLife, scale = 1, scaleVariance = 0, scalePercentOverLife, angle = {}, angleVariance = {}, spin = {}, spinVariance = {}, attachToEmitter = false, rng = Math.random, ...spriteMaterialProps}: CellProps & ThreeElements["spriteMaterial"]) { const { maxParticles, updateParticle } = useParticles({ birthRatePerSecond, birthRateVariance, initialCount, lifeInSeconds, lifeVariance, speed, speedVariance, colorOverLife, scale, scaleVariance, scalePercentOverLife, angle, angleVariance, spin, spinVariance, attachToEmitter, rng, }); const ref = useRef<Group>(null); useFrame((state, delta) => { if (!ref.current) return;
for (let i = 0; i < maxParticles; i++) { const child = ref.current.children[i]; if (!child) continue; if ("material" in child) { // @ts-expect-error updateParticle(i, child, child.material.color, delta); } } });
return ( <group ref={ref}> {Array.from({ length: maxParticles }).map((_, i) => ( <sprite key={i}> <spriteMaterial attach="material" {...spriteMaterialProps} /> </sprite> ))} </group> );}