The three.js .png image is displaying in white instead of the correct colors

Trying to render a .obj with a .png texture has caused the head to turn white in the following image:

White headed avatar

After removing the png texture, the result is as follows:

This code snippet is used to create the texture:

    <script id="vertex_shader" type="x-shader/x-vertex">

    varying vec2 vUv;

    void main() {

    vUv = uv;

   gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );

   }

  </script>

  <script id="fragment_shader" type="x-shader/x-fragment">

   uniform vec3 color;
 uniform sampler2D texture;

   varying vec2 vUv;

 void main() {

 vec4 tColor = texture2D( texture, vUv );

gl_FragColor = vec4( mix( color, tColor.rgb, tColor.a ), 1.0 );

}

var texture = THREE.ImageUtils.loadTexture('./Avatar/FaceTestTr.png'); 
texture.needsUpdate = true;
var uniforms = {
    color: { type: "c", value: new THREE.Color( 0xffffff ) }, // material is "red"
    texture: { type: "t", value: texture },
    transparent: true,
    opacity: 0.9,
};

var AvatarTextureF = new THREE.ShaderMaterial({
    uniforms: uniforms,
    vertexShader: document.getElementById( 'vertex_shader' ).textContent,
    fragmentShader  : document.getElementById( 'fragment_shader' ).textContent
});

To apply it to the object:

object.children[1].material = AvatarTextureF;

Tried different approaches, including using a normal material:

var AvatarTextureF = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('./Avatar/FaceTestTr.png'), shininess: 80, color: 0xffcc66, shading: THREE.SmoothShading, alphaMap: 0x000000} );

Resulting in the same issue.

Updated code now renders the head as invisible:

Invisible head

Shaders:

    <script type="x-shader/x-vertex" id="vertex_shader">
   #if NUM_DIR_LIGHTS > 0
   struct DirectionalLight {
     vec3 direction;
   vec3 color;
  int shadow;
   float shadowBias;
  float shadowRadius;
  vec2 shadowMapSize;
  };
 uniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ];
 #endif
 //varying vec3 color;
 void main() {
 float r = directionalLights[0].color.r;
 //color = vec3(r,0.6,0.6,0.6);
 gl_Position = projectionMatrix * modelViewMatrix * vec4(position , 1.0); }
 </script>


 <script type="x-shader/x-fragment" id="fragment_shader">
  uniform vec3 color;
 uniform sampler2D texture;

 varying vec2 vUv;
   void main() {
   vec4 tColor = texture2D( texture, vUv );
  gl_FragColor = vec4( mix( color, tColor.rgb, tColor.a ), 1.0 );
   }
 </script>

Head rendering code:

var texture = THREE.ImageUtils.loadTexture('./Avatar/FaceTestTr.png'); 
                        texture.needsUpdate = true;

                        // uniforms

                        var uniforms = THREE.UniformsUtils.merge( [
                            THREE.UniformsLib[ "lights" ],
                            {
                                color: { type: "c", value: new THREE.Color( 0xffffff ) },
                                texture: { type: "t", value: texture }
                            }



                        ] );

                        // material
                        var AvatarTextureF = new THREE.ShaderMaterial({
                            uniforms        : uniforms,
                            vertexShader    : document.getElementById( 'vertex_shader' ).textContent,
                            fragmentShader  : document.getElementById( 'fragment_shader' ).textContent,
                            //transparent: true,
                            lights: true
                        });

Answer №1

Have you applied a transparent PNG with a face overlay?

The code

gl_FragColor = vec4( mix( color, tColor.rgb, tColor.a ), 1.0 );
will display the value of color, unless tColor.a is equal to 0.0.

This means that it will appear as white, which is defined by

color: { type: "c", value: new THREE.Color( 0xffffff ) }
, except for areas with an alpha value of 1.0.

You can change the value of color from 0xffffff to any color of your choice.

Additionally, make sure to include the lighting process in your fragment shader.

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

Utilizing Angular to parse a JSON string generated with json.dumps

I am having trouble finding a solution that works for me. Currently, I am using python 3.6 (Django Rest Framework) on the server side and Angular 5 on the client side. This is the code on the server: class TypesView(APIView): def get(self,request): ...

Seamless Exploration with Google StreetView

Google uses a special effect on StreetView images to transition smoothly between them. You can see an example of this effect in the images below, with the second image showing it being applied. My question is: How can I incorporate this same effect into m ...

Using ES6 Babel with multiple package.json files

For a large project, I am looking to break it down into multiple package.json files. This way, the dependencies for each part can be clearly defined and exported as separate entities. However, my goal is to compile all of these packages using webpack and ...

The product image does not display any other images

Hey, I'm experiencing an issue and need help replicating the photo changing effect on this website: I've managed to do everything right except for the photo changing feature (1 / 2 / 3 / 4)!! Here's what I have so far: Can anyone assist m ...

Comparison between on() delegation and delegate()

When using <strong>$(document).on('click', '#target')</strong> versus <strong>$('body').delegate('click', '#target');</strong> It seems like both options achieve the desired outcome ...

There is no component factory available for the DialogDataExampleDialog. Have you ensured to include it in the @NgModule entryComponents?

Currently, I am a beginner in Angular. I recently started integrating MatDialog into my project. To do this, I followed the code provided on the official Angular documentation page https://material.angular.io/components/dialog/overview. However, upon click ...

Utilizing the body in GET requests for enhanced client-server communication

What makes url query strings better than request body values? There are distinct advantages to using url parameters, such as visibility in the address bar and the ability to save requests in the browser history. However, is there more to it? Could reques ...

When implementing fancybox within bxslider, numerous thumbnails are shown simultaneously

My issue arises when using fancybox within bxslider. Once I open an image, multiple thumbnails start repeating themselves endlessly. This problem only started occurring after adding bxslider. Any thoughts on why this might be happening? ...

What are the steps for performing projection in TypeScript?

Looking to fill up the orders array, which consists of objects of type Order. The desired output is orders=[{id:1,qt:4},{id:2, qt:2},{id:3,qt:2}]. How can I achieve this using TypeScript? I am new to this language. export class Product { constructor(publ ...

Generate a JSON structure from HTML with the help of jQuery

Issue Overview Imagine a scenario where there is a delivery of assorted candies. The delivery consists of multiple boxes, each containing various types of unique candies. Each box and candy type has its own distinct identifier. Moreover, every candy comes ...

I am experiencing issues with my $ajax request

After running the code snippet below: websiteUrl= "http://192.168.2.171/LoginAuthentication"; $.ajax({ url: 'websiteUrl', type: 'GET', success: function(response) { var title = $(response.responseText).find('a. ...

Retrieving the title value of the parent span element through a child node with the help of JavaScript or

Rebuilding the query. The HTML element structure is as follows: <li class="inner"><span class="plus" id="sidehome" title="siteHome">SiteHome</span> <ul style="display:none"> <li class="inner"> <span class="plus" id=" ...

Develop a specialized WordPress widget featuring several unique text areas for enhanced customization

class ad_widget extends WP_Widget { function __construct() { $widget_ops = array( 'classname' => 'ad-widget-container', 'description' => __( ' Ad WIDGET' ) ); parent::__construct( 'ad-widget', ...

What kind of mischief can be wreaked by a malicious individual using JavaScript?

My mind has been consumed by thoughts about the safety of my projects, especially when it comes to password recovery. On the password recovery page, users must fill out a form with valid data and complete a recaptcha test for security. To enhance user ex ...

Prevent IonContent from scrolling to the bottom or top when using Ionic framework

In my Ionic app, I have a long text page with 2 buttons that trigger the actions scrollToBottom and scrollToTop. Due to the length of the page, I have set the scroll duration to be 30 seconds. I am facing two issues here: How can I stop the scrolling ...

The input type file is not correctly inserting an image into the image tag

While I was working on a project, I had a question that got answered but now I need to find a different way to use the solution. I have created a jsFiddle to demonstrate how it currently works. You can view it here: http://jsfiddle.net/TbZzH/4/ However, w ...

Transmitting real-time updates from a lengthy asynchronous task in a Node Express Server to a React client

I am looking for a way to send progress data from a long-running async function on a node express server to a client when the client requests and waits for completion. const express = require('express'); const app = express(); const port = proces ...

Remove the icon disc background from a select element using jQuery Mobile

As I delve into building my first jQuery Mobile app using PhoneGap/Cordova, I have encountered some challenges along the way in getting the styling just right, but overall, things are going well. However, when it comes to working with forms, I hit a roadb ...

Updating individual items in the Redux state while displaying the previous version

I'm facing an issue with updating the reducer for my items (icdCode) in my array (icdCodes) within a React component. The update works only after reloading the entire component, which is not ideal. Initially, I had to deal with a duplicate key problem ...

Unveiling the enigma of unresponsive Bootstrap dropdowns

I'm working on creating a custom navigation bar using Bootstrap v5. I found the code on the Bootstrap website and copied it into my project. However, I also added some JavaScript code to enhance its functionality, but unfortunately, it's not work ...