physics_oimo_instancing.html 4.4 KB

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