physics_oimo_instancing.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  26. import { OimoPhysics } from './jsm/physics/OimoPhysics.js';
  27. import Stats from './jsm/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 OimoPhysics();
  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. hemiLight.intensity = 0.35;
  43. scene.add( hemiLight );
  44. const dirLight = new THREE.DirectionalLight();
  45. dirLight.position.set( 5, 5, 5 );
  46. dirLight.castShadow = true;
  47. dirLight.shadow.camera.zoom = 2;
  48. scene.add( dirLight );
  49. const floor = new THREE.Mesh(
  50. new THREE.BoxGeometry( 10, 5, 10 ),
  51. new THREE.ShadowMaterial( { color: 0x111111 } )
  52. );
  53. floor.position.y = - 2.5;
  54. floor.receiveShadow = true;
  55. scene.add( floor );
  56. physics.addMesh( floor );
  57. //
  58. const material = new THREE.MeshLambertMaterial();
  59. const matrix = new THREE.Matrix4();
  60. const color = new THREE.Color();
  61. // Boxes
  62. const geometryBox = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
  63. boxes = new THREE.InstancedMesh( geometryBox, material, 100 );
  64. boxes.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  65. boxes.castShadow = true;
  66. boxes.receiveShadow = true;
  67. scene.add( boxes );
  68. for ( let i = 0; i < boxes.count; i ++ ) {
  69. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  70. boxes.setMatrixAt( i, matrix );
  71. boxes.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  72. }
  73. physics.addMesh( boxes, 1 );
  74. // Spheres
  75. const geometrySphere = new THREE.IcosahedronGeometry( 0.075, 3 );
  76. spheres = new THREE.InstancedMesh( geometrySphere, material, 100 );
  77. spheres.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  78. spheres.castShadow = true;
  79. spheres.receiveShadow = true;
  80. scene.add( spheres );
  81. for ( let i = 0; i < spheres.count; i ++ ) {
  82. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  83. spheres.setMatrixAt( i, matrix );
  84. spheres.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  85. }
  86. physics.addMesh( spheres, 1 );
  87. //
  88. renderer = new THREE.WebGLRenderer( { antialias: true } );
  89. renderer.setPixelRatio( window.devicePixelRatio );
  90. renderer.setSize( window.innerWidth, window.innerHeight );
  91. renderer.shadowMap.enabled = true;
  92. renderer.outputEncoding = THREE.sRGBEncoding;
  93. document.body.appendChild( renderer.domElement );
  94. stats = new Stats();
  95. document.body.appendChild( stats.dom );
  96. //
  97. const controls = new OrbitControls( camera, renderer.domElement );
  98. controls.target.y = 0.5;
  99. controls.update();
  100. animate();
  101. }
  102. function animate() {
  103. requestAnimationFrame( animate );
  104. //
  105. let index = Math.floor( Math.random() * boxes.count );
  106. position.set( 0, Math.random() + 1, 0 );
  107. physics.setMeshPosition( boxes, position, index );
  108. //
  109. index = Math.floor( Math.random() * spheres.count );
  110. position.set( 0, Math.random() + 1, 0 );
  111. physics.setMeshPosition( spheres, position, index );
  112. renderer.render( scene, camera );
  113. stats.update();
  114. }
  115. </script>
  116. </body>
  117. </html>