webgl_instancing_raycast.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing - raycast</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <script type="module">
  11. import * as THREE from '../build/three.module.js';
  12. import Stats from './jsm/libs/stats.module.js';
  13. import { GUI } from './jsm/libs/dat.gui.module.js';
  14. import { OrbitControls } from "./jsm/controls/OrbitControls.js";
  15. var camera, scene, renderer, stats;
  16. var mesh;
  17. var amount = parseInt( window.location.search.substr( 1 ) ) || 10;
  18. var count = Math.pow( amount, 3 );
  19. var raycaster = new THREE.Raycaster();
  20. var mouse = new THREE.Vector2( 1, 1 );
  21. var rotationMatrix = new THREE.Matrix4().makeRotationY( 0.1 );
  22. var instanceMatrix = new THREE.Matrix4();
  23. var matrix = new THREE.Matrix4();
  24. init();
  25. animate();
  26. function init() {
  27. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  28. camera.position.set( amount, amount, amount );
  29. camera.lookAt( 0, 0, 0 );
  30. scene = new THREE.Scene();
  31. var light = new THREE.HemisphereLight( 0xffffff, 0x000088 );
  32. light.position.set( - 1, 1.5, 1 );
  33. scene.add( light );
  34. var light = new THREE.HemisphereLight( 0xffffff, 0x880000, 0.5 );
  35. light.position.set( - 1, - 1.5, - 1 );
  36. scene.add( light );
  37. var geometry = new THREE.SphereBufferGeometry( 0.5 );
  38. var material = new THREE.MeshPhongMaterial( { flatShading: true } );
  39. mesh = new THREE.InstancedMesh( geometry, material, count );
  40. var i = 0;
  41. var offset = ( amount - 1 ) / 2;
  42. var transform = new THREE.Object3D();
  43. for ( var x = 0; x < amount; x ++ ) {
  44. for ( var y = 0; y < amount; y ++ ) {
  45. for ( var z = 0; z < amount; z ++ ) {
  46. transform.position.set( offset - x, offset - y, offset - z );
  47. transform.updateMatrix();
  48. mesh.setMatrixAt( i ++, transform.matrix );
  49. }
  50. }
  51. }
  52. scene.add( mesh );
  53. //
  54. var gui = new GUI();
  55. gui.add( mesh, 'count', 0, count );
  56. renderer = new THREE.WebGLRenderer( { antialias: true } );
  57. renderer.setPixelRatio( window.devicePixelRatio );
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. document.body.appendChild( renderer.domElement );
  60. new OrbitControls( camera, renderer.domElement );
  61. stats = new Stats();
  62. document.body.appendChild( stats.dom );
  63. window.addEventListener( 'resize', onWindowResize, false );
  64. document.addEventListener( 'mousemove', onMouseMove, false );
  65. }
  66. function onWindowResize() {
  67. camera.aspect = window.innerWidth / window.innerHeight;
  68. camera.updateProjectionMatrix();
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. }
  71. function onMouseMove( event ) {
  72. event.preventDefault();
  73. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  74. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  75. }
  76. function animate() {
  77. requestAnimationFrame( animate );
  78. render();
  79. }
  80. function render() {
  81. raycaster.setFromCamera( mouse, camera );
  82. var intersection = raycaster.intersectObject( mesh );
  83. if ( intersection.length > 0 ) {
  84. var instanceId = intersection[ 0 ].instanceId;
  85. mesh.getMatrixAt( instanceId, instanceMatrix );
  86. matrix.multiplyMatrices( instanceMatrix, rotationMatrix );
  87. mesh.setMatrixAt( instanceId, matrix );
  88. mesh.instanceMatrix.needsUpdate = true;
  89. }
  90. renderer.render( scene, camera );
  91. stats.update();
  92. }
  93. </script>
  94. </body>
  95. </html>