123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js physics - cannon.js 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 - cannon.js instancing
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { CannonPhysics } from './jsm/physics/CannonPhysics.js';
- import Stats from './jsm/libs/stats.module.js';
- var camera, scene, renderer, stats;
- var physics, position;
- init();
- animate();
- function init() {
- physics = new CannonPhysics();
- position = new THREE.Vector3();
- //
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
- camera.position.set( - 1, 1, 2 );
- camera.lookAt( 0, 0, 0 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x666666 );
- var light = new THREE.HemisphereLight();
- light.intensity = 0.35;
- scene.add( light );
- var light = new THREE.DirectionalLight();
- light.position.set( 5, 5, 5 );
- light.castShadow = true;
- light.shadow.camera.zoom = 2;
- scene.add( light );
- var plane = new THREE.Mesh(
- new THREE.PlaneBufferGeometry( 5, 5 ),
- new THREE.ShadowMaterial( { color: 0x111111 } )
- );
- plane.rotation.x = - Math.PI / 2;
- plane.receiveShadow = true;
- scene.add( plane );
- physics.addMesh( plane );
- /*
- function getSize() {
- return Math.random() * 0.1 + 0.05;
- }
- */
- var geometry = new THREE.BoxBufferGeometry( 0.1, 0.1, 0.1 );
- var material = new THREE.MeshLambertMaterial( /*{ vertexColors: true }*/ );
- var mesh = new THREE.InstancedMesh( geometry, material, 200 );
- mesh.castShadow = true;
- mesh.receiveShadow = true;
- scene.add( mesh );
- var matrix = new THREE.Matrix4();
- for ( var i = 0; i < mesh.count; i ++ ) {
- matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
- mesh.setMatrixAt( i, matrix );
- }
- /*
- var instanceColors = [];
- for ( var i = 0; i < mesh.count; i ++ ) {
- instanceColors.push( Math.random(), Math.random(), Math.random() );
- }
- mesh.geometry.setAttribute( 'instanceColor', new THREE.InstancedBufferAttribute( new Float32Array( instanceColors ), 3 ) );
- */
- physics.addMesh( mesh, 1 );
- //
- 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 );
- //
- new OrbitControls( camera, renderer.domElement );
- }
- function animate() {
- requestAnimationFrame( animate );
- var mesh = scene.children[ 3 ];
- var index = Math.floor( Math.random() * mesh.count );
- position.set( 0, Math.random() * 2, 0 );
- physics.setMeshPosition( mesh, position, index );
- renderer.render( scene, camera );
- stats.update();
- }
- </script>
- </body>
- </html>
|