physics_rapier_instancing.html 4.8 KB

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