2
0

physics_cannon_instancing.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 src="./js/libs/cannon.js"></script>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { CannonPhysics } from './jsm/physics/CannonPhysics.js';
  18. import Stats from './jsm/libs/stats.module.js';
  19. var camera, scene, renderer, stats;
  20. var physics, position;
  21. init();
  22. animate();
  23. function init() {
  24. physics = new CannonPhysics();
  25. position = new THREE.Vector3();
  26. //
  27. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
  28. camera.position.set( - 1, 1, 2 );
  29. camera.lookAt( 0, 0, 0 );
  30. scene = new THREE.Scene();
  31. scene.background = new THREE.Color( 0x666666 );
  32. var light = new THREE.HemisphereLight();
  33. light.intensity = 0.35;
  34. scene.add( light );
  35. var light = new THREE.DirectionalLight();
  36. light.position.set( 5, 5, 5 );
  37. light.castShadow = true;
  38. light.shadow.camera.zoom = 2;
  39. scene.add( light );
  40. var plane = new THREE.Mesh(
  41. new THREE.PlaneBufferGeometry( 5, 5 ),
  42. new THREE.ShadowMaterial( { color: 0x111111 } )
  43. );
  44. plane.rotation.x = - Math.PI / 2;
  45. plane.receiveShadow = true;
  46. scene.add( plane );
  47. physics.addMesh( plane );
  48. /*
  49. function getSize() {
  50. return Math.random() * 0.1 + 0.05;
  51. }
  52. */
  53. var geometry = new THREE.BoxBufferGeometry( 0.1, 0.1, 0.1 );
  54. var material = new THREE.MeshLambertMaterial( /*{ vertexColors: true }*/ );
  55. var mesh = new THREE.InstancedMesh( geometry, material, 200 );
  56. mesh.castShadow = true;
  57. mesh.receiveShadow = true;
  58. scene.add( mesh );
  59. var matrix = new THREE.Matrix4();
  60. for ( var i = 0; i < mesh.count; i ++ ) {
  61. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  62. mesh.setMatrixAt( i, matrix );
  63. }
  64. /*
  65. var instanceColors = [];
  66. for ( var i = 0; i < mesh.count; i ++ ) {
  67. instanceColors.push( Math.random(), Math.random(), Math.random() );
  68. }
  69. mesh.geometry.setAttribute( 'instanceColor', new THREE.InstancedBufferAttribute( new Float32Array( instanceColors ), 3 ) );
  70. */
  71. physics.addMesh( mesh, 1 );
  72. //
  73. renderer = new THREE.WebGLRenderer( { antialias: true } );
  74. renderer.setPixelRatio( window.devicePixelRatio );
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. renderer.shadowMap.enabled = true;
  77. renderer.outputEncoding = THREE.sRGBEncoding;
  78. document.body.appendChild( renderer.domElement );
  79. stats = new Stats();
  80. document.body.appendChild( stats.dom );
  81. //
  82. new OrbitControls( camera, renderer.domElement );
  83. }
  84. function animate() {
  85. requestAnimationFrame( animate );
  86. var mesh = scene.children[ 3 ];
  87. var index = Math.floor( Math.random() * mesh.count );
  88. position.set( 0, Math.random() * 2, 0 );
  89. physics.setMeshPosition( mesh, position, index );
  90. renderer.render( scene, camera );
  91. stats.update();
  92. }
  93. </script>
  94. </body>
  95. </html>