123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - raycast - sprite</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- body {
- background-color: #fff;
- color: #333;
- }
- a {
- color: #08f;
- }
- </style>
- </head>
- <body>
- <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl raycast sprite<br></div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- var renderer, scene, camera;
- var controls, group;
- init();
- animate();
- function init() {
- // init renderer
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- // init scene
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xffffff );
- group = new THREE.Group();
- scene.add( group );
- // init camera
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.set( 15, 15, 15 );
- camera.lookAt( scene.position );
- controls = new OrbitControls( camera, renderer.domElement );
- controls.enableRotate = true;
- // add sprites
- var sprite = new THREE.Sprite( new THREE.SpriteMaterial( { color: '#69f' } ) );
- sprite.position.set( 6, 5, 5 );
- sprite.scale.set( 2, 5, 1 );
- group.add( sprite );
- var sprite = new THREE.Sprite( new THREE.SpriteMaterial( { color: '#69f', sizeAttenuation: false } ) );
- sprite.material.rotation = Math.PI / 3 * 4;
- sprite.position.set( 8, - 2, 2 );
- sprite.center.set( 0.5, 0 );
- sprite.scale.set( .1, .5, .1 );
- group.add( sprite );
- var group2 = new THREE.Object3D();
- group2.scale.set( 1, 2, 1 );
- group2.position.set( - 5, 0, 0 );
- group2.rotation.set( Math.PI / 2, 0, 0 );
- group.add( group2 );
- var sprite = new THREE.Sprite( new THREE.SpriteMaterial( { color: '#69f' } ) );
- sprite.position.set( 0, 2, 5 );
- sprite.scale.set( 10, 2, 3 );
- sprite.center.set( - 0.1, 0 );
- sprite.material.rotation = Math.PI / 3;
- group2.add( sprite );
- window.addEventListener( 'resize', onWindowResize, false );
- window.addEventListener( "mousemove", onDocumentMouseMove, false );
- }
- function animate() {
- renderer.render( scene, camera );
- requestAnimationFrame( animate );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- var selectedObject = null;
- function onDocumentMouseMove( event ) {
- event.preventDefault();
- if ( selectedObject ) {
- selectedObject.material.color.set( '#69f' );
- selectedObject = null;
- }
- var intersects = getIntersects( event.layerX, event.layerY );
- if ( intersects.length > 0 ) {
- var res = intersects.filter( function ( res ) {
- return res && res.object;
- } )[ 0 ];
- if ( res && res.object ) {
- selectedObject = res.object;
- selectedObject.material.color.set( '#f00' );
- }
- }
- }
- var raycaster = new THREE.Raycaster();
- var mouseVector = new THREE.Vector3();
- function getIntersects( x, y ) {
- x = ( x / window.innerWidth ) * 2 - 1;
- y = - ( y / window.innerHeight ) * 2 + 1;
- mouseVector.set( x, y, 0.5 );
- raycaster.setFromCamera( mouseVector, camera );
- return raycaster.intersectObject( group, true );
- }
- </script>
- </body>
- </html>
|