You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
3.9 KiB

2 years ago
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js raycaster - 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> raycaster - sprite<br></div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
let renderer, scene, camera;
let group;
let selectedObject = null;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
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 );
const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 15;
controls.maxDistance = 250;
// add sprites
const sprite1 = new THREE.Sprite( new THREE.SpriteMaterial( { color: '#69f' } ) );
sprite1.position.set( 6, 5, 5 );
sprite1.scale.set( 2, 5, 1 );
group.add( sprite1 );
const sprite2 = new THREE.Sprite( new THREE.SpriteMaterial( { color: '#69f', sizeAttenuation: false } ) );
sprite2.material.rotation = Math.PI / 3 * 4;
sprite2.position.set( 8, - 2, 2 );
sprite2.center.set( 0.5, 0 );
sprite2.scale.set( .1, .5, .1 );
group.add( sprite2 );
const 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 );
const sprite3 = new THREE.Sprite( new THREE.SpriteMaterial( { color: '#69f' } ) );
sprite3.position.set( 0, 2, 5 );
sprite3.scale.set( 10, 2, 3 );
sprite3.center.set( - 0.1, 0 );
sprite3.material.rotation = Math.PI / 3;
group2.add( sprite3 );
window.addEventListener( 'resize', onWindowResize );
document.addEventListener( 'pointermove', onPointerMove );
}
function animate() {
renderer.render( scene, camera );
requestAnimationFrame( animate );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onPointerMove( event ) {
if ( selectedObject ) {
selectedObject.material.color.set( '#69f' );
selectedObject = null;
}
pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
raycaster.setFromCamera( pointer, camera );
const intersects = raycaster.intersectObject( group, true );
if ( intersects.length > 0 ) {
const res = intersects.filter( function ( res ) {
return res && res.object;
} )[ 0 ];
if ( res && res.object ) {
selectedObject = res.object;
selectedObject.material.color.set( '#f00' );
}
}
}
</script>
</body>
</html>