physics_ammo_instancing.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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="jsm/libs/ammo.wasm.js"></script>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { AmmoPhysics } from 'three/addons/physics/AmmoPhysics.js';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. let camera, scene, renderer, stats;
  31. let physics, position;
  32. let boxes, spheres;
  33. init();
  34. async function init() {
  35. physics = await AmmoPhysics();
  36. position = new THREE.Vector3();
  37. //
  38. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  39. camera.position.set( - 1, 1.5, 2 );
  40. camera.lookAt( 0, 0.5, 0 );
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0x666666 );
  43. const hemiLight = new THREE.HemisphereLight();
  44. hemiLight.intensity = 0.3;
  45. scene.add( hemiLight );
  46. const dirLight = new THREE.DirectionalLight();
  47. dirLight.position.set( 5, 5, 5 );
  48. dirLight.castShadow = true;
  49. dirLight.shadow.camera.zoom = 2;
  50. scene.add( dirLight );
  51. const floor = new THREE.Mesh(
  52. new THREE.BoxGeometry( 10, 5, 10 ),
  53. new THREE.ShadowMaterial( { color: 0x444444 } )
  54. );
  55. floor.position.y = - 2.5;
  56. floor.receiveShadow = true;
  57. scene.add( floor );
  58. physics.addMesh( floor );
  59. //
  60. const material = new THREE.MeshLambertMaterial();
  61. const matrix = new THREE.Matrix4();
  62. const color = new THREE.Color();
  63. // Boxes
  64. const geometryBox = new THREE.BoxGeometry( 0.075, 0.075, 0.075 );
  65. boxes = new THREE.InstancedMesh( geometryBox, material, 400 );
  66. boxes.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  67. boxes.castShadow = true;
  68. boxes.receiveShadow = true;
  69. scene.add( boxes );
  70. for ( let i = 0; i < boxes.count; i ++ ) {
  71. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  72. boxes.setMatrixAt( i, matrix );
  73. boxes.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  74. }
  75. physics.addMesh( boxes, 1 );
  76. // Spheres
  77. const geometrySphere = new THREE.IcosahedronGeometry( 0.05, 4 );
  78. spheres = new THREE.InstancedMesh( geometrySphere, material, 400 );
  79. spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  80. spheres.castShadow = true;
  81. spheres.receiveShadow = true;
  82. scene.add( spheres );
  83. for ( let i = 0; i < spheres.count; i ++ ) {
  84. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  85. spheres.setMatrixAt( i, matrix );
  86. spheres.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  87. }
  88. physics.addMesh( spheres, 1 );
  89. //
  90. renderer = new THREE.WebGLRenderer( { antialias: true } );
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. renderer.shadowMap.enabled = true;
  94. document.body.appendChild( renderer.domElement );
  95. stats = new Stats();
  96. document.body.appendChild( stats.dom );
  97. //
  98. const controls = new OrbitControls( camera, renderer.domElement );
  99. controls.target.y = 0.5;
  100. controls.update();
  101. animate();
  102. setInterval( () => {
  103. let index = Math.floor( Math.random() * boxes.count );
  104. position.set( 0, Math.random() + 1, 0 );
  105. physics.setMeshPosition( boxes, position, index );
  106. //
  107. index = Math.floor( Math.random() * spheres.count );
  108. position.set( 0, Math.random() + 1, 0 );
  109. physics.setMeshPosition( spheres, position, index );
  110. }, 1000 / 60 );
  111. }
  112. function animate() {
  113. requestAnimationFrame( animate );
  114. renderer.render( scene, camera );
  115. stats.update();
  116. }
  117. </script>
  118. </body>
  119. </html>