Shader Material in Three.js causes pixelation glitches when transparency is enabled

Exploring shaders for the first time and utilizing THREE.RawShaderMaterial on multiple meshes. Encountering unusual artifacts with my simple shaders:

Vertex shader:

precision mediump float;
precision mediump int;

uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;

attribute vec3 position;
attribute vec4 color;

varying vec3 vPosition;

void main() {
    vPosition = position;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}

Fragment shader:

precision mediump float;
precision mediump int;

varying vec3 vPosition;

void main() {
    gl_FragColor.r = vPosition.x;
    gl_FragColor.g = vPosition.y;
    gl_FragColor.b = 1.0;
}

Implemented on objects created this way:

asdfobject = new THREE.Object3D();
scene.add(asdfobject);

var geometry = new THREE.SphereGeometry(1, 4, 4);
var material = new THREE.RawShaderMaterial({

    uniforms: {
        time: { type: "f", value: 1.0 }
    },
    vertexShader: document.getElementById( 'vertexShader' ).textContent,
    fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
    side: THREE.DoubleSide,
    transparent: true,
} );

for(var i = 0; i < 80; i++) {

    var mesh = new THREE.Mesh(geometry, material);
    mesh.position.set(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5);
    mesh.position.multiplyScalar(400);
    mesh.rotation.set(Math.random() * 2, Math.random() * 2, Math.random() * 2);
    mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 50;
    asdfobject.add(mesh);

}

The object colors should be smooth, but at times appear distorted and pixelated as shown in this fiddle: https://jsfiddle.net/weqqv5z5/ Especially noticeable when resizing the window.

Unable to pinpoint the exact cause, only observe that setting transparent to false (line 23 in the fiddle) prevents the glitchy effect.

Haven't tested on other devices yet, could be a graphics card issue. Operating on a 64-bit Arch Linux laptop with Intel HD 4000 graphics.

Appreciate any assistance!

Answer №1

To adjust the transparency of your fragment color, you should modify the alpha value.

gl_FragColor.a = 0.5;

You can view a live example here: https://jsfiddle.net/weqqv5z5/1/

Using three.js version r.71

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

The style of declaring functions in ECMA6 is incompatible

When parent is a jQuery element and I use the following code: parent.change(function (e) { e.preventDefault(); console.log($(this)); console.log($(this).data('tab')); }); It works perfectly fine. However, when I try using this code: ...

Display title upon hovering over image linked to mysql with php

Is there a way to display the image title when hovering over images connected to MySQL using PHP? Below is the code I have been using: <div class="galleryko"> <?php $data = mysql_query("SELECT * FROM tbl_gallery_featured")?> <div clas ...

Activate the window.print() function multiple times or trigger it more than once

I have a webpage that consists of 3 separate divs that I need to print individually, one after the other. The challenge is that the web application uses silent printing in Chrome and requires each div to be printed as a separate job. A print stylesheet won ...

Combining video.js and vue.js for seamless integration

Can anyone guide me on how to display a youtube video using a video.js player within vue.js? I'm new to integrations and not sure what it entails. Any assistance will be greatly appreciated. ...

Javascript Tool for Generating Typos

My goal with this code was to introduce typos into all words on a page. The script shuffles the letters inside each word, while keeping the first and last characters intact. However, I encountered an issue with punctuation marks such as ".", "(", ",", ")" ...

When the PHP function is invoked from JavaScript, no results are displayed

I need assistance with calling a PHP function from JavaScript. The goal is to call the PHP function and have it echo two arguments that are passed. However, the code I have written does not seem to be working as expected, as it does not print anything. Ca ...

A basic structure for a JSON array

Perhaps my next question may appear silly, but I'll ask it anyway :) Here is the structure in question: { "name": "Erjet Malaj", "first_name": "Erjet", "work": [ { "employer": { "id": "110108059015688 ...

Steps for concealing a specific field in StrongLoop's outcome

Currently, I am working on a project using strongloop to develop a web service for user login. While my code is functioning properly and producing the desired output, the issue lies in the fact that it does not hide the password. The result I am receiving ...

value of object's property set to string "null" if null

I am facing an issue with my Angular app, where I am sending an object via a PUT request to my Express server. The request's content-type is multipart/form-data. The object structure is as follows: obj = { field1 : "foo", field2 : null } Upon ...

From PHP to Javascript and back to PHP

I'm currently tackling an issue within my project: My database, utilizing PHP, provides an array containing a collection of JavaScript files that require loading. This list is stored in the $array(php) variable. My task is to extract these source fil ...

Using Node.js and React to dynamically render a different HTML file in response to a request

We are currently working on implementing AMP pages for our project. Our current solution involves detecting a query in the URL, such as: myurl.com?amp=1, and completely redrawing the page with the necessary markup. However, our server is set up to choose ...

Problem with utilizing Passport-Local while executing a Mongoose.save operation

I've been troubleshooting the following code snippet: router.post('/register', function(req, res) { User.register(new User({ username : req.body.username }), req.body.password, function(err, user) { if (err) { ...

Having trouble with my Slack Bot development due to an unexpected error

While attempting to create a Slack Bot in Node, I consistently encounter an error when running npm start in my terminal: The error seems to be located in the Vow.js file. Despite double-checking everything, I can't seem to pinpoint the issue. For gui ...

JavaScript Function that Automatically Redirects User or Updates Text upon Clicking "Submit" Button

Looking to create JavaScript functionality that directs users back to the homepage after submitting their name and email. The process should follow these steps: 1.) User clicks "Request a Brochure..." https://i.sstatic.net/KqH2G.jpg 2.) A window opens in ...

Tips for adjusting the maximum height of the column panel within the DataGrid element

I'm trying to adjust the max-height of a column panel that opens when clicking the "Columns" button in the Toolbar component used in the DataGrid. I am working with an older version of @material-ui/data-grid: "^4.0.0-alpha.8". https://i.stack.imgur.c ...

Sending BCrypt hash to a different function

I am having trouble encrypting a password using BCrypt and passing it to another function to store it in the database. An error message saying "ReferenceError: hashedPass is not defined" keeps appearing on write-to-db.js:18 The code below encrypts the pas ...

Multiple executions of Google API sign in listener

I have been working on a Vue module that allows users to add events to Google Calendar easily. Once a user signs in, there is a listener in place to call a method for adding an event to the calendar. To access the gapi functionalities required for this tas ...

What is the procedure for populating a listbox with items selected from an array?

On my aspx page, there is a listbox (techGroups) with preselected items that users can change. I also have a reset button that should restore the listbox to its original selections when clicked. However, I am encountering an issue where only the first pres ...

Navigating sidebars with jQuery Dropdowns

Currently, I am working on creating a slide-in and out navigation menu that displays icons when minimized. Upon hovering over the icons, the menu expands to reveal links. However, I am encountering an issue where I cannot get a dropdown to display when hov ...

The animation did not cause a transition to occur

After creating a search modal triggered by jQuery to add the class -open to the main parent div #search-box, I encountered an issue where the #search-box would fade in but the input did not transform as expected. I am currently investigating why this is ha ...