physics_ammo_instancing.html 4.6 KB

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