physics_rapier_joints.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js physics - rapier3d joints</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> joints
  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 Stats from 'three/addons/libs/stats.module.js';
  32. let camera, scene, renderer, stats;
  33. let physics, pivot, physicsHelper;
  34. init();
  35. async function init() {
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0xbfd1e5 );
  38. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  39. camera.position.set( 0, 3, 10 );
  40. const ambient = new THREE.HemisphereLight( 0x555555, 0xFFFFFF );
  41. scene.add( ambient );
  42. const light = new THREE.DirectionalLight( 0xffffff, 4 );
  43. light.position.set( 0, 12.5, 12.5 );
  44. light.castShadow = true;
  45. light.shadow.radius = 3;
  46. light.shadow.blurSamples = 8;
  47. light.shadow.mapSize.width = 1024;
  48. light.shadow.mapSize.height = 1024;
  49. const size = 10;
  50. light.shadow.camera.left = - size;
  51. light.shadow.camera.bottom = - size;
  52. light.shadow.camera.right = size;
  53. light.shadow.camera.top = size;
  54. light.shadow.camera.near = 1;
  55. light.shadow.camera.far = 50;
  56. scene.add( light );
  57. renderer = new THREE.WebGLRenderer( { antialias: true } );
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.shadowMap.enabled = true;
  61. document.body.appendChild( renderer.domElement );
  62. renderer.setAnimationLoop( animate );
  63. const controls = new OrbitControls( camera, renderer.domElement );
  64. controls.target = new THREE.Vector3( 0, 2, 0 );
  65. controls.update();
  66. stats = new Stats();
  67. document.body.appendChild( stats.dom );
  68. //Create pivot point
  69. const geometry = new THREE.SphereGeometry( 0.5 );
  70. const material = new THREE.MeshStandardMaterial( { color: 0xFF0000 } );
  71. pivot = new THREE.Mesh( geometry, material );
  72. pivot.position.y = 6;
  73. pivot.userData.physics = { mass: 0 };
  74. scene.add( pivot );
  75. initPhysics();
  76. onWindowResize();
  77. window.addEventListener( 'resize', onWindowResize, false );
  78. }
  79. async function initPhysics() {
  80. //Initialize physics engine using the script in the jsm/physics folder
  81. physics = await RapierPhysics();
  82. physics.addScene( scene );
  83. //Optionally display collider outlines
  84. physicsHelper = new RapierHelper( physics.world );
  85. scene.add( physicsHelper );
  86. const link1 = addLink( pivot, 0 );
  87. const link2 = addLink( link1, 2 );
  88. addLink( link2, 4 );
  89. }
  90. //link - the mesh that the new link will be attached to
  91. //x - used to position the new link
  92. function addLink( link, x ) {
  93. const geometry = new THREE.CapsuleGeometry( 0.25, 1.8 );
  94. const material = new THREE.MeshStandardMaterial( { color: 0xCCCC00 } );
  95. const mesh = new THREE.Mesh( geometry, material );
  96. mesh.rotateZ( Math.PI * 0.5 );
  97. mesh.position.set( x + 0.9, 5.8, 0 );
  98. scene.add( mesh );
  99. physics.addMesh( mesh, 1, 0.5 );
  100. const jointParams = physics.RAPIER.JointData.spherical(
  101. ( link == pivot ) ? new physics.RAPIER.Vector3( 0, - 0.5, 0 ) : new physics.RAPIER.Vector3( 0, - 1.15, 0 ), // Joint position in world space
  102. new physics.RAPIER.Vector3( 0, 1.15, 0 ) // Corresponding attachment on sphere
  103. );
  104. const body1 = link.userData.physics.body;
  105. const body2 = mesh.userData.physics.body;
  106. body2.setAngularDamping( 10.0 );
  107. physics.world.createImpulseJoint( jointParams, body1, body2, true );
  108. return mesh;
  109. }
  110. function onWindowResize( ) {
  111. camera.aspect = window.innerWidth / window.innerHeight;
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. }
  115. function animate() {
  116. if ( physicsHelper ) physicsHelper.update();
  117. renderer.render( scene, camera );
  118. stats.update();
  119. }
  120. </script>
  121. </body>
  122. </html>
粤ICP备19079148号