three.js - overlapping surfaces with translucent materials

I am attempting to create three swoosh graphics that revolve around a central atom. I have the swoosh graphic saved as a PNG file, and initially thought about using three canvas elements to position the images and rotate them in place.

However, when I implement this with the WebGL renderer, I encounter strange invisible artifacts where the canvases overlap before even starting their rotation.

WebGL renderer issue

Using the Canvas renderer, the elements seem to overlap correctly; however, with WebGL, they don’t align well, resulting in oddly distorted shapes.

I would appreciate any assistance on this matter.

EDIT: Here’s a jsfiddle link for reference: Check it out here!

The key functions involved are generateImg and rotateNode, as shown below:

function generateImg(xpos, ypos, zpos, xrot, yrot, zrot) {
    // Create a canvas element
    var imageObj = new Image();
    imageObj.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAACXBIWXMAABcRAAAXEQHKJvM...";
    
    var canvas2 = document.createElement('canvas'); 
    canvas2.width = 512;
    canvas2.height = 512;
    var context2 = canvas2.getContext('2d');
    var texture2 = new THREE.Texture(canvas2);

    imageObj.onload = function() {  
        context2.drawImage(imageObj, 0, 0);
        if (texture2) {
            texture2.needsUpdate = true;
        }
    };

    var material2 = new THREE.MeshBasicMaterial({ map: texture2, side: THREE.DoubleSide });
    material2.transparent = true;
    material2.opacity = 0.8;

    var mesh2 = new THREE.Mesh(new THREE.PlaneGeometry(canvas2.width, canvas2.height), material2);
    mesh2.position.set(xpos, ypos, zpos);
    mesh2.rotation.set(xrot, yrot, zrot);
    mesh2.name = "swoosh";
    scene.add(mesh2);
}

function rotateNodes() {
    for (let i = scene.children.length - 1; i > 0; i--) {
        if (scene.children[i].name === "swoosh") {
            scene.children[i].rotation.z -= .05;
        }
    }
}

function animate() {
    requestAnimationFrame(animate);
    if (stats) {
        stats.update();
    }
    if (world === 'HOME') {
        rotateNodes();
    }
    render();
}

Answer №1

Exploring the concept of intersecting planes with transparent materials may push the boundaries of three.js / WebGL. To delve deeper into this topic, visit this resource.

I made some improvements to your fiddle and you can observe how I successfully implemented it here

To achieve this, I utilized alphaTest: 0.5 on the material featuring the texture.

var material = new THREE.MeshBasicMaterial({
    map: texture,
    side: THREE.DoubleSide,
    alphaTest: 0.5, 
    transparent: true
});

This effective approach was recommended by @mrdoob on GitHub and also discussed here on stackoverflow

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

Exploring the depths of jQuery promises

My journey into the world of promises has just begun, and I must say it's been quite intriguing. However, there are some questions that have been lingering in my mind. It seems to me that $.Deferred().promise, $.get().promise, and $.fn.promise().pro ...

How come my $scope variables are not resetting when attempting to refresh the data?

I just started working with Angular and encountered an issue while trying to implement a reload button. My webpage displays a table with data pulled from a Parse database. The table can be modified using various buttons, and after making changes, I want to ...

Drawing on HTML canvas within UV islands

I am currently working on generating a dynamic texture composed of randomized circles, with the goal of applying it to a bowtie object in three.js To accomplish this task, I have opted to utilize HTML canvas for its flexibility in creating intricate patte ...

Creating a 3D polygon in three.js using three vertices

I've been attempting to render a plane using a set of 3 vertices (shown below). Unfortunately, none of the methods I have tried - mostly sourced from SO or the official three.js forum - seem to be working for me. // example vertices const vert1 = new ...

Issue with React.js button functionality not functioning as expected

import React, { Component } from 'react'; import './App.css'; class App extends Component { constructor(props) { super(props); this.state = { items: [] } } addItem(e) { var itemArray = this.state.items; ...

How can we leverage JavaScript to create logistic regression models for datasets and derive the beta-coefficients?

Exploring Logistic Regression in JavaScript In my current project, I am looking to fit a multi-variate logistic regression model to a dataset using JavaScript. My main goal is to extract the beta-coefficients for this model. Can anyone provide guidance on ...

Guide on Resolving AJAX Filtered Product List Issue with Missing Images in Django Ecommerce Website

I'm facing an issue while implementing filter functionality in my e-commerce project. When I utilized AJAX to generate a filtered product list, all the products are visible but the images aren't showing up. urls.py: urlpatterns = [ path(&apo ...

Selecting a "non-operational" collection location on an e-commerce platform

Recently I discovered a bug on an online shopping website. It seems that by using inspect element, it was possible to alter the HTML code and change an unavailable pickup point to available. This allowed me to place an order, make a payment, and even recei ...

Connecting React.js with Socket.io for real-time communication and managing application

Hello, I am currently working on saving the response from my socket in a state via the backend. Here is a method where messages are sent to the socket: export default class Home extends Component { constructor(){ super() this.state ...

When a user clicks on a list item, a class is added to change the background color of that particular item while simultaneously removing the class from other list items

add image description hereI'm attempting to apply a class to the list item in an unordered list when clicked, while simultaneously removing the same class from the other list items. I've tried using the JavaScript logic below, but I'm not a ...

Show the MySQL query results in a pop-up alert box

I need assistance with querying a MySQL database for certain results using PHP and then displaying the result in an alert dialog box through JavaScript. I am able to connect to the database, query the data successfully, and display it on a PHP page. Howeve ...

Can data be fetched from a local port using either Javascript or ASP.NET?

I have created the code below to broadcast data in a Windows application using C#. UdpClient server = new UdpClient("127.0.0.1", 9050); string welcome = "Hello, are you there?"; data = Encoding.ASCII.GetBytes(welcome); ...

Filtering nested JSON objects of children using a specific value in Angular 8

Within the function filterchildrenByRegion(), I am receiving an API response. My goal is to eliminate objects that do not match the selected Region and return all data as it is. Example 1 - If I input '1UL Africa' into the changeRegion() functi ...

Paused momentarily to allow user input

I am currently developing a new game where the player and enemies are stored inside objects with various properties. For example, each object includes: $player->health $player->attack (which represents attack power) Additionally, there is a PHP fun ...

Vue automatically refreshes momentjs dates prior to making changes to the array

I am dealing with a situation where my child component receives data from its parent and, upon button click, sends an event to the parent via an event bus. Upon receiving the event, I trigger a method that fetches data using a Swagger client. The goal is ...

Leveraging JavaScript to unpack references within a intricate object network obtained through SignalR/Json.NET

Utilizing SignalR to send back a complex object hierarchy to my JavaScript client has proven challenging. The JSON structure produced by SignalR/Json.NET contains multiple references to the same object, resulting in a convoluted output like this: { &qu ...

Rendering with ReactDom in a SharePoint Framework application

Our current project requires us to generate a PDF file using the <div></div> elements. Most of the code I've seen renders from ReactDom.Render() instead of the render class: Take for instance React-pdf: import React from 'react&apo ...

Converting JSON to Array: A Step-by-Step Guide

Greetings, StackOverflow community! This is my inaugural question here. I believe the answer is not overly complex, but my knowledge of Javascript is quite rudimentary. Currently, I have a JQuery AJAX function that processes this JSON object: { "Users" ...

Editable span Option

One of my skills includes creating a dynamic contenteditable span within text that can expand and contract, as well as wrap around multiple lines without taking up the entire width: div { width: 100px; background: #f00; padding: 5px; } span { bo ...

Struggling with integrating jQuery and .php within a Wordpress environment

My goal is to create a user-friendly translation site at . To achieve this, I have developed a simple PHP code that utilizes an external translation API through the use of GET requests. The code functions correctly as shown below: <?php if(!empty($_GET ...