-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
Examples: Add raycast support for LineSegments2 #17872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
ee92495
76b55e4
7907d6c
74a0a3f
d073165
c4c0c46
fcd8db2
9e4fb71
30b27b2
3d420b1
1dc2887
e1120f9
fca2ea8
eca2412
dc96a86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| var matLine, matLineBasic, matLineDashed; | ||
| var stats; | ||
| var gui; | ||
| var mouse, raycaster, raycastPoint; | ||
|
|
||
| // viewport | ||
| var insetWidth; | ||
|
|
@@ -123,6 +124,14 @@ | |
| window.addEventListener( 'resize', onWindowResize, false ); | ||
| onWindowResize(); | ||
|
|
||
| mouse = new THREE.Vector2(); | ||
| raycaster = new THREE.Raycaster(); | ||
| raycastPoint = new THREE.Mesh( new THREE.SphereBufferGeometry( 1, 20, 10 ) ); | ||
| raycastPoint.material.color.set( 0xff0000 ); | ||
| raycastPoint.scale.setScalar( 0.25 ); | ||
| scene.add( raycastPoint ); | ||
| document.addEventListener( 'mousemove', onMouseMove, false ); | ||
|
|
||
| stats = new Stats(); | ||
| document.body.appendChild( stats.dom ); | ||
|
|
||
|
|
@@ -145,6 +154,33 @@ | |
|
|
||
| } | ||
|
|
||
| function onMouseMove( e ) { | ||
|
|
||
| e.preventDefault(); | ||
|
|
||
| mouse.x = ( e.clientX / window.innerWidth ) * 2 - 1; | ||
| mouse.y = - ( e.clientY / window.innerHeight ) * 2 + 1; | ||
|
|
||
| } | ||
|
|
||
| function doRaycast() { | ||
|
|
||
| camera.updateMatrixWorld(); | ||
| raycaster.setFromCamera( mouse, camera ); | ||
| var intersects = raycaster.intersectObjects( [ line, line1 ] ); | ||
| if ( intersects.length !== 0 ) { | ||
|
|
||
| raycastPoint.position.copy( intersects[ 0 ].point ); | ||
| raycastPoint.visible = true; | ||
|
|
||
| } else { | ||
|
|
||
| raycastPoint.visible = false; | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| function animate() { | ||
|
|
||
| requestAnimationFrame( animate ); | ||
|
|
@@ -160,6 +196,10 @@ | |
| // renderer will set this eventually | ||
| matLine.resolution.set( window.innerWidth, window.innerHeight ); // resolution of the viewport | ||
|
|
||
| // perform raycast after the resolution has been updated for the camera | ||
| // view we care about | ||
| doRaycast(); | ||
|
||
|
|
||
| renderer.render( scene, camera ); | ||
|
|
||
| // inset scene | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.