Consistent faceIndex value returned by raycaster.intersectObject() in three.js

I am working on a project that involves a 3D cube rendered with THREE.CanvasRenderer(). My goal is to open a specific webpage based on which face of the cube is double clicked. However, I am facing an issue where the same page is opened regardless of the face clicked. After some makeshift debugging due to lack of console knowledge, I discovered that intersects[0].faceIndex always returns '11'. How can I modify it to accurately determine the nearest face of the cube?

Below is the snippet of my JavaScript code handling this functionality:

document.addEventListener('dblclick', onDocumentMouseDblClick, false);

function onDocumentMouseDblClick(event) {

    event.preventDefault();

    mouse.x = (event.clientX / renderer.domElement.width) * 2 - 1;
    mouse.y = -(event.clientY / renderer.domElement.height) * 2 + 1;

    raycaster.setFromCamera(mouse, camera);

    var intersects = raycaster.intersectObject(cube);

    if (intersects.length > 0) {
      if (intersects[0].faceIndex = 1 || 2) {
        window.open("About.html", "_self")
          //title.innerHTML = intersects[0].faceIndex;
      }
      if (intersects[0].faceIndex = 3 || 4) {
        window.open("Mondrian.html", "_self")
          //title.innerHTML = intersects[0].faceIndex;
      }
      if (intersects[0].faceIndex = 5 || 6) {
        window.open("page3.html", "_self")
          //title.innerHTML = intersects[0].faceIndex;
      }
      if (intersects[0].faceIndex = 7 || 8) {
        window.open("page4.html", "_self")
          //title.innerHTML = intersects[0].faceIndex;
      }
      if (intersects[0].faceIndex = 9 || 10) {
        window.open("page5.html", "_self")
          //title.innerHTML = intersects[0].faceIndex;
      }
      if (intersects[0].faceIndex = 11 || 12) {
        window.open("page6.html", "_self")
          //title.innerHTML = intersects[0].faceIndex;
      }

I initially referred to the THREE.js interactive cubes example and consulted the THREE.raycaster documentation, indicating that intersections should be ordered by closest distance. Despite trying methods involving Vector3's and unprojections from various Stack Overflow posts, they either failed or I misunderstood them, unfortunately unable to share links due to reputation.

Answer №1

Chances are, it will likely look something like this:

if (intersects[0].faceIndex === 1 || intersects[0].faceIndex === 2) {

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to repeatedly make ajax calls from a Java service layer function in order to display page number status?

Script: function retrievePageStatus(pageNumber) { showMessageHtml('messageDiv', "Retrieving page status for page " + pageNumber); var url = getURLString(pageForm); var urlstr = "importTemplate.do?userAction=retrieveStatus&" + url.s ...

I encountered an issue: "SyntaxError: missing ) after argument list". What steps should I take to fix it?

I'm encountering the error message "SyntaxError: missing ) after argument list" and I need assistance in resolving it. Click here to view the code that is causing the error ...

One way to combine two elements from an array in JavaScript is by comparing their dates (or any other similar element)

Currently, the array is structured as follows: array = [ { date: '2020/06/12', hours: 8.4 }, { date: '2020/06/15', hours: 4.5 }, { date: '2020/06/12', hours: 3.8 }, { date: '2020/06/16', hours ...

Encountered a 500 error while trying to save data in MongoDB

Currently, I am in the process of building a Vue application using Mongo and Express. Unfortunately, I have encountered a bug after sending a post request to Mongo. Uncaught (in promise) Error: Request failed with status code 500 at createError (createErr ...

Changing the Size of a Map using react-simple-maps within a React Application

I'm having difficulties with displaying a France map using react-simple-maps. The issue I'm facing is that the map appears too small despite my efforts to adjust it through CSS, width and height attributes, and by utilizing ZoomableGroup. Unfortu ...

Oops, it seems like the project is missing a `pages` directory. Please kindly create one in the project root. Thank you!

Initially, my project setup looked like this: public .next src pages components assets next.config.js It was functioning properly, but I made a structural change to the following: public src client next.config.js jsconfig.json pa ...

Having trouble accessing data from MongoDB using Node.js and Express

Looking to retrieve data from Mongo DB using Node JS and Express JS? After creating a mongoose schema and exporting the module, you may encounter an issue where running the function returns an empty JSON object. Take a look at the code snippets below: Thi ...

The content within the textbox remains locked until the user interacts with the textbox by clicking on

Is there a way someone can assist me with this task? I am looking to make it so that the values in the textbox cannot be changed or removed until the textbox is clicked. Does anyone have any insights on how to achieve this? ...

Unleashing the Power of Javascript in Selenium with Java (featuring PrimeFaces)

Recently, I discovered that JavaScript seems to be malfunctioning in my Selenium Tests written in Java. The reason behind this issue remains a mystery to me. Any ideas on how to tackle this roadblock? ((JavascriptExecutor) driver).executeScript("retur ...

One helpful tip for adjusting the size of a UI chip on the fly

I am attempting to adjust the size of a UI chip dynamically based on the font size of its parent elements using the em unit in CSS. My objective is to achieve something like this: style={{size:'1em'}} The issue I'm encountering: The chip e ...

Tips for effectively transferring data to a modal box that is declared outside of the immediate scope

In my current project, I am working with Bootstrap tabs and modal boxes. To correctly display the data in the modal box, I need to feed it with the right information. The issue arises when I try to define the modal inside the tabs - it doesn't display ...

How can we verify email addresses and URLs in PHP? Let's discuss converting this validation process

After studying the code extracted from the jquery.validate plugin, I find it quite challenging to decipher. My goal is to convert this code into PHP and I would greatly appreciate any assistance in understanding each segment of the regular expression codes ...

Having trouble with e.preventDefault() not working on submit() in Javascript?

I'm facing an issue with submitting a form using JavaScript submit() LIVE ACTION : https://jsfiddle.net/98sm3f3t/ HTML : <form id="myForm" action=""> First name: <input type="text" name="fname"><br> <button id="myButton ...

Retrieve the content from a div element and convert it into an integer for mathematical computations

I have a JavaScript function that performs calculations. The final values are stored and displayed in div elements like this: <div id="n3"></div> <div id="n4"></div> <div id="n5"></div> <div id="n6"></div> & ...

Error Message: TypeError - Unable to access property 'method' as it is not defined

I've just started working on a new node project and I've encountered an issue that I can't seem to figure out. :\Users\RTECH\Desktop\work\aumentedreality\modelViewerwithExpress\node_modules\express&b ...

Using the Petite-vue Initialization attribute within an HTML script tag

I came across a snippet of Vue.js code on Twitter from Evan You, but I'm confused about the purpose of the init attribute in the script tag. I've searched on MDN and other sites but couldn't find any information about it. However, I do unde ...

How to Use Jquery to Retrieve the Selected Value from a Drop-down List

I've implemented the following HTML code for a dropdown list: <select id="dd"> <option>Choose Report Type</option> <option value="1">Blocked Information</option> <option value="2">Supervisor Review</ ...

When attempting to run the npm install mathjs command, an error is displayed

Trying to install mathjs using npm but encountering an error: npm install mathjs The error message received is as follows: npm WARN tar TAR_ENTRY_ERROR UNKNOWN: unknown error, write npm WARN tar TAR_ENTRY_ERROR UNKNOWN: unknown error, write npm WARN tar T ...

Issues encountered with Jquery hover functionality not responding as expected

When the mouse hovers over it, I want to explain changing the src of img tag as much as possible; here is the HTML: <ul id="nav-tabs" data-tabs="tabs"> <li id="test" style="list-style: none;" class="active"> <a href="#home" data-toggle ...

What causes my Excel file to become corrupted when inputting new data?

My intention with this code is to insert "ABC" into cell B3 of an existing Excel document. However, when the file is written, its size decreases significantly and Excel is unable to open it. const excel = require("exceljs"); const template = "./myexcel.xl ...