physics_rapier_basic.html 5.2 KB

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