physics_cannon_instancing.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js physics - cannon.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 - cannon.js 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 { CannonPhysics } from './jsm/physics/CannonPhysics.js';
  17. import Stats from './jsm/libs/stats.module.js';
  18. var camera, scene, renderer, stats;
  19. var physics, position;
  20. init();
  21. animate();
  22. function init() {
  23. physics = new CannonPhysics();
  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. var light = new THREE.HemisphereLight();
  32. light.intensity = 0.35;
  33. scene.add( light );
  34. var light = new THREE.DirectionalLight();
  35. light.position.set( 5, 5, 5 );
  36. light.castShadow = true;
  37. light.shadow.camera.zoom = 2;
  38. scene.add( light );
  39. var plane = new THREE.Mesh(
  40. new THREE.PlaneBufferGeometry( 5, 5 ),
  41. new THREE.ShadowMaterial( { color: 0x111111 } )
  42. );
  43. plane.rotation.x = - Math.PI / 2;
  44. plane.receiveShadow = true;
  45. scene.add( plane );
  46. physics.addMesh( plane );
  47. var geometry = new THREE.BoxBufferGeometry( 0.1, 0.1, 0.1 );
  48. var material = new THREE.MeshLambertMaterial();
  49. var mesh = new THREE.InstancedMesh( geometry, material, 200 );
  50. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); // will be updated every frame
  51. mesh.castShadow = true;
  52. mesh.receiveShadow = true;
  53. scene.add( mesh );
  54. var matrix = new THREE.Matrix4();
  55. var color = new THREE.Color();
  56. for ( var i = 0; i < mesh.count; i ++ ) {
  57. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  58. mesh.setMatrixAt( i, matrix );
  59. mesh.setColorAt( i, color.setHex( 0xffffff * Math.random() ) );
  60. }
  61. physics.addMesh( mesh, 1 );
  62. //
  63. renderer = new THREE.WebGLRenderer( { antialias: true } );
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. renderer.shadowMap.enabled = true;
  67. renderer.outputEncoding = THREE.sRGBEncoding;
  68. document.body.appendChild( renderer.domElement );
  69. stats = new Stats();
  70. document.body.appendChild( stats.dom );
  71. //
  72. var controls = new OrbitControls( camera, renderer.domElement );
  73. controls.target.y = 0.5;
  74. controls.update();
  75. }
  76. function animate() {
  77. requestAnimationFrame( animate );
  78. var mesh = scene.children[ 3 ];
  79. var index = Math.floor( Math.random() * mesh.count );
  80. position.set( 0, Math.random() * 2, 0 );
  81. physics.setMeshPosition( mesh, position, index );
  82. renderer.render( scene, camera );
  83. stats.update();
  84. }
  85. </script>
  86. </body>
  87. </html>