Loading JSON models with raycasting and colliding with meshes in three.js

Currently, I am utilizing a raycaster to detect if the mouse (or touch) hits a mesh. The object is slender and I would like to enlarge the size of whatever can be intersected by the Ray. While working with game objects in three.js, is there a way for me to insert my own collision mesh that does not render but still activates the Ray?

https://i.sstatic.net/Aad1V.jpg

For instance, when the ray collides with an (invisible) pink cylinder linked to the original mesh, it triggers the intersection.

Simply creating another mesh grouped with the original one and setting visibility to false or opacity to 0 does not produce the desired result. I am searching for a more integrated solution, if available.

Thank you in advance!

Answer №1

If you need a collision mesh in your scene that can be detected by raycasting but doesn't need to be visible, follow these steps:

Add the collision mesh as a child of another mesh in your scene. Then, make sure to set the visibility of the collision mesh's material to false using the following code snippet:

collisionMeshMaterial = new THREE.MeshBasicMaterial( {
    visible: false
} );

By doing this, the collision mesh will be responsive to raycasting without being rendered on the screen.

This solution is applicable for three.js version r.74.

Answer №2

One way to handle clickable meshes is by maintaining a separate collection of them instead of adding them directly to the scene.

To check for intersection, you can simply use:

raycaster.intersectObjects(collection);

It's important to ensure that these meshes are synchronized with their parent object at all times.

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

How can NodeJS functions leverage parameters such as req, res, and result?

As a newcomer to JS, particularly Node and Express, I am in the process of learning how to build an API through tutorials. Along the way, I am also exploring various special features of JS such as let/const/var and arrow functions. One common pattern I no ...

Adjust the size of both the right and left price scales on TradingView Lightweight Charts

Is it possible to set a fixed width for both the right and left price scales on a chart? Let's say, 50 pixels for the right price scale and 70 pixels for the left price scale? For a working example, please visit https://jsfiddle.net/TradingView/cnbam ...

jQuery Form: Despite the Ajax response being visible on the page, it does not appear in the source code

Utilizing Jquery Form for an Ajax image upload. This is the relevant HTML code: <div class="input_con imageupload_con"> <form action="processupload.php" method="post" enctype="multipart/form-data" id="MyUploadForm"> < ...

AngularJS application: the button requires two clicks to activate

In my attempt to develop a simple CRUD app using AngularJS, I encountered an issue. The initial step involves clicking a button that fetches data from a REST API and populates an array. This array is then iterated using ng-repeat to generate Bootstrap card ...

"Mastering the art of event delegation: A guide to effectively engaging with various

I have data that is generated dynamically, as shown in the snippet below: const resultsDiv = document.getElementById("results"); const getList = document.getElementById("example"); document.querySelector(".container").addEventListener("click", function ...

Material UI filterSelectedOptions not functioning properly on initial search with multiple autocomplete

When I utilize the filterSelectedOptions prop in my autocomplete feature, it functions as intended when using a pre-defined chip. Check out image1 for an example: image1 However, when a brand new typed option is entered, it ends up duplicating multiple ti ...

Tips on converting deeply nested JSON into an excel file using Node.js

I am attempting to convert the JSON data below into an Excel file using XLSX. Although it successfully converts my JSON to Excel, I encountered an issue where the nested array of dailyPointsArray appears blank after conversion. Code Attempted const XLSX ...

Using Highcharts to dynamically color a map based on data values in a React TypeScript project

I'm attempting to generate a map where each country is filled with colors based on its specific data, similar to the example shown in this map. I am looking for a functionality akin to the use of the formatter function within the tooltip. I have expe ...

A guide to extracting functions from a `v-for` loop in a table

Beginner here. I am attempting to create a dropdown menu for the div with an id matching my specific name. For instance, let's say my table column names are: A, B, C. I only want to have a dropdown menu for column A. The template of my table looks ...

Switch out the Select feature for Checkboxes

Hey there! I'm currently trying to enhance the style and functionality of the Select checkboxes on my blog. However, when I change them to checkboxes, they lose their original function of searching for selected tags in each Select option. This issue i ...

What is the process for deleting a token from local storage and displaying the logout page in Next JS?

I developed a Next.js web application and am looking to display a logout page when the token expires, while also removing the expired token from local storage. How can I ensure that this functionality works no matter which page the user visits within the a ...

What is the best way to transfer attributes from a stateful component to an event handler within a Higher Order Component that encloses a child component?

My current project involves using a framework that requires passing an event handler into a Higher Order Component (HOC) which wraps one of the children of my stateful Page component. <Page> <HOC onClick={fn}> <PageColumn> ...

Using dynamic classes within a v-for loop

Edited How can I dynamically assign classes to multiple elements within a v-for loop? Since I cannot utilize a computed property due to the presence of v-for, I attempted using a method by passing the index of the element, but that approach did not yield t ...

What is causing this code to run immediately upon the addition of Promise logic?

The coding scenario written below was supposed to have two 4-second delays. However, when the code is run, it executes immediately. It seems that there might be a lack of understanding on my part regarding some basic concept, or perhaps there's a hidd ...

What is the method for incorporating functionality into the "X" icon within the Material UI Searchbar?

Check out this code snippet: import SearchBar from "material-ui-search-bar"; const info = [ { name: "Jane" }, { name: "Mark" }, { name: "Jason" } ]; export default function App() { const [o ...

Delay a macro task in the user interface event listener

I created a basic react component and added an onBlur event handler that creates a new macrotask using setTimeout. function App() { const onBlur=()=>{ console.log('onBlur') setTimeout(() => { console.log('setTimeout&apo ...

The drop-down menu remains visible even after clicking outside of it

I've written a script that works when clicked, but it doesn't hide when clicked outside of it. $(document).ready(function() { //Translate(); //caling Language Translater function $("#translate_image").attr('href', base_url) ...

`How to utilize the spread operator in Angular 4 to push an object to a specific length`

One issue I'm facing is trying to push an object onto a specific index position in an array, but it's getting pushed to the end of the array instead. this.tradingPartner = new TradingPartnerModel(); this.tradingPartners = [...this.tradingPartner ...

"Jquery's .append function may sometimes display as undefined when

$("#clckjson").click(function() { $(document).ready(function() { $.getJSON("fuelj.json", function(data) { $(data).each(function(i, item) { console.log(item.stationID); var $table = $('<table>'); $table.a ...

Encountering an endless loop while attempting to retrieve data from Firebase in Next.js through the use of useEffect

Currently, I am in the process of setting up a video section for a project using NextJS. The videos are stored in firebase storage. I have implemented a dynamic route that retrieves all videos from a specific reference within the bucket. For instance, if ...