physics_rapier_instancing.html 4.4 KB

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