Why is the raycaster unable to detect the object that has been loaded in three.js?

I am new to THREE.js. I would like to implement a feature where clicking the mouse selects a loaded .obj object in the scene, highlights it by making it half transparent, and then allows the object to follow the mouse as it moves. Subsequently, another click should restore the object's solid color and make it stick to the clicked position.

Despite adding the loaded objects to the objects array, the raycaster seems unable to detect when I click on these objects.

Check out my code below:

<html>
<head includeDefault="true">

  <script src="js/three.js"></script>
  <script src="js/PointerLockControls.js"></script>
  <script src="js/OBJLoader.js"></script>
  <script src="js/MTLLoader.js"></script>
  <script src="js/dat.gui.min.js"></script>
  <script src="js/stats.min.js"></script>
  <script src="js/OrbitControls.js"></script>
  </head>
<body>
    <script>
        // Your JavaScript code goes here
    </script>
    </body>
    </html>

Answer №1

You should consider using this more conventional syntax for raycasting in three.js

      var cursor = new THREE.Vector2();
      var detector = new THREE.RayCaster();
      cursor.x = (e.clientX / window.innerWidth) * 2 - 1;
      cursor.y = -(e.clientY / window.innerHeight) * 2 + 1;
      detector.setFromCamera(cursor, camera);

      var hits = detector.intersectObjects(objects);

If you're still having problems, ensure that all your meshes are visible because three.js raycasting won't detect meshes with visible set to false

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

Learn how to create a dynamic React.useState function that allows for an unlimited number of input types sourced from a content management system

Currently, I am exploring the possibility of dynamically creating checkbox fields in a form using DatoCMS and the repeater module. My idea is to generate multiple checkbox fields based on the input from a text field in Dato as a repeater, which can then be ...

Creating TypeScript projects without the use of a conventional IDE or text editor

Due to restrictions in my workplace, I am unable to download or install any applications, regardless of my job duties. I am interested in migrating several web apps to Angular 2 using TypeScript. I am a C# developer in my free time and I find TypeScript ap ...

Generate responsive elements using Bootstrap dynamically

I'm having success dynamically generating bootstrap elements in my project, except for creating a drop-down menu. ColdFusion is the language I am using to implement these div elements: <div class="panel panel-primary"><div class="panel-head ...

What is the best way to highlight selected navigation links?

Recently, I implemented a fixed navigation bar with relevant links on a website. The navbar includes a jquery script for smooth scrolling when clicked. However, I am struggling to add a selected class to the clicked link. Despite trying various solutions f ...

Storing user authentication tokens securely in session storage within Next.js can help maintain a

Is there a way to ensure that user data remains persistent even after a page refresh? I considered storing it in local storage, but that may result in a flash of unauthenticated content. Storing it in a cookie could also be problematic when working with ...

What could be the reason for the absence of {{ list.title }} on display

I'm currently learning how to develop my first MEAN stack app by following a tutorial on YouTube. However, I've encountered an issue where the title of the list is not displaying correctly. I'm using Insomnia to create the lists. Here's ...

Struggling to retrieve the fake JavaScript data for my Vue.js table

I am a beginner in the development field and encountering the following error report while working with Vue.js 3 115:5 error 'vendorTable' is assigned a value but never used no-unused-vars 115:23 error 'Vue' is not defined no- ...

Is there a way to import a module generated by typescript using its name directly in JavaScript?

I am trying to bring a function from a typescript-generated module into the global namespace of JavaScript. The typescript module I have is called foo.ts: export const fooFn = (): string => { return "hello"; }; Below is the structure of my HTML file ...

Is there a way to automatically launch a new tab upon arriving from another website?

I currently have a website that displays a table of elements, each with its own set of sub-elements. On another page, I can view the element along with multiple tabs for its corresponding sub-elements. The current setup is depicted as follows: <a clas ...

During the loop, the variable seems to be undefined despite being defined earlier

Alright, so here's the deal - I'm working on a script for a responsive slider and my main goal is to identify the slide with the most text in order to determine which one is the largest. Once I find that slide, I measure its height and adjust the ...

jQuery click() function fires twice when dealing with dynamic elements

I am loading content from a database through ajax into a div and then when clicking on any of these content pieces, it should reload new content. The ajax request is initialized as a method within a class that I call at the beginning: function Request(ta ...

What's the best way to ensure an endless supply of copied elements with identical classes to the clipboard?

Here is a snippet of my HTML code: <Div Class='POSTS'> <Div Class='POST'> <Div Class='CONTENT'>Text1</Div> </Div> <Div Class='POST'> <Div Class=&apos ...

Using Javascript to parse a regular expression in a string

I am facing a challenge with a JavaScript string that contains contact information that needs to be filtered. For example, I want to mask any email or phone number in the message. I have attempted the following approach: function(filterMessage) { ...

Sending an array from one page to another with Vue

I'm facing the challenge of passing an array from one page to another. When accessing the next page on form submission using this.$router.push('path'), is there a way for me to also transfer the array so that it can be accessed on the new p ...

Unable to submit AngularJS form - experiencing technical difficulties

I am encountering an issue with a form that takes input for the name of a country and should update a global variable. However, when the submit button is clicked, nothing happens. Can someone point out where I am going wrong? Below is the HTML code snippet ...

Tips for eliminating corner indexes from a 2D array?

Exploring forward ray tracing algorithm using Three.js. I have developed a sample using a 2D array where the Spotlight's location is parsed, but not directly involved. To create lines of sight, I set: startPoint = position of SpotLight endPoint = ...

Saving JavaScript variables to a JSON file

In my possession is a JSON file named example_json.json, which contains the following data: { "timeline": { "headline":"WELCOME", "type":"default", "text":"People say stuff", "startDate":"10/4/2011 15:02:00", ...

Trigger the select dropdown when a key is clicked

I am currently working on a project in react where I am utilizing the Select component from material. When I open the dropdown and press a key on my keyboard, the option starting with that key gets automatically selected. This behavior is also observed in ...

Utilize the key-value pair from ng-repeat to expand the scope of the expression

In an attempt to utilize the key value from ng-repeat as an extension of another scope.arrayResult, I aim to achieve arrayResult.q1/q2/q3 etc... <ul ng-repeat="(key,x) in data"> <li><h4>Question: {{x}}</h4> <p>{{ ar ...

Checking the correctness of an entered regex pattern and example

My form contains two textboxes - one for entering a regex pattern, and the other for inputting text. I am attempting to validate if the entered regex pattern matches with the input text. Check out my simple code snippet. Here's a demo of it in acti ...