physics_rapier_basic.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js physics - rapier3d basic</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. <style>
  9. body {
  10. color: #333;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="info">
  16. <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> basic
  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 { RapierHelper } from 'three/addons/helpers/RapierHelper.js';
  31. import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
  32. import Stats from 'three/addons/libs/stats.module.js';
  33. let camera, scene, renderer, stats, controls;
  34. let physics, physicsHelper;
  35. init();
  36. async function init() {
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0xbfd1e5 );
  39. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  40. camera.position.set( 0, 3, 10 );
  41. const ambient = new THREE.HemisphereLight( 0x555555, 0xFFFFFF );
  42. scene.add( ambient );
  43. const light = new THREE.DirectionalLight( 0xffffff, 4 );
  44. light.position.set( 0, 12.5, 12.5 );
  45. light.castShadow = true;
  46. light.shadow.radius = 3;
  47. light.shadow.blurSamples = 8;
  48. light.shadow.mapSize.width = 1024;
  49. light.shadow.mapSize.height = 1024;
  50. const size = 10;
  51. light.shadow.camera.left = - size;
  52. light.shadow.camera.bottom = - size;
  53. light.shadow.camera.right = size;
  54. light.shadow.camera.top = size;
  55. light.shadow.camera.near = 1;
  56. light.shadow.camera.far = 50;
  57. scene.add( light );
  58. renderer = new THREE.WebGLRenderer( { antialias: true } );
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. renderer.shadowMap.enabled = true;
  62. document.body.appendChild( renderer.domElement );
  63. renderer.setAnimationLoop( animate );
  64. controls = new OrbitControls( camera, renderer.domElement );
  65. controls.enableDamping = true;
  66. controls.target = new THREE.Vector3( 0, 2, 0 );
  67. controls.update();
  68. const geometry = new THREE.BoxGeometry( 10, 0.5, 10 );
  69. const material = new THREE.MeshStandardMaterial( { color: 0xFFFFFF } );
  70. const floor = new THREE.Mesh( geometry, material );
  71. floor.receiveShadow = true;
  72. floor.position.y = - 0.25;
  73. floor.userData.physics = { mass: 0 };
  74. scene.add( floor );
  75. new THREE.TextureLoader().load( 'textures/grid.png', function ( texture ) {
  76. texture.wrapS = THREE.RepeatWrapping;
  77. texture.wrapT = THREE.RepeatWrapping;
  78. texture.repeat.set( 20, 20 );
  79. floor.material.map = texture;
  80. floor.material.needsUpdate = true;
  81. } );
  82. stats = new Stats();
  83. document.body.appendChild( stats.dom );
  84. initPhysics();
  85. onWindowResize();
  86. window.addEventListener( 'resize', onWindowResize, false );
  87. }
  88. async function initPhysics() {
  89. //Initialize physics engine using the script in the jsm/physics folder
  90. physics = await RapierPhysics();
  91. physics.addScene( scene );
  92. addBody( );
  93. //Optionally display collider outlines
  94. physicsHelper = new RapierHelper( physics.world );
  95. scene.add( physicsHelper );
  96. setInterval( addBody, 1000 );
  97. }
  98. const geometries = [
  99. new THREE.BoxGeometry( 1, 1, 1 ),
  100. new THREE.SphereGeometry( 0.5 ),
  101. new RoundedBoxGeometry( 1, 1, 1, 2, 0.25 )
  102. ];
  103. function addBody( ) {
  104. const geometry = geometries[ Math.floor( Math.random() * geometries.length ) ];
  105. const material = new THREE.MeshStandardMaterial( { color: Math.floor( Math.random() * 0xFFFFFF ) } );
  106. const mesh = new THREE.Mesh( geometry, material );
  107. mesh.castShadow = true;
  108. mesh.position.set( Math.random() * 2 - 1, Math.random() * 3 + 6, Math.random() * 2 - 1 );
  109. scene.add( mesh );
  110. //parameter 2 - mass, parameter 3 - restitution ( how bouncy )
  111. physics.addMesh( mesh, 1, 0.5 );
  112. }
  113. function onWindowResize( ) {
  114. camera.aspect = window.innerWidth / window.innerHeight;
  115. camera.updateProjectionMatrix();
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. }
  118. function animate() {
  119. for ( const object of scene.children ) {
  120. if ( object.isMesh ) {
  121. if ( object.position.y < - 10 ) {
  122. scene.remove( object );
  123. physics.removeMesh( object );
  124. }
  125. }
  126. }
  127. if ( physicsHelper ) physicsHelper.update();
  128. controls.update();
  129. renderer.render( scene, camera );
  130. stats.update();
  131. }
  132. </script>
  133. </body>
  134. </html>
粤ICP备19079148号