This project aims to provide a base setup for working with shaders and particles in React Three Fiber (R3F). The goal is to have a starting point for shader-based particle systems, allowing easy modifications without starting from scratch every time.
- Features
- Quick Start
- Usage
- Particle Distributions (Examples)
- How to Modify
- Troubleshooting
- Contributing
- Custom Shader Material using
shaderMaterial
from @react-three/drei - Particle Distribution Modes:
- Surface: Particles are distributed on the surface of a sphere.
- Random: Particles are randomly distributed inside a sphere.
- Uniform: Particles are uniformly distributed inside a sphere.
- Animation Support: Particles can animate using time-based shader effects.
- Leva Controls: Easily tweak parameters like particle size, orbit rotation, and performance settings.
- Performance Monitor: Integrated with r3f-perf to track FPS and WebGL stats.
-
Clone the repository:
git clone https://github.com/0Shree005/r3fParticleSystem.git && cd r3fParticleSystem
-
Install dependencies and run the project:
npm install && npm run dev
-
Open your browser and navigate to
http://localhost:8080
. -
To access the development server from another device on your network, visit
http://<Host's-Local-IP>:8080
.
This project uses Leva to provide UI controls:
pSize
: Controls the size of each particle.autoRotate
: Enables or disables auto-rotation.autoRotateSpeed
: Adjusts rotation speed whenautoRotate
is enabled.perfVisible
: Toggles the performance monitor visibility.
Particles.jsx
→ Main component that handles particle generation, controls, and shaders.shaders/vertex.glsl
→ Custom vertex shader for particle animation.shaders/fragment.glsl
→ Custom fragment shader for particle rendering.
Inside Particles.jsx
, modify the radius
calculation:
// Surface Distribution
const radius = sphereRadius;
// Random Distribution
// const radius = sphereRadius * Math.random();
// Uniform Distribution
// const radius = sphereRadius * Math.cbrt(Math.random());
You can modify how the particles move:
modelPosition.x += sin(uTime + aRandom * 70.0) * aScale;
modelPosition.y += sin(uTime + aRandom * 90.0) * aScale;
modelPosition.z += cos(uTime + aRandom * 30.0) * aScale;
Modify the shape and color of particles:
vec3 mixedColor = mix(vec3(0.0), vec3(gl_PointCoord, 1.0), strength);
- Shader Compilation Errors: Ensure your GLSL code is valid and free of syntax errors.
- Performance Issues: Reduce the number of particles or optimize shader code.
- Leva Controls Not Working: Ensure
useControls
is properly imported and used.
Contributions are welcome! If you have ideas, improvements, or bug fixes, feel free to open an issue or submit a pull request.
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeatureName
). - Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/YourFeatureName
). - Open a pull request.