webgl_instancing_raycast.html 3.9 KB

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