The Wayback Machine - https://webcf.waybackmachine.org/web/20220312142843/https://github.com/mrdoob/three.js/commit/a9f9539ec6c1f6a848f99828c9e3677154d69236
Skip to content
Permalink
Browse files
Examples: Improved physics_ammo_instancing.
  • Loading branch information
mrdoob committed on Aug 23, 2020
1 parent 8258225 commit a9f9539ec6c1f6a848f99828c9e3677154d69236
Showing with 55 additions and 23 deletions.
  1. +3 −4 examples/jsm/physics/AmmoPhysics.js
  2. +52 −19 examples/physics_ammo_instancing.html
  3. BIN examples/screenshots/physics_ammo_instancing.jpg
@@ -39,12 +39,11 @@ async function AmmoPhysics() {

return shape;

} else if ( geometry.type === 'PlaneBufferGeometry' ) {
} else if ( geometry.type === 'SphereBufferGeometry' || geometry.type === 'IcosahedronBufferGeometry' ) {

const sx = parameters.width !== undefined ? parameters.width / 2 : 0.5;
const sy = parameters.height !== undefined ? parameters.height / 2 : 0.5;
const radius = parameters.radius !== undefined ? parameters.radius : 1;

const shape = new AmmoLib.btBoxShape( new AmmoLib.btVector3( sx, sy, 0.01 ) );
const shape = new AmmoLib.btSphereShape( radius );
shape.setMargin( 0.05 );

return shape;
@@ -24,6 +24,8 @@
var camera, scene, renderer, stats;
var physics, position;

var boxes, spheres;

init();

async function init() {
@@ -50,34 +52,57 @@
light.shadow.camera.zoom = 2;
scene.add( light );

var plane = new THREE.Mesh(
new THREE.PlaneBufferGeometry( 5, 5 ),
var floor = new THREE.Mesh(
new THREE.BoxBufferGeometry( 10, 5, 10 ),
new THREE.ShadowMaterial( { color: 0x111111 } )
);
plane.rotation.x = - Math.PI / 2;
plane.receiveShadow = true;
scene.add( plane );
physics.addMesh( plane );
floor.position.y = - 2.5;
floor.receiveShadow = true;
scene.add( floor );
physics.addMesh( floor );

//

var geometry = new THREE.BoxBufferGeometry( 0.1, 0.1, 0.1 );
var material = new THREE.MeshLambertMaterial();
var mesh = new THREE.InstancedMesh( geometry, material, 200 );
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add( mesh );

var matrix = new THREE.Matrix4();
var color = new THREE.Color();

for ( var i = 0; i < mesh.count; i ++ ) {
// Boxes

var geometry = new THREE.BoxBufferGeometry( 0.1, 0.1, 0.1 );
boxes = new THREE.InstancedMesh( geometry, material, 100 );
boxes.castShadow = true;
boxes.receiveShadow = true;
scene.add( boxes );

for ( var i = 0; i < boxes.count; i ++ ) {

matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
mesh.setMatrixAt( i, matrix );
mesh.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
boxes.setMatrixAt( i, matrix );
boxes.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );

}

physics.addMesh( mesh, 1 );
physics.addMesh( boxes, 1 );

// Spheres

var geometry = new THREE.IcosahedronBufferGeometry( 0.075, 2 );
spheres = new THREE.InstancedMesh( geometry, material, 100 );
spheres.castShadow = true;
spheres.receiveShadow = true;
scene.add( spheres );

for ( var i = 0; i < spheres.count; i ++ ) {

matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
spheres.setMatrixAt( i, matrix );
spheres.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );

}

physics.addMesh( spheres, 1 );

//

@@ -105,11 +130,19 @@

requestAnimationFrame( animate );

var mesh = scene.children[ 3 ];
var index = Math.floor( Math.random() * mesh.count );
//

var index = Math.floor( Math.random() * boxes.count );

position.set( 0, Math.random() + 1, 0 );
physics.setMeshPosition( boxes, position, index );

//

var index = Math.floor( Math.random() * spheres.count );

position.set( 0, Math.random() * 2, 0 );
physics.setMeshPosition( mesh, position, index );
position.set( 0, Math.random() + 1, 0 );
physics.setMeshPosition( spheres, position, index );

renderer.render( scene, camera );

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a9f9539

Please sign in to comment.