Bug with transparency model texture in Three.js

I've scoured the entire internet and tried numerous methods, but to no avail. How can I fix the texture transparency bug? Please refer to the image.

https://i.sstatic.net/L4SOI.png

When viewing the 3dsmax model, everything appears fine. However, after converting it to .js format from .obj using a python script, the .png files turned transparent (since .obj does not support transparency).

Please help me find a solution to this issue. Thank you

var loader = new THREE.JSONLoader();
loader.load('tree_model.js', function(geometry, materials) {
    var material = new THREE.MeshFaceMaterial(materials);
    var object = new THREE.Mesh(geometry, material);
    object.traverse( function ( child ) {
            if ( child.material instanceof THREE.MeshPhongMaterial ) {

                // This code seems unreachable, but omitting if(..) prevents it from working

                child.material.alphaTest = 0.5;
                child.material.depthWrite = false;
                child.material.depthTest = false;
                child.material.side = THREE.BackSide;
                child.material.transparent = true;
            }
        });
        scene.add(object);
    });
});

Rendering code:

renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, premultipliedAlpha: true });

Answer №1

Make sure to configure the alphaTest property on all your materials. It is also important to set the THREE.DoubleSide for leafes and branches to prevent them from disappearing when viewed from different angles.

The code snippet you provided has some errors, consider replacing it with the following:

var loader = new THREE.JSONLoader();
loader.load('model/Elka.js', function(geometry, materials) {

    for( var i = 0; i < materials.length; i ++ ) {
        var material = materials[ i ];
        material.alphaTest = 0.5;
        material.side = THREE.DoubleSide;
        
        if ( material.name === "NorwaySpruceBark" ) {
            material.transparent = false;
        }
    }

    var material = new THREE.MeshFaceMaterial(materials);
    var object = new THREE.Mesh(geometry, material);
    scene.add(object);
});

To avoid transparency issues, ensure that the trunk is not transparent. Although it's not ideal, adjusting the material settings directly in the model can help mitigate this problem.

Edit: Note that setting alpha and premultipliedAlpha in the renderer is unnecessary in this case.

See the resulting effect here:

https://i.sstatic.net/ktwJX.png

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

What is the process of transferring information from a form to mongodb?

I have created a nodejs project with the following structure: https://i.stack.imgur.com/JiMmd.png api.js contains: const express = require('express'); const router = express.Router(); const add = require('../model/myModel'); router.g ...

The exported name of the node (class ButtonRadioGroupDirective) could not be located

Currently, I am enrolled in a course on Udemy that focuses on building an app with ASP.NET Core and Angular from scratch (). Everything was going smoothly until I encountered an issue while trying to install ngx-bootstrap. After running the command npm in ...

Sequential Bootstrap collapse

I'm struggling with the collapse feature in bootstrap. For example, I have three buttons that are supposed to collapse different content in the same target box. When I click on the first button, div #one collapses. However, when I try to open anothe ...

Using JavaScript onChange event with ModelChoiceField arguments

In my Django project, I have a Students model with various fields. I created a ModelChoiceField form allowing users to select a record from the Students table via a dropdown. forms.py: class StudentChoiceField(forms.Form): students = forms.ModelChoic ...

Utilizing JSON instead of GeoJSON with AJAX in Leaflet

I am in search of a method to utilize JSON instead of GeoJSON with leaflet, making use of AJAX. The use of JSON and AJAX is imperative for this task. I have successfully called a JSON file using AJAX. However, I am now unsure about how to effectively util ...

Can anyone help me understand how to use the Promise constructor in TypeScript es6-promise?

Attempting this: start():Promise<mongodb.Db> { return new Promise<mongodb.Db>((resolve: (value?: R | Thenable<R>) => void, reject: (error?: any) => void) => { this.db = new mongodb.Db("test", new mongodb.Server("loca ...

Receiving an 'undefined' result in an asynchronous response even with 'await' and 'then' statements implemented

I'm struggling with sending a GET request, parsing the response, and passing it to another function. It seems like I'm having difficulty dealing with the asynchronous nature of the response. I prefer to stick to using Node.js' built-in modu ...

Mandatory press for a function known as a click

After dealing with a previous question on this matter, I have encountered yet another issue. My goal is to rotate an element clockwise by 22 degrees and then back to its initial state of 0 degrees on click. The first function executes correctly, but the ...

Tips for sequentially executing URL requests in Node.JS without triggering a stack overflow

Currently, the code I am using is as follows: function DownloadPage(uri) { request(uri, function (err, res, body) { if (err) { console.log(err); } else { nextURL = FindURLBySomeLogic(body); DownloadP ...

I am experiencing issues with my Vue.js application when trying to send an HTTP POST request

I am encountering an issue in my Vuejs application while trying to send an HTTP POST request to my server. The error message that keeps popping up in the console is: TypeError: _api__WEBPACK_IMPORTED_MODULE_0__.default.post is not a function at Object ...

Seamless transition between different camera perspectives

I'm currently working on creating seamless transitions between different camera viewpoints by using cubes to represent each perspective. The transition is functional for the first click, but subsequent clicks abruptly jump to the designated cube witho ...

Perform grouping and reducing operations on an array of objects using a pointfree syntax

Recently delved into the world of Ramda and am on a quest to discover a pointfree method for reducing an array of objects. Behold, the array of objects : const someObj = [ { name: 'A', city: 1, other: { p ...

What is the best way to prevent jQuery from adding a unique identifier to scripts that are loaded through ajax requests?

Recently, I've been utilizing jquery's ajax functions to fetch a page fragment and showcase it in a section of an existing page - this particular fragment contains html along with references to external javascript files. The flow of the program ...

Diverse Range of Exports Available in React Component Library

I have been working on developing a component library consisting of multiple independent components. My objective is to enable users to easily import and use these components in their projects, similar to the following: import One from 'component-lib ...

The Homescreen.js file is not showing up as expected on the localhost/home page and is not displaying

Struggling to showcase the rooms on my hotel reservation website. Can't seem to crack it. Need some assistance, please. Spent a good 3 hours trying to figure this out. Snippet from My Homescreen.js import React ,{useState, useEffect, } from &apo ...

Utilize a date state variable in JSX Text based on a specific condition

My goal is to create a feature that allows users to pick a date using DateTimePickerModal. I want to display a clickable message that says "Select date" before any date is chosen, and then replace it with the selected date. While I am not certain if there ...

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

"VueJS FusionCharts come with a fully transparent background, ensuring seamless integration

Using FusionCharts with Vue 2 has been smooth sailing so far. However, I am aiming to make my charts have a completely transparent background to allow the underlying component's background color to shine through. Despite experimenting with different v ...

How can I read "binary" characters from a file using JavaScript?

When I mention binary, I'm referring to ghjkl54╞←‼╝454┴ rather than 10101110. I am interested in implementing a tilemap loading functionality in JavaScript without having to redo my map editor, which is coded in Java and exports maps as bin ...

Tips for accessing the attribute of a JSON object in JavaScript that includes the '#' symbol in the key

When I receive the JSON object from the last.fm API, it looks like this: { "artists": { "artist": { "name": "Daft Punk", "playcount": "494599", "listeners": "106101", "mbid": "056e4f3e-d505-4dad- ...