Created
January 10, 2022 20:03
-
-
Save tomshaw/97c65dafbb2600798a74f8824448031c to your computer and use it in GitHub Desktop.
Make the mesh follow the mouse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onMouseMove(event) { | |
this.mouse.x = (event.clientX / window.innerWidth) * 2 - 1; | |
this.mouse.y = - (event.clientY / window.innerHeight) * 2 + 1; | |
// Make the sphere follow the mouse | |
var vector = new THREE.Vector3(this.mouse.x, this.mouse.y, 0.5); | |
vector.unproject( this.camera ); | |
var dir = vector.sub( this.camera.position ).normalize(); | |
var distance = - this.camera.position.z / dir.z; | |
var pos = this.camera.position.clone().add( dir.multiplyScalar( distance ) ); | |
this.mesh.position.copy(pos); | |
// Make the sphere follow the mouse | |
// mouseMesh.position.set(event.clientX, event.clientY, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment