webgl_instancing_raycast.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.module.js",
  14. "three/addons/": "./jsm/"
  15. }
  16. }
  17. </script>
  18. <script type="module">
  19. import * as THREE from 'three';
  20. import Stats from 'three/addons/libs/stats.module.js';
  21. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  22. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  23. let camera, scene, renderer, controls, stats;
  24. let mesh;
  25. const amount = parseInt( window.location.search.slice( 1 ) ) || 10;
  26. const count = Math.pow( amount, 3 );
  27. const raycaster = new THREE.Raycaster();
  28. const mouse = new THREE.Vector2( 1, 1 );
  29. const color = new THREE.Color();
  30. const white = new THREE.Color().setHex( 0xffffff );
  31. init();
  32. function init() {
  33. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  34. camera.position.set( amount, amount, amount );
  35. camera.lookAt( 0, 0, 0 );
  36. scene = new THREE.Scene();
  37. const light = new THREE.HemisphereLight( 0xffffff, 0x888888, 3 );
  38. light.position.set( 0, 1, 0 );
  39. scene.add( light );
  40. const geometry = new THREE.IcosahedronGeometry( 0.5, 3 );
  41. const material = new THREE.MeshPhongMaterial( { color: 0xffffff } );
  42. mesh = new THREE.InstancedMesh( geometry, material, count );
  43. let i = 0;
  44. const offset = ( amount - 1 ) / 2;
  45. const matrix = new THREE.Matrix4();
  46. for ( let x = 0; x < amount; x ++ ) {
  47. for ( let y = 0; y < amount; y ++ ) {
  48. for ( let z = 0; z < amount; z ++ ) {
  49. matrix.setPosition( offset - x, offset - y, offset - z );
  50. mesh.setMatrixAt( i, matrix );
  51. mesh.setColorAt( i, color );
  52. i ++;
  53. }
  54. }
  55. }
  56. scene.add( mesh );
  57. //
  58. const gui = new GUI();
  59. gui.add( mesh, 'count', 0, count );
  60. renderer = new THREE.WebGLRenderer( { antialias: true } );
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.setAnimationLoop( animate );
  64. document.body.appendChild( renderer.domElement );
  65. controls = new OrbitControls( camera, renderer.domElement );
  66. controls.enableDamping = true;
  67. controls.enableZoom = false;
  68. controls.enablePan = false;
  69. stats = new Stats();
  70. document.body.appendChild( stats.dom );
  71. window.addEventListener( 'resize', onWindowResize );
  72. document.addEventListener( 'mousemove', onMouseMove );
  73. }
  74. function onWindowResize() {
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. }
  79. function onMouseMove( event ) {
  80. event.preventDefault();
  81. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  82. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  83. }
  84. function animate() {
  85. controls.update();
  86. raycaster.setFromCamera( mouse, camera );
  87. const intersection = raycaster.intersectObject( mesh );
  88. if ( intersection.length > 0 ) {
  89. const instanceId = intersection[ 0 ].instanceId;
  90. mesh.getColorAt( instanceId, color );
  91. if ( color.equals( white ) ) {
  92. mesh.setColorAt( instanceId, color.setHex( Math.random() * 0xffffff ) );
  93. mesh.instanceColor.needsUpdate = true;
  94. }
  95. }
  96. renderer.render( scene, camera );
  97. stats.update();
  98. }
  99. </script>
  100. </body>
  101. </html>