All Projects → schteppe → Gpu Physics.js

schteppe / Gpu Physics.js

GPGPU physics for Three.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Gpu Physics.js

Expo Voxel
🎮🌳 Voxel Terrain made in React Native. ∛
Stars: ✭ 169 (-31.3%)
Mutual labels:  webgl, threejs
Three Nebula
WebGL based particle system engine for three.js
Stars: ✭ 192 (-21.95%)
Mutual labels:  webgl, threejs
Encom Boardroom
📈 Web tribute to the Tron: Legacy Boardroom Scene
Stars: ✭ 2,094 (+751.22%)
Mutual labels:  webgl, threejs
Threejs Sandbox
Set of experiments and extensions to THREE.js.
Stars: ✭ 163 (-33.74%)
Mutual labels:  webgl, threejs
Magicshader
🔮 Tiny helper for three.js to debug and write shaders
Stars: ✭ 205 (-16.67%)
Mutual labels:  webgl, threejs
Threejs Path Flow
🐬🐟 ↶Mesh Deformation / Bending / Following on a Curve
Stars: ✭ 165 (-32.93%)
Mutual labels:  webgl, threejs
Aframe Effects
A VR Ready Post processing framework for Three.js and/or A-Frame
Stars: ✭ 176 (-28.46%)
Mutual labels:  webgl, threejs
Texture Compressor
CLI tool for texture compression using ASTC, ETC, PVRTC and S3TC in a KTX container.
Stars: ✭ 156 (-36.59%)
Mutual labels:  webgl, threejs
Demos
One repo to rule them all.
Stars: ✭ 204 (-17.07%)
Mutual labels:  webgl, threejs
Droneworld
droneWorld: a 3D world map and a three.js playground
Stars: ✭ 197 (-19.92%)
Mutual labels:  webgl, threejs
Vue Displacement Slideshow
A Vue.js 2.0 slideshow component working with Three.js
Stars: ✭ 165 (-32.93%)
Mutual labels:  webgl, threejs
Pathfinding Visualizer Threejs
A visualizer for pathfinding algorithms in 3D with maze generation, first-person view and device camera input.
Stars: ✭ 209 (-15.04%)
Mutual labels:  webgl, threejs
Patches
Patches is a visual programming editor for building WebVR and WebGL experiences.
Stars: ✭ 164 (-33.33%)
Mutual labels:  webgl, threejs
React Globe
Create beautiful and interactive React + ThreeJS globe visualizations with ease.
Stars: ✭ 167 (-32.11%)
Mutual labels:  webgl, threejs
Stickyimageeffect
A sticky image effect for a slideshow inspired by ultanoir's website.
Stars: ✭ 158 (-35.77%)
Mutual labels:  webgl, threejs
Threejs Modern App
Boilerplate and utils for a fullscreen Three.js app
Stars: ✭ 176 (-28.46%)
Mutual labels:  webgl, threejs
Interactivelandscape
An exploration of an animated interactive landscape built with three.js.
Stars: ✭ 150 (-39.02%)
Mutual labels:  webgl, threejs
Wechart
Create all the [ch]arts by cax or three.js - Cax 和 three.js 创造一切图[表]
Stars: ✭ 152 (-38.21%)
Mutual labels:  webgl, threejs
React Three Next
React Three Fiber, Nextjs, Tailwind starter
Stars: ✭ 195 (-20.73%)
Mutual labels:  webgl, threejs
Forgejs
ForgeJS is a javascript framework that unleashes immersive WebVR experiences.
Stars: ✭ 207 (-15.85%)
Mutual labels:  webgl, threejs

GPGPU rigid body physics for Three.js

Launch demo or watch the video. NOTE: Works only on desktops with good GPUs.

Demo

Full examples

Demos

Usage

Include gp.js into your Three.js project HTML:

<script src="gp.js"></script>

Sample code below. See the examples/ directory for full examples.

// Create a simulation world
var world = new gp.World({
    renderer: threejsRenderer,            // Must be a THREE.WebGLRenderer
    maxBodies: 128 * 128,                 // Max number of bodies
    maxParticles: 128 * 128,              // Max number of particles (each body consists of a number of particles)
    radius: 0.05,                         // Size of a particle in the simulation
    stiffness: 100,                       // Contact stiffness
    damping: 0.4,                         // Contact damping
    fixedTimeStep: 1/60,                  // Simulation timestep
    boxSize: new THREE.Vector3(10,10,10), // World collision bounds

    // The "grid" is a box where collisions can occur. Specify its position and resolution.
    // The size of the grid box is gridResolution * radius * 2
    gridPosition: new THREE.Vector3(0,0,0),
    gridResolution: new THREE.Vector3(128,16,128),

    gravity: new THREE.Vector3(0,-1,0),
    friction: 0.4,
    drag: 0.3,
});

// Create a body
//                         position   rotation   mass  inertia
var bodyId = world.addBody(0, 0, 0,   0,0,0,1,   1,    0.1,0.1,0.1);
world.addParticle(bodyId, 0,0,0); // Add a particle in the center of the body

// Get the UV coordinate for the body
var uv = world.getBodyUV(bodyId);
myCustomShaderMaterial.uniforms.bodyUV.value = uv;

// A simple render loop can look like this:
var prevTime;
function render(time) {
    requestAnimationFrame(render);
    
    // Calculate time since last frame
    var deltaTime = prevTime ? (time - prevTime) / 1000 : 0;
    prevTime = time;
    
    // Update physics    
    world.step( deltaTime );

    // Use textures from the world, they contain positions and rotations of all bodies.
    // Note: you need to fetch these textures every frame from the World, since they are swapped by the World every step.
    myCustomShaderMaterial.uniforms.bodyPositionTex.value = world.bodyPositionTexture;
    myCustomShaderMaterial.uniforms.bodyQuaternionTex.value = world.bodyQuaternionTexture;

    // Render scene
    renderer.render( scene, camera );
}
requestAnimationFrame(render);

Implementation

The demo is largely based on GPU Gems 3 ch. 29, Real-Time Rigid Body Simulation on GPUs. It heavily relies on the THREE.WebGLRenderTarget class and custom shaders.

The simulation loop is in short:

  1. Create float render targets of size N*N for bodies: position, quaternion, velocity, angular velocity, force, torque.
  2. Create float render targets of size M*M for particles: local position, world position, relative position, force.
  3. Create float render target of size 4*M*M for a broadphase grid.
  4. While running:
    1. Calculate particle properties: world position, body-relative position, velocity.
    2. Set up "broadphase render target". Stencil buffer is set up for stencil routing (see this presentation, slide 24) by clearing once (to set stencil values to zero) and drawing point clouds thrice to set values 1, 2 and 3 into the stencil buffer. An alternative is using PBOs to set these values, but it doesn't seem to be available in WebGL1.
    3. Particles are drawn to the "broadphase render target" using GL_POINTS with point-size 2. This maps them into the correct "grid bucket" and writes the particle ID's there. The stencil routing guarantees four particle ID's can be drawn into the same grid bucket in this single draw call.
    4. Particle forces are calculated using spring-and-dashpot model equations. Neighboring particles are easily looked up in the broadphase render target.
    5. Forces are added to the bodies' force render target using GL_POINTS with additive blending. Other forces such as gravity is added here too.
    6. Torque is added to bodies' torque render target in the same way.
    7. Body velocities are updated: velocity += deltaTime * force / inertia.
    8. Body positions are updated: position += deltaTime * velocity.
    9. Render each body by looking up body position and quaternion in the correct render target texture.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].