physics_ammo_instancing.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js physics - ammo.js instancing</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> physics - ammo.js instancing
  12. </div>
  13. <script src="js/libs/ammo.js"></script>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { AmmoPhysics } from './jsm/physics/AmmoPhysics.js';
  18. import Stats from './jsm/libs/stats.module.js';
  19. var camera, scene, renderer, stats;
  20. var physics, position;
  21. init();
  22. async function init() {
  23. physics = await AmmoPhysics();
  24. position = new THREE.Vector3();
  25. //
  26. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  27. camera.position.set( - 1, 1.5, 2 );
  28. camera.lookAt( 0, 0.5, 0 );
  29. scene = new THREE.Scene();
  30. scene.background = new THREE.Color( 0x666666 );
  31. var light = new THREE.HemisphereLight();
  32. light.intensity = 0.35;
  33. scene.add( light );
  34. var light = new THREE.DirectionalLight();
  35. light.position.set( 5, 5, 5 );
  36. light.castShadow = true;
  37. light.shadow.camera.zoom = 2;
  38. scene.add( light );
  39. var plane = new THREE.Mesh(
  40. new THREE.PlaneBufferGeometry( 5, 5 ),
  41. new THREE.ShadowMaterial( { color: 0x111111 } )
  42. );
  43. plane.rotation.x = - Math.PI / 2;
  44. plane.receiveShadow = true;
  45. scene.add( plane );
  46. physics.addMesh( plane );
  47. var geometry = new THREE.BoxBufferGeometry( 0.1, 0.1, 0.1 );
  48. var material = new THREE.MeshLambertMaterial();
  49. var mesh = new THREE.InstancedMesh( geometry, material, 200 );
  50. mesh.castShadow = true;
  51. mesh.receiveShadow = true;
  52. scene.add( mesh );
  53. var matrix = new THREE.Matrix4();
  54. var color = new THREE.Color();
  55. for ( var i = 0; i < mesh.count; i ++ ) {
  56. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  57. mesh.setMatrixAt( i, matrix );
  58. mesh.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  59. }
  60. physics.addMesh( mesh, 1 );
  61. //
  62. renderer = new THREE.WebGLRenderer( { antialias: true } );
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. renderer.shadowMap.enabled = true;
  66. renderer.outputEncoding = THREE.sRGBEncoding;
  67. document.body.appendChild( renderer.domElement );
  68. stats = new Stats();
  69. document.body.appendChild( stats.dom );
  70. //
  71. var controls = new OrbitControls( camera, renderer.domElement );
  72. controls.target.y = 0.5;
  73. controls.update();
  74. animate();
  75. }
  76. function animate() {
  77. requestAnimationFrame( animate );
  78. var mesh = scene.children[ 3 ];
  79. var index = Math.floor( Math.random() * mesh.count );
  80. position.set( 0, Math.random() * 2, 0 );
  81. physics.setMeshPosition( mesh, position, index );
  82. renderer.render( scene, camera );
  83. stats.update();
  84. }
  85. </script>
  86. </body>
  87. </html>