Custom geometry in Three.js raycaster detects incorrect object

I am facing a challenge with the default cube's appearance in wireframe mode, as it is made up of triangles instead of squares.

To combat this, I created my own geometry which looks satisfactory. However, I have noticed that the raycaster does not function as effectively with my custom objects compared to the pre-built cubes.

var cube = new THREE.Line( getCube( 5,5, 5), new THREE.LineDashedMaterial( { color: 0x000000,dashSize: 1, gapSize: 0.1, linewidth: 2 } ),THREE.LinePieces );

The function getCube() returns:

var geometry = new THREE.Geometry()

For a visual example, refer to: http://jsfiddle.net/QHjSM/12/

The six color filled boxes on the top are default THREE.CubeGeometry boxes, which can be selected accurately using the raycaster. The wireframe boxes below are my custom geometry.

There are some challenges: When attempting to click outside a box, it may inadvertently select the box instead. Additionally, clicking directly in the center of a box may not register the selection. Another issue is that clicking near one box may result in the selection of a neighboring box.

Despite trying various geometry.compute methods, the issue persists and I am uncertain if there is a better solution.

Answer №1

Hello there! I noticed that your custom cubes may not be true cubes after all, as they seem to be more like a stack of lines without corresponding faces. This could be why your selection is not working as expected, as the "cubes" have gaps running through them. One solution could be to adjust your getCube function to build the faces after creating the vertices in a similar manner.

For reference, you can check out this example: Issue with custom geometry and face normal

Typically, you need to organize every 3 sets of vertices carefully to ensure that the faces are built in a clockwise direction for correct normal computation. Below is a simple example of adding a face.

geometry.faces.push(new THREE.Face3(1,2,3));

However, keep in mind that this method may still result in diagonal lines in your wireframe. To address this, you could consider using a basic cube mesh for selection and then overlaying custom wireframe lines for the desired effect.

Just a heads up, Face4 is no longer available, so you'll have to work with Face3 and implement a custom wireframe for this purpose.

Regarding your fiddle test, I observed an odd behavior. Even with the wireframe disabled, diagonal lines are still visible on the default cube when using the CanvasRender option. Switching to WebGLRenderer seems to resolve this issue. I will investigate further on this matter.

CanvasRenderer http://jsfiddle.net/QHjSM/13/

WebGLRenderer http://jsfiddle.net/QHjSM/14/

Upon further examination, it seems that the ghosted face lines are present in all CanvasRenderer examples utilizing a MeshBasicMaterial on the Three.js website. One workaround could be to reduce the opacity of the cube mesh material to minimize this effect. Alternatively, switching to WebGLRenderer may offer a different result. Here's one final test:

http://jsfiddle.net/QHjSM/16/

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

Is there a way to transfer textbox value to ng-repeat in AngularJS?

1) The Industry dropdown menu corresponds with a code textbox. Depending on the selected industry, the code will change accordingly. 2) There is a dynamic feature to add or delete Movie Names and Directors' names. In this table, there are three colu ...

Pair each element with an array of objects and add them to a fresh array

Let's consider an array of objects like, const attachmentData = [{name: 'Suman Baidh',attachment: ["123","456"]}, {name: 'John Sigma',attachment: ["789","101112]}, ...

What is the method for obtaining the size of a mesh object in pixels (instead of Vector3) within BabylonJS?

Exploring Babylon.js How can I retrieve the size of a mesh object in pixels instead of Vector3 in a React application using Babylonjs? This is what I attempted: let meshPixelSize = meshObject.getBoundingInfo().boundingBox; ...

Scope of MongoDB's `.findOne` method

Having trouble with variable scope. var max; ClassModel.findOne({ class: '1a' }, function (err, class1a) { if (err) return handleError(err); max = class1a.members; console.log(max); }); console.log(max); Why does ...

Include a personalized header in $http

In my factory, I have multiple functions that follow this structure: doFunction: function () { return $http({ method: 'POST', url: 'https://localhost:8000/test', data: {}, headers: { "My_Header" ...

Is it possible to simultaneously run watchify and node-sass watch together?

Currently, I am utilizing npm scripts in conjunction with watchify and node-sass while incorporating the -w parameter. I am curious if it is feasible to execute both of these 'watch' commands simultaneously. Would this setup ensure that only sty ...

How can I retrieve x-data from an external file using Alpine.js?

I recently started exploring Alpine.js and grasped the fundamentals, but I'm facing challenges when trying to move functions outside of inline script tags. Consider this snippet from index.html: <div x-data="{ loading: false }"/> &l ...

Executing a message in Azure Service Bus using Javascript/Node.js

I am utilizing the @azure/service-bus library in a Node.js application to receive messages. The code I am using is as follows: const { ServiceBusClient } = require("@azure/service-bus"); const sbClient = new ServiceBusClient(connectionString); ...

Implement seamless content loading using jQuery, eliminating the need

Is there a reason why this piece of code isn't working for me? I'm trying to load HTML content using jQuery and followed the instructions from this tutorial: Here's how my HTML looks: <div id="icon"> <a href="http://localhost/ ...

Error TS2307: Module 'angular' could not be located

I have encountered an error in my project and despite searching for solutions, I haven't been able to find one that addresses my specific issue. It seems like a simple problem, but I can't figure out what I'm missing. The error arises in th ...

Using Regular Expressions to access a deeply nested object

Within my JS objects, there is a common grandchild property called 'products'. For example: ecommerce.add.products, ecommerce.remove.products, ecommerce.detail.products, ecommerce.checkout.products, ecommerce.purchase.products I am attempting t ...

Tracking ajax calls with piwik: A step-by-step guide

I'm curious about how to enable piwik to track ajax requests. I know there is an API available, but I'm unsure about the exact steps I need to take in order to view ajax loaded pages in the dashboard. Could it be something like this: _paq.push( ...

What is the speed of communication for Web Worker messages?

One thing I've been pondering is whether transmission to or from a web worker could potentially create a bottleneck. Should we send messages every time an event is triggered, or should we be mindful and try to minimize communication between the two? ...

Utilizing Angular's ngShow and ngHide directives to hide spinner when no data is retrieved

I'm having an issue with the HTML code below. It currently displays a spinner until a list of tags is loaded for an application. However, the spinner continues even if no events exist. I want to make the spinner disappear and show either a blank inpu ...

What changes can I make to the script in order to display every element from a MySQL query using PHP, MySQL, JavaScript,

Enhanced Multiple Dropdown Selection Using Ajax In the following scenario, we have a webpage featuring a multiple dropdown selection that interacts with a MySQL database. By choosing options in the dropdowns labeled site, menu, and category (categ), a que ...

Tips for validating and retrieving data from a radio button paired with an input box in reactjs

I'm diving into the world of React and facing a challenge with multiple radio buttons that have associated input fields, like in this image: https://i.stack.imgur.com/Upy3T.png Here's what I need: If a user checks a radio button with a ...

I'm encountering an issue with my React master component not being able to locate the

I am having trouble importing a component in my APP.js file. I have been attempting to bring MainComponent into the app.js component, but I am facing difficulties in fetching the component. Any assistance in resolving this issue would be greatly apprecia ...

Issue with Node.js MongoDB collection.find().toArray not returning results

Despite coming across questions similar to mine, I struggled to resolve the issue independently. Within my '../models/user' model, my goal is to retrieve all users and store them in an array, which will then be returned to the controller for fur ...

Using Highmaps in a VueJs application involves passing a state to the mapOptions for customization

I'm currently struggling with passing a vuex state to mapOptions in vuejs components. Here is the code snippet: <template> <div> <highcharts :constructor-type="'mapChart'" :options="mapOptions" class="map">&l ...

The useSelector value remains undefined within the handleSubmit button in a React component

Once a user fills out and submits the form, the action is triggered to call the API. Upon returning the postId, it is stored in the reducer. The main React component then utilizes useSelector to retrieve the latest state for the postId. However, when attem ...