When casting a ray from the inside, the raycast does not collide with the mesh

In my latest project, I've created a unique scene that involves placing my camera inside a sphere geometry.

var mat = new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture('0.jpg') , overdraw:true,  color: 0xffffff, wireframe: false });
        var sphereGeo = new THREE.SphereGeometry(1000,50,50);
        var sphere = new THREE.Mesh(sphereGeo,mat);
        sphere.scale.x = -1;
        sphere.doubleSided = false;
        scene.add(sphere);

One of the features I'm trying to implement allows me to move around within the sphere and cast a ray on mouse down to determine the coordinates of the point where the ray hits the sphere. However, I'm facing an issue with empty intersects when I cast the ray.

var vector = new THREE.Vector3();
      vector.set( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
      vector.unproject( camera );

      raycaster.ray.set( camera.position, vector.sub( camera.position ).normalize());

var intersects = raycaster.intersectObjects(scene.children, true);

I have successfully tested this functionality with a cube placed inside the sphere. Now, I'm wondering if it matters whether you hit the object from the inside or not, as this could be a potential explanation for the issue I'm encountering.

Any insights or suggestions would be greatly appreciated. Thank you!

Answer №1

sphere.doubleSided to
sphere.material.side = THREE.DoubleSide
which occurred several years ago.

Answer №2

When dealing with hitting objects from the inside, it becomes crucial as a ray's path may be affected. Typically, inverted surfaces are overlooked during rendering and raycasting due to backface culling in the pipeline.

To address this issue, you can experiment by toggling between sphere.doubleSided = false; and sphere.doubleSided = true;. This adjustment should enable the raycast to detect intersections within the sphere. [note: does not apply to negative scale]

If needed, you can access the "dirty vertices" mode to manually flip the normals:

mesh.geometry.dynamic = true
mesh.geometry.__dirtyVertices = true;
mesh.geometry.__dirtyNormals = true;

mesh.flipSided = true;

//invert every vertex normal within mesh by multiplying by -1
for(var i = 0; i<mesh.geometry.faces.length; i++) {
    mesh.geometry.faces[i].normal.x = -1*mesh.geometry.faces[i].normal.x;
    mesh.geometry.faces[i].normal.y = -1*mesh.geometry.faces[i].normal.y;
    mesh.geometry.faces[i].normal.z = -1*mesh.geometry.faces[i].normal.z;
}

mesh.geometry.computeVertexNormals();
mesh.geometry.computeFaceNormals();

Additionally, I recommend adjusting the scale to 1.0 instead of -1.0 for better results.

Feel free to reach out if you encounter any difficulties!

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

Navigating with React in ES5

My email addresses are "[email protected]" and "[email protected]". I am facing an issue with the following minimal example code: var React = require('react'); var ReactDOM = require('react-dom'); var Router = require(' ...

How can I insert an empty option following a selected value in a dropdown menu using jQuery?

How to use jQuery to insert a blank option after an existing option? I attempted to add a blank option, but it ended up at the top of the dropdown. I actually need one existing option followed by one blank option. For example: <option></option& ...

Is it possible to prevent the text from appearing in the text box when a button is

I have successfully implemented a feature where clicking on the button (click on me) in HTML displays a textbox along with another button (show) on the screen. The text written in the textbox is visible when the show button is clicked. However, after the i ...

How can I omit extra fields when using express-validator?

Currently, I am integrating express-validator into my express application and facing an issue with preventing extra fields from being included in POST requests. The main reason for this restriction is that I pass the value of req.body to my ORM for databas ...

Using a variable name to retrieve the output in JavaScript

I created a unique JavaScript function. Here is the scenario: Please note that the code provided below is specific to my situation and is currently not functioning correctly. analyzeData('bill', 'userAge'); Function analyzeData(u, vari ...

The AngularJS directive "ng-include" is used to dynamically

I am encountering an issue with ng-include not retrieving the file. What could be the reason for the problem in accessing a property from a link within ng-include? I would appreciate any assistance with resolving this matter. (function(){ var app = angu ...

Issues arise when using ng-repeat in conjunction with ng-click

I am facing some new challenges in my spa project with angularjs. This is the HTML snippet causing issues: <a ng-repeat="friend in chat.friendlist" ng-click="loadChat('{{friend.friend_username}}')" data-toggle="modal" data-target="#chat" d ...

Tips on creating a personalized memoizeOne function that delivers the accurate data type

I've implemented a function for object memoization: import memoizeOne from 'memoize-one'; type ArrayWithOneObj = [Record<string, unknown>]; const compareObject = ([obj1]: ArrayWithOneObj, [obj2]: ArrayWithOneObj) => obj1 === obj ...

Preserve the output from the jQuery ajax request

I have created a custom function that calls an ajax request and saves the response data to a variable before returning it. It always shows '0' as the return value, but the alert displays numbers like 3712. Below is the implementation of the func ...

Unlocking the intricacies of navigating through nested arrays of objects in JavaScript

I need help figuring out how to loop through a nested array of objects in my code. I've tried different approaches but can't seem to grasp how it works. The object data I have looks like this: [ { "restaurantName":"Bron ...

Verifying website responsiveness using Puppeteer

Looking to create a script that can determine if a webpage has a responsive design? Wondering how to go about it? Well, it's quite simple. In responsive websites, elements like divs, spans, footers, headers, and sections typically adjust to fit the s ...

What is the best way to create an arrow connecting a parent div to multiple child divs?

I'm faced with a challenge involving a reusable React list component that supports nested children. My goal is to visually connect the parent div to its direct children using arrows, similar to the image linked below. View List Component Image Below ...

The filtering feature for array and model selection in Angular's UI-Select appears to be malfunctioning

Below is a Json structure: $scope.people = [ { name: 'Niraj'}, { name: 'Shivam'}, { name: 'Arun'}, { name: 'Mohit'}] There's also a variable, var array = "Niraj,Shivam";. My goal is to filter out the names fro ...

"Unfortunately, SimplyScroll isn't functioning properly on this particular page

What is the reason for simplyscroll not being able to handle this div with nested divs? EXTRA INFORMATION The scrolling functionality does not work. Clicking on the buttons doesn't trigger any scrolling. Changing the contents of the div to plain tex ...

Managing Modules at Runtime in Electron and Typescript: Best Practices to Ensure Smooth Operation

Creating an Electron application using Typescript has led to a specific project structure upon compilation: dist html index.html scripts ApplicationView.js ApplicationViewModel.js The index.html file includes the following script tag: <script ...

Show a variety of pictures in a random arrangement?

Looking for some help here! I'm trying to shuffle my images in a random order each time the page is refreshed. It's like a game of musical chairs but with pictures! Does anyone know how to achieve this? I've done some searching, but haven& ...

What could be causing the malfunction of Bootstrap Multiselect functionality?

I have been attempting to set up Bootstrap Multiselect but it simply refuses to work. Despite trying various solutions, I am unable to pinpoint the issue. My index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

Discover a method to retrieve all recommended strings based on a search query using JavaScript

I have two strings: one containing all the values of countries and the second string that I entered when searching for a country. Now, I am looking to retrieve all results that contain "In", such as India and Indonesia. For example, if I search for "IN" ...

Grouping an array of arrays of objects

I am trying to group an array of objects based on the value of the first item below: const data = [{"value":"value1","metric":1},{"value":"value1","metric":2},{"value":"value3","metric":0},{"value":"value2","metric":4},{"value":"value3","metric":1},{"va ...

Enter the variable into the parameter

I'm working with some Javascript code: document.getElementById("Grid").style.gridTemplateRows = "repeat(3, 2fr)"; I'm trying to insert a variable as an argument to modify my CSS style. When I attempt the following: "repe ...