physics_ammo_instancing.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.wasm.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. let camera, scene, renderer, stats;
  20. let physics, position;
  21. let boxes, spheres;
  22. init();
  23. async function init() {
  24. physics = await AmmoPhysics();
  25. position = new THREE.Vector3();
  26. //
  27. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  28. camera.position.set( - 1, 1.5, 2 );
  29. camera.lookAt( 0, 0.5, 0 );
  30. scene = new THREE.Scene();
  31. scene.background = new THREE.Color( 0x666666 );
  32. const hemiLight = new THREE.HemisphereLight();
  33. hemiLight.intensity = 0.35;
  34. scene.add( hemiLight );
  35. const dirLight = new THREE.DirectionalLight();
  36. dirLight.position.set( 5, 5, 5 );
  37. dirLight.castShadow = true;
  38. dirLight.shadow.camera.zoom = 2;
  39. scene.add( dirLight );
  40. const floor = new THREE.Mesh(
  41. new THREE.BoxGeometry( 10, 5, 10 ),
  42. new THREE.ShadowMaterial( { color: 0x111111 } )
  43. );
  44. floor.position.y = - 2.5;
  45. floor.receiveShadow = true;
  46. scene.add( floor );
  47. physics.addMesh( floor );
  48. //
  49. const material = new THREE.MeshLambertMaterial();
  50. const matrix = new THREE.Matrix4();
  51. const color = new THREE.Color();
  52. // Boxes
  53. const geometryBox = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
  54. boxes = new THREE.InstancedMesh( geometryBox, material, 100 );
  55. boxes.castShadow = true;
  56. boxes.receiveShadow = true;
  57. scene.add( boxes );
  58. for ( let i = 0; i < boxes.count; i ++ ) {
  59. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  60. boxes.setMatrixAt( i, matrix );
  61. boxes.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  62. }
  63. physics.addMesh( boxes, 1 );
  64. // Spheres
  65. const geometrySphere = new THREE.IcosahedronGeometry( 0.075, 3 );
  66. spheres = new THREE.InstancedMesh( geometrySphere, material, 100 );
  67. spheres.castShadow = true;
  68. spheres.receiveShadow = true;
  69. scene.add( spheres );
  70. for ( let i = 0; i < spheres.count; i ++ ) {
  71. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  72. spheres.setMatrixAt( i, matrix );
  73. spheres.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  74. }
  75. physics.addMesh( spheres, 1 );
  76. //
  77. renderer = new THREE.WebGLRenderer( { antialias: true } );
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. renderer.shadowMap.enabled = true;
  81. renderer.outputEncoding = THREE.sRGBEncoding;
  82. document.body.appendChild( renderer.domElement );
  83. stats = new Stats();
  84. document.body.appendChild( stats.dom );
  85. //
  86. const controls = new OrbitControls( camera, renderer.domElement );
  87. controls.target.y = 0.5;
  88. controls.update();
  89. animate();
  90. }
  91. function animate() {
  92. requestAnimationFrame( animate );
  93. //
  94. let index = Math.floor( Math.random() * boxes.count );
  95. position.set( 0, Math.random() + 1, 0 );
  96. physics.setMeshPosition( boxes, position, index );
  97. //
  98. index = Math.floor( Math.random() * spheres.count );
  99. position.set( 0, Math.random() + 1, 0 );
  100. physics.setMeshPosition( spheres, position, index );
  101. renderer.render( scene, camera );
  102. stats.update();
  103. }
  104. </script>
  105. </body>
  106. </html>