physics_ammo_instancing.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  56. boxes.castShadow = true;
  57. boxes.receiveShadow = true;
  58. scene.add( boxes );
  59. for ( let i = 0; i < boxes.count; i ++ ) {
  60. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  61. boxes.setMatrixAt( i, matrix );
  62. boxes.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  63. }
  64. physics.addMesh( boxes, 1 );
  65. // Spheres
  66. const geometrySphere = new THREE.IcosahedronGeometry( 0.075, 3 );
  67. spheres = new THREE.InstancedMesh( geometrySphere, material, 100 );
  68. spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  69. spheres.castShadow = true;
  70. spheres.receiveShadow = true;
  71. scene.add( spheres );
  72. for ( let i = 0; i < spheres.count; i ++ ) {
  73. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  74. spheres.setMatrixAt( i, matrix );
  75. spheres.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  76. }
  77. physics.addMesh( spheres, 1 );
  78. //
  79. renderer = new THREE.WebGLRenderer( { antialias: true } );
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. renderer.shadowMap.enabled = true;
  83. renderer.outputEncoding = THREE.sRGBEncoding;
  84. document.body.appendChild( renderer.domElement );
  85. stats = new Stats();
  86. document.body.appendChild( stats.dom );
  87. //
  88. const controls = new OrbitControls( camera, renderer.domElement );
  89. controls.target.y = 0.5;
  90. controls.update();
  91. animate();
  92. }
  93. function animate() {
  94. requestAnimationFrame( animate );
  95. //
  96. let index = Math.floor( Math.random() * boxes.count );
  97. position.set( 0, Math.random() + 1, 0 );
  98. physics.setMeshPosition( boxes, position, index );
  99. //
  100. index = Math.floor( Math.random() * spheres.count );
  101. position.set( 0, Math.random() + 1, 0 );
  102. physics.setMeshPosition( spheres, position, index );
  103. renderer.render( scene, camera );
  104. stats.update();
  105. }
  106. </script>
  107. </body>
  108. </html>