1
0

physics_jolt_instancing.html 4.3 KB

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