physics_ammo_instancing.html 4.8 KB

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