physics_ammo_instancing.html 4.3 KB

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