physics_oimo_instancing.html 4.1 KB

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