-
-
Notifications
You must be signed in to change notification settings - Fork 35.8k
Added additional Rapier physics examples #30818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
13b018e
Added new Rapier physics examples
NikLever fce0d1d
Delete attribute when using Rapier debugger
NikLever 3e37098
Added screenshots
NikLever d4840bd
Restored jsDocs to RapierPhysics.js
NikLever d2a1d32
Updated files.json
NikLever 89f4533
added exception
NikLever 10e8d38
Updates for Michael
NikLever 36707c0
Refactored debugger as RapierHelper
NikLever 683fcc7
Removed unused imports from RapierPhysics.js
NikLever a9817e5
Update RapierPhysics.js
Mugen87 f354a25
Update RapierPhysics.js
Mugen87 a5fbe86
Refactored physics_rapier_joints.html to not use getBody.
NikLever a1d7592
Merge branch 'RapierExamples2' of https://github.com/NikLever/three.j…
NikLever e051c98
Removed getBody from RapierPhysics.js
NikLever dbedc77
Update RapierPhysics.js
Mugen87 c9d2cdc
Update RapierPhysics.js
Mugen87 900969c
Refactored access to the RigidBody
NikLever 576e7fa
Merge branch 'RapierExamples2' of https://github.com/NikLever/three.j…
NikLever 6b31a83
Update physics_rapier_vehicle_controller.html
Mugen87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { LineSegments, LineBasicMaterial, BufferAttribute } from 'three'; | ||
/** | ||
* This class displays all Rapier Colliders in outline. | ||
* | ||
* @augments LineSegments | ||
*/ | ||
class RapierHelper extends LineSegments { | ||
|
||
/** | ||
* Constructs a new Rapier debug helper. | ||
* | ||
* @param {RAPIER.world} world - The Rapier world to visualize. | ||
*/ | ||
constructor( world ) { | ||
|
||
super(); | ||
|
||
/** | ||
* The Rapier world to visualize. | ||
* | ||
* @type {RAPIER.world} | ||
*/ | ||
this.world = world; | ||
|
||
this.material = new LineBasicMaterial( { vertexColors: true } ); | ||
this.frustumCulled = false; | ||
|
||
} | ||
|
||
/** | ||
* Call this in the render loop to update the outlines. | ||
*/ | ||
update() { | ||
|
||
const { vertices, colors } = this.world.debugRender(); | ||
|
||
this.geometry.deleteAttribute( 'position' ); | ||
this.geometry.deleteAttribute( 'color' ); | ||
|
||
this.geometry.setAttribute( 'position', new BufferAttribute( vertices, 3 ) ); | ||
this.geometry.setAttribute( 'color', new BufferAttribute( colors, 4 ) ); | ||
|
||
} | ||
|
||
/** | ||
* Frees the GPU-related resources allocated by this instance. Call this | ||
* method whenever this instance is no longer used in your app. | ||
*/ | ||
dispose() { | ||
|
||
this.geometry.dispose(); | ||
this.material.dispose(); | ||
|
||
} | ||
|
||
} | ||
|
||
export { RapierHelper }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
import { Clock, Vector3, Quaternion, Matrix4 } from 'three'; | ||
|
||
const RAPIER_PATH = 'https://cdn.skypack.dev/@dimforge/[email protected]'; | ||
|
@@ -28,6 +29,20 @@ function getShape( geometry ) { | |
const radius = parameters.radius !== undefined ? parameters.radius : 1; | ||
return RAPIER.ColliderDesc.ball( radius ); | ||
|
||
} else if ( geometry.type === 'CylinderGeometry') { | ||
|
||
const radius = parameters.radiusBottom !== undefined ? parameters.radiusBottom : 0.5; | ||
const length = parameters.length !== undefined ? parameters.length : 0.5; | ||
|
||
return RAPIER.ColliderDesc.cylinder( length / 2, radius); | ||
|
||
} else if ( geometry.type === 'CapsuleGeometry') { | ||
|
||
const radius = parameters.radius !== undefined ? parameters.radius : 0.5; | ||
const length = parameters.length !== undefined ? parameters.length : 0.5; | ||
|
||
return RAPIER.ColliderDesc.capsule( length / 2, radius); | ||
|
||
} else if ( geometry.type === 'BufferGeometry' ) { | ||
|
||
const vertices = []; | ||
|
@@ -108,6 +123,10 @@ async function RapierPhysics() { | |
|
||
} | ||
|
||
function getBody( mesh ){ | ||
return meshMap.get( mesh ); | ||
} | ||
|
||
function addMesh( mesh, mass = 0, restitution = 0 ) { | ||
|
||
const shape = getShape( mesh.geometry ); | ||
|
@@ -121,12 +140,14 @@ async function RapierPhysics() { | |
? createInstancedBody( mesh, mass, shape ) | ||
: createBody( mesh.position, mesh.quaternion, mass, shape ); | ||
|
||
if ( mass > 0 ) { | ||
//if ( mass > 0 ) { | ||
|
||
meshes.push( mesh ); | ||
meshMap.set( mesh, body ); | ||
|
||
} | ||
//} | ||
|
||
return body; | ||
Mugen87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} | ||
|
||
|
@@ -242,6 +263,18 @@ async function RapierPhysics() { | |
setInterval( step, 1000 / frameRate ); | ||
|
||
return { | ||
RAPIER, | ||
world, | ||
/** | ||
* Returns the Rapier RigidBody given a ThreeJS mesh that has been added via | ||
* addScene or addBody | ||
* | ||
* | ||
* @method | ||
* @name RapierPhysics#getBody | ||
* @param {Object3D} mesh A ThreeJS mesh that is included in the physics. | ||
*/ | ||
getBody, | ||
/** | ||
* Adds the given scene to this physics simulation. Only meshes with a | ||
* `physics` object in their {@link Object3D#userData} field will be honored. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>three.js physics - rapier3d basic</title> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> | ||
<link type="text/css" rel="stylesheet" href="main.css"> | ||
<style> | ||
body { | ||
color: #333; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<div id="info"> | ||
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> physics - <a href="https://github.com/dimforge/rapier.js" target="_blank">rapier</a> basic | ||
</div> | ||
|
||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"three": "../build/three.module.js", | ||
"three/addons/": "./jsm/" | ||
} | ||
} | ||
</script> | ||
|
||
<script type="module"> | ||
|
||
import * as THREE from 'three'; | ||
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; | ||
import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js'; | ||
import { RapierHelper } from 'three/addons/helpers/RapierHelper.js'; | ||
import Stats from 'three/addons/libs/stats.module.js'; | ||
|
||
let camera, scene, renderer, stats; | ||
let physics, physicsHelper; | ||
|
||
init(); | ||
|
||
async function init() { | ||
|
||
scene = new THREE.Scene(); | ||
scene.background = new THREE.Color( 0xbfd1e5 ); | ||
|
||
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 ); | ||
camera.position.set( 0, 3, 10 ); | ||
|
||
const ambient = new THREE.HemisphereLight( 0x555555, 0xFFFFFF ); | ||
|
||
scene.add( ambient ); | ||
|
||
const light = new THREE.DirectionalLight( 0xffffff, 4 ); | ||
|
||
light.position.set( 0, 12.5, 12.5 ); | ||
light.castShadow = true; | ||
light.shadow.radius = 3; | ||
light.shadow.blurSamples = 8; | ||
light.shadow.mapSize.width = 1024; | ||
light.shadow.mapSize.height = 1024; | ||
|
||
const size = 10; | ||
light.shadow.camera.left = - size; | ||
light.shadow.camera.bottom = - size; | ||
light.shadow.camera.right = size; | ||
light.shadow.camera.top = size; | ||
light.shadow.camera.near = 1; | ||
light.shadow.camera.far = 50; | ||
|
||
scene.add( light ); | ||
|
||
renderer = new THREE.WebGLRenderer( { antialias: true } ); | ||
renderer.setPixelRatio( window.devicePixelRatio ); | ||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
Mugen87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
renderer.shadowMap.enabled = true; | ||
document.body.appendChild( renderer.domElement ); | ||
renderer.setAnimationLoop( animate ); | ||
|
||
const controls = new OrbitControls( camera, renderer.domElement ); | ||
controls.target = new THREE.Vector3( 0, 2, 0 ); | ||
controls.update(); | ||
|
||
const geometry = new THREE.BoxGeometry( 10, 0.5, 10 ); | ||
const material = new THREE.MeshStandardMaterial( { color: 0xFFFFFF } ); | ||
|
||
const floor = new THREE.Mesh( geometry, material ); | ||
floor.receiveShadow = true; | ||
|
||
floor.position.y = - 0.25; | ||
floor.userData.physics = { mass: 0 }; | ||
|
||
scene.add( floor ); | ||
|
||
new THREE.TextureLoader().load( 'textures/grid.png', function ( texture ) { | ||
|
||
texture.wrapS = THREE.RepeatWrapping; | ||
texture.wrapT = THREE.RepeatWrapping; | ||
texture.repeat.set( 20, 20 ); | ||
floor.material.map = texture; | ||
floor.material.needsUpdate = true; | ||
|
||
} ); | ||
|
||
stats = new Stats(); | ||
document.body.appendChild( stats.dom ); | ||
|
||
initPhysics(); | ||
|
||
onWindowResize(); | ||
|
||
window.addEventListener( 'resize', onWindowResize, false ); | ||
|
||
} | ||
|
||
async function initPhysics() { | ||
|
||
//Initialize physics engine using the script in the jsm/physics folder | ||
physics = await RapierPhysics(); | ||
|
||
physics.addScene( scene ); | ||
|
||
addBody( ); | ||
|
||
//Optionally display collider outlines | ||
physicsHelper = new RapierHelper( physics.world ); | ||
scene.add( physicsHelper ); | ||
|
||
setInterval( addBody, 1000 ); | ||
|
||
} | ||
|
||
function addBody( ) { | ||
|
||
const geometry = ( Math.random() > 0.5 ) ? new THREE.SphereGeometry( 0.5 ) : new THREE.BoxGeometry( 1, 1, 1 ); | ||
const material = new THREE.MeshStandardMaterial( { color: Math.floor( Math.random() * 0xFFFFFF ) } ); | ||
|
||
const mesh = new THREE.Mesh( geometry, material ); | ||
mesh.castShadow = true; | ||
|
||
mesh.position.set( Math.random() * 2 - 1, Math.random() * 3 + 6, Math.random() * 2 - 1 ); | ||
|
||
scene.add( mesh ); | ||
|
||
//parameter 2 - mass, parameter 3 - restitution ( how bouncy ) | ||
physics.addMesh( mesh, 1, 0.5 ); | ||
|
||
} | ||
|
||
function onWindowResize( ) { | ||
|
||
camera.aspect = window.innerWidth / window.innerHeight; | ||
camera.updateProjectionMatrix(); | ||
|
||
renderer.setSize( window.innerWidth, window.innerHeight ); | ||
|
||
} | ||
|
||
|
||
function animate() { | ||
|
||
if ( physicsHelper ) physicsHelper.update(); | ||
|
||
renderer.render( scene, camera ); | ||
|
||
stats.update(); | ||
|
||
} | ||
|
||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.