Whats the simplest way to change color and size of mesh when the mouse hover over it and also make it clickable with url directly in three.js editor? I read docs tried codes for raycasting/intersection but it doesnt work.
How to apply it on simple box for example?
The simplest interaction. When you hover over a cube, it becomes (and still remains) “yellow”.
- Add a box.
- Add a direcitonal light.
- On the “Scene” tab, in the object tree, choose “Scene”
- Below on the same tab there’s the “Script” section. Click the “New” button, enter the name of the script and click the “Edit” button.
- In the code editor:
var box = this.getObjectByName("Box");
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2(1, 1);
var intersects = [];
function mousemove(event){
mouse.x = ( event.clientX / player.width ) * 2 - 1;
mouse.y = - ( event.clientY / player.height ) * 2 + 1;
}
function update( event ) {
raycaster.setFromCamera(mouse, camera);
intersects = raycaster.intersectObject(box);
if (intersects.length !== 0) {
let obj = intersects[0].object;
obj.material.color.set(0xffff00);
}
}
- Click “Play”
Perfect! Any similar basic example how to make box as hyperlink that open in same window? By the way great editor.
Hey Nobertino - just wondering if you ever were able to make a box a hyperlink? I’m trying to figure out how to do it but not having much luck. Thanks!
Instead of changing the material color in @prisoner849 code snippet, you execute
window.open( url, '_blank' );
You can assign the value for url
from a custom property in Object3D.userData.
Hi, I tried this code but nothing happened, can you help me?
@herfeiha
Try to change this
function mousemove(event){
to this
function pointermove(event){
thanks.
Can you change the color by clicking the mouse?
Of course, use window.addEventListener(‘pointerup’, listener); use the raycaster to get the object, and then change the material color…