webgl_raycast_sprite.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - raycast - sprite</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. <style>
  9. body {
  10. background-color: #fff;
  11. color: #333;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl raycast sprite<br></div>
  20. <script type="module">
  21. import * as THREE from '../build/three.module.js';
  22. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  23. var renderer, scene, camera;
  24. var controls, group;
  25. init();
  26. animate();
  27. function init() {
  28. // init renderer
  29. renderer = new THREE.WebGLRenderer( { antialias: true } );
  30. renderer.setPixelRatio( window.devicePixelRatio );
  31. renderer.setSize( window.innerWidth, window.innerHeight );
  32. document.body.appendChild( renderer.domElement );
  33. // init scene
  34. scene = new THREE.Scene();
  35. scene.background = new THREE.Color( 0xffffff );
  36. group = new THREE.Group();
  37. scene.add( group );
  38. // init camera
  39. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  40. camera.position.set( 15, 15, 15 );
  41. camera.lookAt( scene.position );
  42. controls = new OrbitControls( camera, renderer.domElement );
  43. controls.enableRotate = true;
  44. // add sprites
  45. var sprite = new THREE.Sprite( new THREE.SpriteMaterial( { color: '#69f' } ) );
  46. sprite.position.set( 6, 5, 5 );
  47. sprite.scale.set( 2, 5, 1 );
  48. group.add( sprite );
  49. var sprite = new THREE.Sprite( new THREE.SpriteMaterial( { color: '#69f', sizeAttenuation: false } ) );
  50. sprite.material.rotation = Math.PI / 3 * 4;
  51. sprite.position.set( 8, - 2, 2 );
  52. sprite.center.set( 0.5, 0 );
  53. sprite.scale.set( .1, .5, .1 );
  54. group.add( sprite );
  55. var group2 = new THREE.Object3D();
  56. group2.scale.set( 1, 2, 1 );
  57. group2.position.set( - 5, 0, 0 );
  58. group2.rotation.set( Math.PI / 2, 0, 0 );
  59. group.add( group2 );
  60. var sprite = new THREE.Sprite( new THREE.SpriteMaterial( { color: '#69f' } ) );
  61. sprite.position.set( 0, 2, 5 );
  62. sprite.scale.set( 10, 2, 3 );
  63. sprite.center.set( - 0.1, 0 );
  64. sprite.material.rotation = Math.PI / 3;
  65. group2.add( sprite );
  66. window.addEventListener( 'resize', onWindowResize, false );
  67. window.addEventListener( "mousemove", onDocumentMouseMove, false );
  68. }
  69. function animate() {
  70. renderer.render( scene, camera );
  71. requestAnimationFrame( animate );
  72. }
  73. function onWindowResize() {
  74. camera.aspect = window.innerWidth / window.innerHeight;
  75. camera.updateProjectionMatrix();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. }
  78. var selectedObject = null;
  79. function onDocumentMouseMove( event ) {
  80. event.preventDefault();
  81. if ( selectedObject ) {
  82. selectedObject.material.color.set( '#69f' );
  83. selectedObject = null;
  84. }
  85. var intersects = getIntersects( event.layerX, event.layerY );
  86. if ( intersects.length > 0 ) {
  87. var res = intersects.filter( function ( res ) {
  88. return res && res.object;
  89. } )[ 0 ];
  90. if ( res && res.object ) {
  91. selectedObject = res.object;
  92. selectedObject.material.color.set( '#f00' );
  93. }
  94. }
  95. }
  96. var raycaster = new THREE.Raycaster();
  97. var mouseVector = new THREE.Vector3();
  98. function getIntersects( x, y ) {
  99. x = ( x / window.innerWidth ) * 2 - 1;
  100. y = - ( y / window.innerHeight ) * 2 + 1;
  101. mouseVector.set( x, y, 0.5 );
  102. raycaster.setFromCamera( mouseVector, camera );
  103. return raycaster.intersectObject( group, true );
  104. }
  105. </script>
  106. </body>
  107. </html>