123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js physics - OimoPhysics instancing</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">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> physics - OimoPhysics instancing
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import Stats from './jsm/libs/stats.module.js';
- // Or use latest version from NPM `oimophysics`
- import {oimo} from './jsm/libs/OimoPhysics.js';
- const Vec3 = oimo.common.Vec3;
- const World = oimo.dynamics.World;
- const RigidBodyType = oimo.dynamics.rigidbody.RigidBodyType;
- const RigidBodyConfig = oimo.dynamics.rigidbody.RigidBodyConfig;
- const ShapeConfig = oimo.dynamics.rigidbody.ShapeConfig;
- const RigidBody = oimo.dynamics.rigidbody.RigidBody;
- const Shape = oimo.dynamics.rigidbody.Shape;
- const OBoxGeometry = oimo.collision.geometry.BoxGeometry;
- const OSphereGeometry = oimo.collision.geometry.SphereGeometry;
- let camera, scene, renderer, stats;
- let physics, position;
- let world;
- let boxes, spheres;
- let boxesPhys, spheresPhys;
- const dummy = new THREE.Object3D();
- init();
- async function init() {
- world = new World(2, new Vec3(0, -9.8, 0));
- boxesPhys = [];
- spheresPhys = [];
- position = new THREE.Vector3();
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
- camera.position.set( - 1, 1.5, 2 );
- camera.lookAt( 0, 0.5, 0 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x666666 );
- const hemiLight = new THREE.HemisphereLight();
- hemiLight.intensity = 0.35;
- scene.add( hemiLight );
- const dirLight = new THREE.DirectionalLight();
- dirLight.position.set( 5, 5, 5 );
- dirLight.castShadow = true;
- dirLight.shadow.camera.zoom = 2;
- scene.add( dirLight );
- const floor = new THREE.Mesh(
- new THREE.BoxGeometry( 10, 5, 10 ),
- new THREE.ShadowMaterial( { color: 0x111111 } )
- );
- floor.position.y = - 2.5;
- floor.receiveShadow = true;
- floor.userData.physics = addRigidBody(world, vec3FromVector3(floor.position), new OBoxGeometry(new Vec3(10 / 2, 5 / 2, 10 / 2)), true);
- scene.add( floor );
- const material = new THREE.MeshLambertMaterial();
- const matrix = new THREE.Matrix4();
- const color = new THREE.Color();
- // Boxes
- const boxSideSize = 0.1;
- const geometryBox = new THREE.BoxGeometry( boxSideSize, boxSideSize, boxSideSize );
- boxes = new THREE.InstancedMesh( geometryBox, material, 100 );
- boxes.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
- boxes.castShadow = true;
- boxes.receiveShadow = true;
- scene.add( boxes );
- for ( let i = 0; i < boxes.count; i ++ ) {
- position.set(Math.random() - 0.5, Math.random() * 10, Math.random() - 0.5);
- matrix.setPosition( position.x, position.y, position.z );
- boxes.setMatrixAt( i, matrix );
- boxes.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
- // size of the side of the cube should be 2 times smaller
- boxesPhys.push(addRigidBody(world, vec3FromVector3(position), new OBoxGeometry(new Vec3(boxSideSize / 2, boxSideSize / 2, boxSideSize / 2)), false));
- }
- // Spheres
- const geometrySphere = new THREE.IcosahedronGeometry( 0.075, 3 );
- spheres = new THREE.InstancedMesh( geometrySphere, material, 100 );
- spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
- spheres.castShadow = true;
- spheres.receiveShadow = true;
- scene.add( spheres );
- for ( let i = 0; i < spheres.count; i ++ ) {
- position.set(Math.random() - 0.5, Math.random() * 10, Math.random() - 0.5);
- matrix.setPosition( position.x, position.y, position.z );
- spheres.setMatrixAt( i, matrix );
- spheres.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
- spheresPhys.push(addRigidBody(world, vec3FromVector3(position), new OSphereGeometry(0.075), false));
- }
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.shadowMap.enabled = true;
- renderer.outputEncoding = THREE.sRGBEncoding;
- document.body.appendChild( renderer.domElement );
- stats = new Stats();
- document.body.appendChild( stats.dom );
- //
- const controls = new OrbitControls( camera, renderer.domElement );
- controls.target.y = 0.5;
- controls.update();
- window.addEventListener('resize', onWindowResize);
- animate();
- }
- function onWindowResize() {
- camera.aspect = document.body.clientWidth / document.body.clientHeight;
- camera.updateProjectionMatrix();
- renderer.setSize(document.body.clientWidth, document.body.clientHeight);
- }
- function animate() {
- requestAnimationFrame( animate );
- world.step(1 / 60);
- // updating data of visual objects from physical objects
- // BOXES
- boxesPhys.forEach((elPhys, index) => {
- const posVec3 = elPhys.getPosition();
- dummy.position.set(posVec3.x, posVec3.y, posVec3.z);
- dummy.setRotationFromQuaternion(quaternionFromQuat(elPhys.getOrientation()));
- dummy.updateMatrix();
- boxes.setMatrixAt( index, dummy.matrix );
- });
- let index = Math.floor( Math.random() * boxes.count );
- boxesPhys[index].setPosition( new Vec3(0, Math.random() + 1, 0) );
- boxes.instanceMatrix.needsUpdate = true;
- // SPHERES
- spheresPhys.forEach((elPhys, index) => {
- const posVec3 = elPhys.getPosition();
- dummy.position.set(posVec3.x, posVec3.y, posVec3.z);
- dummy.setRotationFromQuaternion(quaternionFromQuat(elPhys.getOrientation()));
- dummy.updateMatrix();
- spheres.setMatrixAt( index, dummy.matrix );
- });
- index = Math.floor( Math.random() * spheres.count );
- spheresPhys[index].setPosition( new Vec3(0, Math.random() + 1, 0) );
- spheres.instanceMatrix.needsUpdate = true;
- renderer.render( scene, camera );
- stats.update();
- }
- // adding a physical object to the world of physics
- function addRigidBody(w, center, geom, wall) {
- const shapeConfig = new ShapeConfig();
- shapeConfig.geometry = geom;
- const bodyConfig = new RigidBodyConfig();
- bodyConfig.type = wall ? RigidBodyType.STATIC : RigidBodyType.DYNAMIC;
- bodyConfig.position = center;
- let body = new RigidBody(bodyConfig);
- body.addShape(new Shape(shapeConfig));
- w.addRigidBody(body);
- return body;
- }
- // convert threejs Vector3 to oimo Vec3
- function vec3FromVector3(position) {
- return new Vec3(...position.toArray());
- }
- // convert oimo Quat to threejs quaternion
- function quaternionFromQuat(quat) {
- return new THREE.Quaternion(quat.x, quat.y, quat.z, quat.w);
- }
- </script>
- </body>
- </html>
|