physics_jolt_instancing.html 5.0 KB

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