physics_rapier_instancing.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. <meta property="og:title" content="three.js physics - rapier3d instancing">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/physics_rapier_instancing.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/physics_rapier_instancing.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> physics - <a href="https://github.com/dimforge/rapier.js" target="_blank">rapier</a> instancing<br />
  16. <button id="shake" style="margin-top: 25px;">SHAKE</button>
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js';
  30. import Stats from 'three/addons/libs/stats.module.js';
  31. let camera, scene, renderer, stats;
  32. let physics, position;
  33. let boxes, spheres;
  34. init();
  35. async function init() {
  36. physics = await RapierPhysics();
  37. position = new THREE.Vector3();
  38. //
  39. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  40. camera.position.set( - 1, 1.5, 2 );
  41. camera.lookAt( 0, 0.5, 0 );
  42. scene = new THREE.Scene();
  43. scene.background = new THREE.Color( 0x666666 );
  44. const hemiLight = new THREE.HemisphereLight();
  45. scene.add( hemiLight );
  46. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  47. dirLight.position.set( 5, 5, 5 );
  48. dirLight.castShadow = true;
  49. dirLight.shadow.camera.zoom = 2;
  50. scene.add( dirLight );
  51. const shadowPlane = new THREE.Mesh(
  52. new THREE.PlaneGeometry( 10, 10 ),
  53. new THREE.ShadowMaterial( {
  54. color: 0x444444
  55. } ),
  56. );
  57. shadowPlane.rotation.x = - Math.PI / 2;
  58. shadowPlane.receiveShadow = true;
  59. scene.add( shadowPlane );
  60. const floorCollider = new THREE.Mesh(
  61. new THREE.BoxGeometry( 10, 5, 10 ),
  62. new THREE.MeshBasicMaterial( { color: 0x666666 } )
  63. );
  64. floorCollider.position.y = - 2.5;
  65. floorCollider.userData.physics = { mass: 0 };
  66. floorCollider.visible = false;
  67. scene.add( floorCollider );
  68. //
  69. const material = new THREE.MeshLambertMaterial();
  70. const matrix = new THREE.Matrix4();
  71. const color = new THREE.Color();
  72. // Boxes
  73. const geometryBox = new THREE.BoxGeometry( 0.075, 0.075, 0.075 );
  74. boxes = new THREE.InstancedMesh( geometryBox, material, 400 );
  75. boxes.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  76. boxes.castShadow = true;
  77. boxes.receiveShadow = true;
  78. boxes.userData.physics = { mass: 1 };
  79. scene.add( boxes );
  80. for ( let i = 0; i < boxes.count; i ++ ) {
  81. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  82. boxes.setMatrixAt( i, matrix );
  83. boxes.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  84. }
  85. // Spheres
  86. const geometrySphere = new THREE.IcosahedronGeometry( 0.05, 4 );
  87. spheres = new THREE.InstancedMesh( geometrySphere, material, 400 );
  88. spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  89. spheres.castShadow = true;
  90. spheres.receiveShadow = true;
  91. spheres.userData.physics = { mass: 1 };
  92. scene.add( spheres );
  93. for ( let i = 0; i < spheres.count; i ++ ) {
  94. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  95. spheres.setMatrixAt( i, matrix );
  96. spheres.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  97. }
  98. physics.addScene( scene );
  99. //
  100. renderer = new THREE.WebGLRenderer( { antialias: true } );
  101. renderer.setPixelRatio( window.devicePixelRatio );
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. renderer.setAnimationLoop( animate );
  104. renderer.shadowMap.enabled = true;
  105. document.body.appendChild( renderer.domElement );
  106. stats = new Stats();
  107. document.body.appendChild( stats.dom );
  108. //
  109. const controls = new OrbitControls( camera, renderer.domElement );
  110. controls.target.y = 0.5;
  111. controls.update();
  112. document.querySelector( 'button#shake' ).addEventListener( 'click', () => {
  113. const impulse = new THREE.Vector3();
  114. for ( let i = 0; i < spheres.count; i ++ ) {
  115. impulse.set( ( Math.random() - 0.5 ) * 5, Math.random() * 5, ( Math.random() - 0.5 ) * 5 );
  116. physics.applyImpulse( spheres, impulse, i );
  117. }
  118. for ( let i = 0; i < boxes.count; i ++ ) {
  119. impulse.set( ( Math.random() - 0.5 ) * 5, Math.random() * 5, ( Math.random() - 0.5 ) * 5 );
  120. physics.applyImpulse( boxes, impulse, i );
  121. }
  122. } );
  123. setInterval( () => {
  124. let index = Math.floor( Math.random() * boxes.count );
  125. position.set( 0, Math.random() + 1, 0 );
  126. physics.setMeshPosition( boxes, position, index );
  127. //
  128. index = Math.floor( Math.random() * spheres.count );
  129. position.set( 0, Math.random() + 1, 0 );
  130. physics.setMeshPosition( spheres, position, index );
  131. }, 1000 / 60 );
  132. window.addEventListener( 'resize', onWindowResize );
  133. }
  134. function onWindowResize() {
  135. camera.aspect = window.innerWidth / window.innerHeight;
  136. camera.updateProjectionMatrix();
  137. renderer.setSize( window.innerWidth, window.innerHeight );
  138. }
  139. function animate() {
  140. renderer.render( scene, camera );
  141. stats.update();
  142. }
  143. </script>
  144. </body>
  145. </html>
粤ICP备19079148号