OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Determine if an object is the same as another object in ThreeJS

  • Thread starter Thread starter realhuman
  • Start date Start date
R

realhuman

Guest
I am using ThreeJS and have the following code which should make a specific object move up when you hover over it, something like:

Code:
function onPointerMove(event) {
    pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;   

    raycaster.setFromCamera(pointer, camera);

    var intersects = raycaster.intersectObject(scene, true);

    if (intersects.length > 0) {
        var object = intersects[0].object;

        if (object == chair) { // Can I do this?
            object.position.set(0,0,10)
        }
    }
}

window.addEventListener( 'pointermove', onPointerMove );

chair is defined to be:

Code:
function loadGLTF() {
    loader.load('assets/models/Chair.glb', (gltf) => {
        scene.add(gltf.scene);

        gltf.scene.traverse( function ( object ) {

            if ( object.isMesh ) {
                object.castShadow = true
                object.recieveShadow = true;
            };

        } );

        return gltf;
    }, // ....
}

var chair = loadGLTF();

Is there any way to achieve this? I've already tried object == chair and object === chair but both unfortunately don't work.

<p>I am using ThreeJS and have the following code which should make a specific object move up when you hover over it, something like:</p>
<pre class="lang-js prettyprint-override"><code>function onPointerMove(event) {
pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

raycaster.setFromCamera(pointer, camera);

var intersects = raycaster.intersectObject(scene, true);

if (intersects.length > 0) {
var object = intersects[0].object;

if (object == chair) { // Can I do this?
object.position.set(0,0,10)
}
}
}

window.addEventListener( 'pointermove', onPointerMove );
</code></pre>
<p><code>chair</code> is defined to be:</p>
<pre><code>function loadGLTF() {
loader.load('assets/models/Chair.glb', (gltf) => {
scene.add(gltf.scene);

gltf.scene.traverse( function ( object ) {

if ( object.isMesh ) {
object.castShadow = true
object.recieveShadow = true;
};

} );

return gltf;
}, // ....
}

var chair = loadGLTF();
</code></pre>
<p>Is there any way to achieve this? I've already tried <code>object == chair</code> and <code>object === chair</code> but both unfortunately don't work.</p>
 

Latest posts

Top