Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

403
Views
Clicking on GLTF models - Threejs

I'm working on a threejs project where I'm trying to load a GLTF file containing geometric shapes. I am writing a logic to get info, for now just the name, of the shapes that I click within the GLTF file. For now, I am just using console.log() to print the name of the shape I have clicked. I also want a side navigation bar to appear with the name of the component, I have a simple OpenNav() function for the navigation bar, as shown below. I now want to use the HTML header tag to display found[index].object.name (the component's name). I am kind of stuck here. How do I achieve this?

<div id = "mySidenav" class = "sidenav">
        <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
        <h2>Hello world!</h2>
</div>
    <script src = "three.js"></script>
    <script type = "module" src = "index.js"></script>
    <script>
        function closeNav() {
            document.getElementById("mySidenav").style.width = "0";
        }  
    </script>  

click event function in index.js

function openNav() {
  document.getElementById("mySidenav").style.width = "300px";
}

window.addEventListener('click', event => {  
  // enables object selection
  clickMouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  clickMouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  // enables different object selection 
  raycaster.setFromCamera( clickMouse, camera );
  //checking mesh objects the ray are intersecting with
  const found = raycaster.intersectObjects( scene.children );
  // console.log(found);
  // console.log(found[0]);
  if(found.length > 0){
      // console.log(found);
       for (let index = 0; index < found.length; index++) {
         if (found[index].object.name === "Cone") {
            console.log(found[index].object.name);          
            openNav();
         }
    }
  }
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ok i have it.

I would recommend you replace:

window.addEventListener('click', event => {  
  // enables object selection
  clickMouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  clickMouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  // enables different object selection 
  raycaster.setFromCamera( clickMouse, camera );
  //checking mesh objects the ray are intersecting with
  const found = raycaster.intersectObjects( scene.children );
  // console.log(found);
  // console.log(found[0]);
  if(found.length > 0){
      // console.log(found);
       for (let index = 0; index < found.length; index++) {
         if (found[index].object.name === "Cone") {
            console.log(found[index].object.name);          
            openNav();
         }
    }
  }
})

With this:

function Listener(listener, mesh, callback) {
    let objects = [mesh];
    let raycaster = new THREE.Raycaster();
    let mouse = { x: 0, y: 0 };
    renderer.domElement.addEventListener(listener, raycast, false);
    function raycast(e) {
        mouse.x = (e.clientX / renderer.domElement.clientWidth) * 2 - 1;
        mouse.y = -(e.clientY / renderer.domElement.clientHeight) * 2 + 1;
        raycaster.setFromCamera(mouse, camera);
        let intersects = raycaster.intersectObjects(objects, true);
        let intersect = intersects[0];
        if (typeof intersect !== "undefined") {
            callback(e, intersect);
        }
    }
}
for (let i = 0; i < scene.children.length; i++) {
    Listener('click', scene.children[i], (e, intersect) => {
        let object = intersect.object;
        if (object.name === 'Cone') {
            openNav();
        }
    });
}

Be gentle i'm 13 :)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!