webgl_instancing_raycast.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, geometry;
  17. var amount = parseInt( window.location.search.substr( 1 ) ) || 3;
  18. var count = Math.pow( amount, 3 );
  19. var object = new THREE.Object3D();
  20. var intersection;
  21. var raycaster = new THREE.Raycaster();
  22. var mouse = new THREE.Vector2( 1, 1 );
  23. var orbitControls;
  24. var rotationTheta = 0.1;
  25. var rotationMatrix = new THREE.Matrix4().makeRotationY( rotationTheta );
  26. var instanceMatrix = new THREE.Matrix4();
  27. var matrix = new THREE.Matrix4();
  28. init();
  29. animate();
  30. function init() {
  31. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 );
  32. camera.position.set( amount, amount, amount );
  33. camera.lookAt( 0, 0, 0 );
  34. scene = new THREE.Scene();
  35. geometry = new THREE.TorusKnotBufferGeometry( 0.5, 0.2, 16, 4, 2, 3 );
  36. geometry.scale( 0.5, 0.5, 0.5 );
  37. var material = new THREE.MeshNormalMaterial( { flatShading: true } );
  38. mesh = new THREE.InstancedMesh( geometry, material, count );
  39. var i = 0;
  40. var offset = amount / 2;
  41. for ( var x = 0; x < amount; x ++ ) {
  42. for ( var y = 0; y < amount; y ++ ) {
  43. for ( var z = 0; z < amount; z ++ ) {
  44. object.position.set( offset - x, offset - y, offset - z );
  45. object.updateMatrix();
  46. mesh.setMatrixAt( i ++, object.matrix );
  47. }
  48. }
  49. }
  50. scene.add( mesh );
  51. var gui = new GUI();
  52. gui.add( mesh, 'count', 0, count );
  53. renderer = new THREE.WebGLRenderer( { antialias: true } );
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. document.body.appendChild( renderer.domElement );
  57. orbitControls = new OrbitControls( camera, renderer.domElement );
  58. stats = new Stats();
  59. document.body.appendChild( stats.dom );
  60. window.addEventListener( 'resize', onWindowResize, false );
  61. document.addEventListener( 'mousemove', onMouseMove, false );
  62. }
  63. function onWindowResize() {
  64. camera.aspect = window.innerWidth / window.innerHeight;
  65. camera.updateProjectionMatrix();
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. }
  68. function onMouseMove( event ) {
  69. event.preventDefault();
  70. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  71. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  72. }
  73. function animate() {
  74. requestAnimationFrame( animate );
  75. render();
  76. }
  77. function render() {
  78. raycaster.setFromCamera( mouse, camera );
  79. intersection = raycaster.intersectObjects( scene.children );
  80. if ( intersection.length > 0 ) {
  81. mesh.getMatrixAt( intersection[ 0 ].instanceId, instanceMatrix );
  82. matrix.multiplyMatrices( instanceMatrix, rotationMatrix );
  83. mesh.setMatrixAt( intersection[ 0 ].instanceId, matrix );
  84. mesh.instanceMatrix.needsUpdate = true;
  85. }
  86. renderer.render( scene, camera );
  87. stats.update();
  88. }
  89. </script>
  90. </body>
  91. </html>