The problem with transparency in Three.js ParticleSystem

Currently, in my project using Three.js, I have a ParticleSystem where each particle can vary in transparency and color.

Here is the code snippet:

var shaderMaterial = new THREE.ShaderMaterial({
    uniforms: customUniforms,
    attributes: customAttributes,
    vertexShader: document.getElementById('vertexshader').textContent,
    fragmentShader: document.getElementById('fragmentshader').textContent,
    transparent: true,
    alphaTest: 0.5
});


particles = new THREE.ParticleSystem(geometry, shaderMaterial);
particles.dynamic = true;

For the vertex shader:

attribute float size;
attribute vec3 color;

varying vec3 vColor;

void main() {

    vColor = color;

    vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );

    gl_PointSize = size * ( 300.0 / length( mvPosition.xyz ) );

    gl_Position = projectionMatrix * mvPosition;

}

And for the fragment shader:

uniform sampler2D texture;
uniform float alpha;
varying vec3 vColor;

void main() {
    gl_FragColor = vec4(vColor, 100);
    vec4 textureColor =  texture2D( texture, gl_PointCoord );
    gl_FragColor = gl_FragColor *  textureColor;
    gl_FragColor.a = alpha;
}

I face an issue with setting the alpha level for the provided texture. When I include gl_FragColor.a = alpha, the texture appears as a circle within a black square. The alpha level works correctly but I want only the circle without the square. If I omit the alpha setting, the square does not show up. How can I correctly set the alpha for the texture?

Answer №1

Check out this informative link regarding adjusting the opacity of individual particles with three.js: three.js - Adjusting opacity of individual particles

If you visit the provided page, you will come across a jsfiddle example that utilizes ShaderMaterial for ParticleSystem and demonstrates variable alpha values: http://jsfiddle.net/yfSwK/27/

Furthermore, it is recommended to make slight modifications to the fragment shader. Ensure that gl_FragColor is used as a write-only variable rather than a read-from variable:

vec4 col = vec4(vColor, 100);
vec4 tex = texture2D( texture, gl_PointCoord );
gl_FragColor = vec4( (col*tex).rgb, alpha );

To condense it into one line:

gl_FragColor = vec4( (vec4(vColor, 100) *  texture2D( texture, gl_PointCoord )).rgb, alpha);

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

Display components created for children above the components created for parents

I am looking to render a component with a different route without it covering the entire page. For example, let's say I click on a question from a list on Stack Overflow, and then I want an animated modal to appear from right to left without changing ...

AngularJS - A Guide to Detecting Window Resize and Orientation Changes in Landscape and Portrait Modes

I have attempted various suggestions from Stack Overflow, but none of them have been successful. Essentially, I am seeking to update (re-render) a custom directive whenever the window size changes. This is my code: angular.module('dynamicSlideBox&ap ...

Use JavaScript to convert only the initial letter to uppercase

Once again, I am in the process of learning! Apologies for the simple questions, but learning is key... Attempting to implement a trick I found online to change all letters to uppercase, I am now trying to adjust it to only capitalize the first letters. T ...

Updating the values of parent components in Vue.js 3 has been discovered to not function properly with composite API

Here is a Vue component I have created using PrimeVue: <template lang="pug"> Dialog(:visible="dShow" :modal="true" :draggable="false" header="My Dialog" :style="{ width: '50vw' }" ...

Adding color dynamically to text within ion-card based on a regex pattern

My goal is to enhance the appearance of certain text elements by wrapping them in a span tag whenever a # or a @ symbol is detected, creating the look of usernames and hashtags on Twitter. Below is the code I am currently using: TS FILE: ngOnInit(): void ...

Eliminate any untagged text on the page

Is there a way to remove dynamic text that appears on an HTML page without any surrounding tags, using jQuery? The text in question looks like this: <div id="Container2" class="tab default-tab" style="display: block; margin-top: -15px; overflow: auto; ...

PHP script unable to identify AJAX request as a POST method

I'm facing an issue while trying to submit a form to a PHP script. The problem arises during the validation process where it fails to recognize the request as a POST request and exits from the PHP script. Surprisingly, the AJAX request registers as su ...

Fade in and out MaterialUI text using useEffect in combination with setInterval

I have implemented a text carousel using MaterialUI's Fade component. The carousel displays text from an array provided in a prop called dataArray. To achieve the carousel effect, I am toggling the boolean value of the Fade component and updating the ...

Suggestions for placing a script under the scripts menu in Illustrator CS5.1

My script for Adobe Illustrator CS5.1 is not showing up in the scripts menu despite trying to place it in various directories such as: C:\Program Files\Adobe\Adobe Illustrator CS5.1\Presets\en_GB\Scripts\ C:\Progra ...

The function window.dispatchEvent does not function properly in browsers such as Firefox, Safari, and IE

I am facing an issue where I need to resize my browser window in order to fix a problem with IScroll caused by a promise that alters the width of a very wide element. The trouble arises because the promise modifies the width after IScroll has already loade ...

Validation check: Ensure that the value does not match any other field

Looking for a method to compare two fields and only validate them if they are not equal. This is the approach I've tried, but it's not working: yup .number() .required() .notOneOf( [FormField.houseHoldMembers as any], &ap ...

Transferring a JSON value to PHP and saving it in a MySQL database

I currently have JSON values stored in a variable called json_obj in JavaScript. My goal is to pass this data on submission and store it in a MySQL database using PHP. However, I am unsure of how to achieve this. var json_obj = [ {"id":"1","name":"mun ...

Employing jQuery to redirect to a different URL when a button is clicked

I've been experimenting with a project that involves both JQuery and AJAX. One of the features I have added is JQuery autofill. Here is the code snippet for the form: <form class="form-horizontal"> <div class="form-group"> < ...

Puppeteer - Issue with Opening Calendar Widget

Problem: Unable to interact with the calendar widget on a hotel website (). Error Message: Node is either not clickable or not an Element Steps Taken (code snippet below): const puppeteer = require('puppeteer') const fs = require('fs/promi ...

Learn the process of extracting various objects from a text file using Node.js

I have a text file that contains multiple objects written to it. Is there a way to extract all the objects as JSON from this text file? What would be the best approach? Here is an example of data in my file: {"events":[...] },{"events" ...

Tips for identifying the amount of <ul> elements contained within every div

Dealing with a large file structured in the same way, I am looking for a method to isolate a specific div, determine the number of ul's within it, and then iterate through each one to extract the value of every li element. <div class="experiment ...

Adjusting spotLight.shadowCameraFov dynamically

Help needed with changing the shadowCameraFov property of a spot light object using onChange method in Javascript: gui.add(controls, 'spotCameraFov', 30, 270).onChange( function (e) { spotLight.shadowCameraFov = e; co ...

Pass data from Action Class to ajax using JSON syntax

Currently I am facing an issue while working on fullCalendar, a jQuery plugin. I am trying to populate event details from a database when the Calendar page loads, but I seem to be stuck with a silly problem that I can't figure out. $('#calendar& ...

When text contains line breaks, it will return in the text when using Ajax/JavaScript

Everything is working well with the code, except for a minor issue of passing values back and forth between JavaScript, Ajax, and PHP. The problem arises when editing text in TinyMCE editor and saving it through JavaScript/Ajax to PHP. After adding a parag ...

Transferring user-selected values from JavaScript to a PHP file

When sending values from JavaScript to a PHP file, everything works smoothly when all the values are collected. Step1 functions perfectly as both fields are mandatory. However, in Step2, values are only sent when all the fields are selected. There are co ...