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