physics_jolt_instancing.html 4.7 KB

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