Using three.js to render two transparent spheres that overlap each other and handling the intersection visibility

I am struggling with controlling the display of two transparent, overlapping spheres in a webgl context. The issue arises when I want one sphere to be hidden behind the other during overlap. This problem can be observed on this page:

Specifically, I want the blue sphere to be concealed by the red sphere when they intersect.

Although rearranging the order in which the spheres are added to the scene (adding the big sphere first) resolves the problem and provides the desired outcome:

In my personal project, however, even adding the big sphere first does not eliminate the intersection issue.

The closest I have come to a solution is moving the smaller spheres closer to the camera. To demonstrate, I shifted the smaller spheres 10 units forward in this instance:

Yet, upon navigating with the arrow keys, occasional glimpses of the intersection occur and sometimes persist (especially noticeable with lateral flow of components).

Attempts such as applying depthWrite: false to both materials proved ineffective.

Tinkering with renderer.sortObjects = false gave unsatisfactory results as it disrupted the "natural" draw order that should take place within the broader scenery. The issue solely pertains to these spheres.

  1. Is there a method to dictate draw order of transparent objects in webgl / three.js? Even after shifting the smaller spheres 10 units nearer, they continue to z-fight.
  2. Alternatively, is there a way to enforce a specific behavior for overlapped transparent objects?

Answer №1

After spending quite a bit of time troubleshooting, I've uncovered a neat trick to control draw order using a flag on the mesh.

For Three.js versions r70 and above, you can utilize renderOrder:

mesh.renderOrder = 0.5; // Value between 0 and 1

On the other hand, for Three.js versions r69 and below, you should go with renderDepth:

mesh.renderDepth = 0.5; // Value between 0 and 1

The draw order follows a pattern from lowest rendering first to highest rendering last.

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 we stop the page from scrolling after a click?

Upon clicking the link labeled Click Me, the page automatically scrolls back to the top. This behavior is not desired. How can I correct this issue? For example, here is a link to a relevant resource: http://jsfiddle.net/Y6Y3Z/ Regarding the scroll-bar: ...

Converting and downloading CSV to XLSX directly from the front end using TypeScript and React

After successfully converting a JSON response to CSV format for download using the function below, I am now looking to achieve the same functionality but with xlsx files on the front end. The current function works well for CSV files and handles Japanese ...

The onChange event does not function properly on formatted Mui sliders

Having an issue with the Mui styled Slider. The value is not changing smoothly and it seems like the thumb of the slider is not draggable. You can see the issue reproduced here. Thank you in advance. ...

Exploring the intricacies of Bower and enhancing dependency management

Currently, I am working on developing a project named myapp using angularJS along with Yeoman Generator. This project involves utilizing Bower to manage dependencies and Grunt to wiredep those dependencies into the index.html file (which essentially genera ...

The most convenient method for automatically updating Google Charts embedded on a webpage

I am facing an issue with refreshing a Google Graph that displays data from a MySQL database. The graph is being drawn within a webpage along with other metrics: Data Output from grab_twitter_stats.php: [15, 32], [14, 55], [13, 45], [12, 52], [11, 57], [ ...

Enter a value into the input field with a single click

I am trying to accomplish the following: As I type on each line of a textarea, I want to automatically add a + symbol at the beginning of that line. The desired scenario is as follows: When the user starts typing on the first line, a + should appe ...

``I am experiencing an issue with HttpXMLRequest becoming un

I am currently experimenting with the HttpXMLRequest object in JS and have created a basic loop that retrieves name and age data from an XML file to display in a table. The issue I am facing is that if one of the elements is missing from a sibling, the cod ...

Determine the overall width of a JSX element

Is there a way to retrieve the width of a JSX.Element? In a traditional setup, I would typically use something like: const button = document.createElement('button'); document.body.appendChild(button) window.getComputedStyle(button).width However ...

Await the sorting of intervals

Looking for a solution to re-execute a hand-made for loop with a delay of 10 seconds after it finishes indefinitely. I've attempted some code, but it keeps re-executing every 10 seconds rather than waiting for the loop to finish before starting again. ...

A custom JavaScript function designed to facilitate the downloading of a file that includes the user's input directly to the user

Is there a way to use javascript to generate a file based on user input and provide it as a download without storing it on the server? For instance, imagine a scenario where a user is using an application and they want to download their work by clicking ...

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(' ...

I encountered an issue with passing arguments in the invoke function I redesigned

I am currently attempting to replicate the functionality of the .invoke() function. Although I can successfully call the function, I am facing difficulties when it comes to passing arguments. I have made attempts using call and apply, but unfortunately, I ...

Encountering error code 2064 without any clear explanation in sight

Hey, I'm currently facing an issue while uploading values to a MySQL table from Node.js. The error 1064 keeps popping up, indicating that the query is badly formatted. However, I can't seem to pinpoint the exact problem. Here's the query in ...

Is it possible to divide a three-dimensional object along the x-axis using three.js and WebGL?

Is it possible to crop a portion of a 3D object while keeping the hidden parts hidden and allowing the camera to move and rotate with the mouse? I need one object to be half shown and the others fully visible at all times. The current code crops everythin ...

Creating Comet applications without the need for IFrames

Currently embarking on my journey to develop an AJAX application with server side push. My choice of tools includes Grizzly Comet on Glassfish V2. While exploring sample applications, I've noticed that most utilize IFrames for content updates on the c ...

Focusing on the initial element following CSS prioritization

Check out this codepen link: http://codepen.io/muji/pen/XpEYzO I am looking to target the first element after it has been sorted using a CSS "order" property and apply another CSS property to it. Is there a way to use jQuery or any other method to identi ...

Choose the AuthGuard category in real-time

My application intends to employ two distinct authentication strategies - one for users accessing via a browser and another for the public API. A specific header will be set for browser users, allowing my app to determine the appropriate auth strategy base ...

Changing ch to Px in CSS

Important Notes: I have exhaustively explored all the questions and answers related to this particular topic. The question I have is very straightforward: How many pixels are equivalent to 1 character? Here's a sample of my code - code Related Sear ...

What are the steps to enable JavaScript before the next webpage finishes loading?

I am looking to create a loading screen that triggers when a Django form is submitted. Here is an example code snippet: forms.py class ExampleForm(forms.Form): example_field = forms.CharField( widget=forms.TextInput(attrs={'class': ...

Select elements from a PHP loop

As part of my school project, I am developing a basic webshop. Currently, I am using a while loop to display featured products on the homepage. However, I now need to implement a shopping cart functionality. After a user clicks the "add to cart" button, th ...