Exploring Transparency in THREE JS: Rendering select faces of a BufferGeometry with a see-through effect

I am faced with the challenge of rendering two cubes on a single non-indexed buffer geometry. My goal is to make these cubes transparent so that all sides are visible simultaneously. To achieve this, I aim to utilize the "vertexAlphas" material property to specify an alpha value for each face of the geometry through the color attribute.

Below is my code snippet:

const geometry = new THREE.BufferGeometry()

const vertices = getVertices() // retrieves array of vertices for the two cubes
const colors = getColors() // retrieves rgba values for the two cubes

geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(vertices), 3))
geometry.setAttribute('color', new THREE.BufferAttribute(new Uint8Array(colors)), 4))

geometry.attributes.color.normalized = true
geometry.computeVertexNormals()

const material = new THREE.MeshStandardMaterial({ transparent: true, vertexColors: true, vertexAlphas: true })

const mesh = new THREE.Mesh(geometry, material)
scene.add(mesh)

This implementation successfully achieves transparency for the cubes. However, the middle faces are only visible from one side.

Front view: https://i.sstatic.net/8T7D1.png

Rear view: https://i.sstatic.net/CE67C.png

The issue appears to be related to the depthWrite function. While setting DepthWrite to false could potentially resolve it, I require the flexibility to have one of the cubes opaque, making this solution unsuitable. Is there a method to essentially "bypass" depthWrite for the transparent faces (or a similar approach) to ensure everything behind them is always visible regardless of the viewing angle? I prefer to maintain the usage of the vertex alpha property rather than resorting to grouping and multiple materials to avoid multiple render calls.

Could a custom shader offer a solution? Without knowledge in writing a custom shader, I would appreciate insights on whether this approach could be effective. Thank you to anyone able to provide guidance!

Answer №1

The source of the problem has been identified,

It turns out that the issue was related to the normal vectors. The normal vectors determine the direction in which each face is oriented, so for a cube, opposite faces need to have opposite normal values (e.g., the top face needs {x: 0, y: 0, z: 1} while the bottom face needs {x: 0, y: 0, z: -1}).

The reason why only one side of the faces was hidden was due to the computeVertexNormals function. This function did not recognize that I was dealing with a cube geometry, resulting in identical normal values for opposite faces.

By removing the computeVertexNormals function and manually assigning the correct normal values, the issue was resolved effortlessly.

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

Struggling with validating props in React with JavaScript

Hello everyone, I'm new to JS and react so please bear with me. I've encountered an issue in the CurrentNumber component where instead of getting a number type returned, it shows [object Object]. I believe there might be a problem with my types/p ...

Learn how to extract values from an object in Vue JS time-slots-generator and display either PM or AM based on the

Using the time-slots-generator package, I am able to display the time from 0:15 to 24:00. However, the issue is that this package does not have built-in functionality to display AM/PM, so I had to create a manual solution for it. To achieve this, I modifi ...

Unspecified content was received in the AJAX response

These are the two posts I am currently following: Ajax responseText comes back as undefined Can't return xmlhttp.responseText? Although I have implemented the code in the same way, I am encountering: "undefined is not a function" error message ...

Issues with Bootstrap 4 accordions failing to expand on mobile devices

I'm facing a strange issue with my Bootstrap 4 accordions. They work fine in responsive mode on Chrome, but when I try it on an Android phone or iPhone, they refuse to open. I'm really puzzled by this. Any thoughts on what might be causing this? ...

Error: Module cannot be found when using node.js local modules

I am currently working on incorporating local modules into my application Within the project root folder, I have created a folder named test with a file named index.js module.exports = { myFunction: function() { console.log('ok'); ...

The object in question does not have the capability to support the "live" property or method

I've encountered an issue in IE related to a script error with the live method. The problem arises when testing on the website URL @ Despite trying multiple solutions and various fixes, I haven't been able to resolve the issue yet. Any assistanc ...

Turn off the feature that highlights links

Hi there! I'm curious to know if it's feasible to remove the highlighting effect when clicking on a link. I'd like the link to function more like an image, without the appearance of a highlighting box upon clicking. ...

Is it appropriate to invoke componentWillReceiveProps within the componentWillMount lifecycle method?

I'm new to working with react and I have a question. How can I call a function within another function? For example, I want to call componentWillReceiveProps inside of componentWillMount. How can I achieve this? import React from 'react' cl ...

Update the user information by using its unique identifier at a specific location

I am currently working on editing user data with a specific MongoDB instance. When I click on the user's details, a modal popup appears with an update button below it. My goal is to have the user data updated when this button is clicked for the partic ...

The issue arises when HTML tables with unique IDs are all displaying the same value from an AJAX callback

This situation has been incredibly frustrating for me!! Let me explain what's going on - I have a callback data coming from my AJAX post method that needs to be passed to seven different HTML tables. Although I successfully received the data from the ...

"Using the push method in JavaScript allows for the combination of arrays rather than

Looking to retrieve data from an API and store it in an array. When I assign the value directly using '=', the desired data is displayed. However, when attempting to add elements using 'push', they are added as another array. Below is ...

Error encountered while compiling a method within a .vue component due to a syntax issue

I have been closely following a tutorial on Vue.js app development from this link. The guide instructed me to add a login() function in the block of the Login.vue file. Here is the snippet of code provided: login() { fb.auth.signInWithEmailAndPa ...

What is the best way to implement a calendar view for selecting time periods on an HTML page?

Is there a way to implement a calendar view when clicking on the input box within an HTML page? I am looking to create a function where users can select a time period to generate a file. How can this be accomplished? If possible, could you share some samp ...

Selecting elements with special characters in their IDs using JQuery can be done

I am facing an issue with this HTML code where the Id includes special characters: <input type="text" id="e09b989c-7201-4b7e-8fd4-a2309db6d356|b5d9746a-7f7c-432d-a4ef-a5e0011d989d|IssueDate" name="e09b989c-7201-4b7e-8fd4-a2309db6d356|b5d9746a-7f7c-432d ...

handsontable - personalized HTML button placed at the beginning of each row that performs an action when clicked

In my handsontable, I have successfully added a custom button in the 2nd column. When this button is clicked, I want to send the data of that particular row to the server for processing. I have written an onClick function for the button, but I am struggli ...

Showing chosen checkbox information using jQuery ajax

When a user enters more than 3 characters into a text box for live search, results are displayed from the database via an AJAX response as checkboxes with names. I want selected checkboxes to display in a separate div below so users can search multiple dat ...

Unable to retrieve the JSON response sent by the REST API within an HTML page

My ajax function is unable to properly receive the JSON content generated by a REST API. The REST API successfully creates the JSON data, but when passed to my ajax function, it does not work as expected. function loadJsonData(){ var dropDownValue = ...

Incorporating words into the core of a pie chart: Highcharts

Is there a way to display the total value in the center of a pie chart? I've attempted some methods but haven't been successful. The goal is to show the sum of all y values. Input export const data = [ { y: 15, name: "Category 1&q ...

"Utilizing jQuery to Trigger CSS3 Transform Animation Replays After a Set Number of Iterations

I currently have an animated image set up like this: <div class="image_for_sping"> <img src="/anyimage.png"> </div> The image has a style attribute added by jQuery which contains the following animation properties: animation: spin3 ...

What steps can be taken to address the problem of the "incorrect referer value" when sending a form?

I am experiencing some challenges with accessing a certain forum as my IP address has been blocked. It seems that another user may have violated the rules and their ISP or IP address is similar to mine, causing me to be impacted. I attempted to post comm ...