physics_cannon_instancing.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, 2 );
  28. camera.lookAt( 0, 0, 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. /*
  48. function getSize() {
  49. return Math.random() * 0.1 + 0.05;
  50. }
  51. */
  52. var geometry = new THREE.BoxBufferGeometry( 0.1, 0.1, 0.1 );
  53. var material = new THREE.MeshLambertMaterial( /*{ vertexColors: true }*/ );
  54. var mesh = new THREE.InstancedMesh( geometry, material, 200 );
  55. mesh.castShadow = true;
  56. mesh.receiveShadow = true;
  57. scene.add( mesh );
  58. var matrix = new THREE.Matrix4();
  59. for ( var i = 0; i < mesh.count; i ++ ) {
  60. matrix.setPosition( Math.random() - 0.5, Math.random() * 2, Math.random() - 0.5 );
  61. mesh.setMatrixAt( i, matrix );
  62. }
  63. /*
  64. var instanceColors = [];
  65. for ( var i = 0; i < mesh.count; i ++ ) {
  66. instanceColors.push( Math.random(), Math.random(), Math.random() );
  67. }
  68. mesh.geometry.setAttribute( 'instanceColor', new THREE.InstancedBufferAttribute( new Float32Array( instanceColors ), 3 ) );
  69. */
  70. physics.addMesh( mesh, 1 );
  71. //
  72. renderer = new THREE.WebGLRenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. renderer.shadowMap.enabled = true;
  76. renderer.outputEncoding = THREE.sRGBEncoding;
  77. document.body.appendChild( renderer.domElement );
  78. stats = new Stats();
  79. document.body.appendChild( stats.dom );
  80. //
  81. new OrbitControls( camera, renderer.domElement );
  82. }
  83. function animate() {
  84. requestAnimationFrame( animate );
  85. var mesh = scene.children[ 3 ];
  86. var index = Math.floor( Math.random() * mesh.count );
  87. position.set( 0, Math.random() * 2, 0 );
  88. physics.setMeshPosition( mesh, position, index );
  89. renderer.render( scene, camera );
  90. stats.update();
  91. }
  92. </script>
  93. </body>
  94. </html>