Adjust the alpha channel of buffer geometry vertex colors instead of the color property in three.js

I am currently working on a particle cloud in threejs using buffer geometry, where I connect the particles via THREE.LineSegments with a THREE.LineBasicMaterial:

https://i.sstatic.net/Iz4DL.gif

In the image above, you can see that some of the lines appear black or gray - my goal is to change them to transparent shades of white.

Here are what I believe to be the key lines of code:

var material = new THREE.LineBasicMaterial({
    vertexColors: THREE.VertexColors,
    blending: THREE.AdditiveBlending,
    color: 0xffffff,
    transparent: true,
  });

var colors = new Float32Array(segments * 3);

geometry.addAttribute(
    "color",
    new THREE.BufferAttribute(colors, 3).setDynamic(true)
); //this will allow us to set the color of the lines between particles in buffer geometry

linesMesh = new THREE.LineSegments(geometry, material);

...

animate(){
  for (var i = 0; i < particleCount; i++) { //loop through particles to create line connections
    for (var j = i + 1; j < particleCount; j++) { //check for collisions
      var dist = Math.sqrt(dx * dx + dy * dy + dz * dz); //getting positions of neighboring particles
      if (dist < effectController.minDistance) { //establish a connection
        var alpha = 1.0 - dist / effectController.minDistance; 
        colors[colorpos++] = alpha;
        
      }
    }
  }
}

Answer №1

Basic Explanation

The standard three.js shader includes RGB vertex colors with no alpha transparency component.

To view the definition of this in the code, search for USE_COLOR in https://github.com/mrdoob/three.js/blob/r118/src/renderers/webgl/WebGLProgram.js (r118).

Advanced Insight

If desired, you can create a custom shader that accepts color attributes as vec4, allowing for transparency. Understanding how the line-type material uses colors and blends them along a line will be essential. Essentially, as the creator of the shader, you have control over how the blending occurs.

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 a message collector for configuring embed fields

I'm currently working on a message collector feature that collects data to use in an embed's title, description, and color. My goal is to have each value correspond to a different aspect of the embed. However, I've encountered some challenge ...

Determine whether certain radio buttons are selected using jQuery

JavaScript $('input').change(function() { $('input:radio').prop('disabled', true); $('.answer-detail').show(); $(this).next('label').addClass('correct'); var correctAnswers = ("#answer ...

Is it possible for ResizeObserver to only observe changes in width?

Is there a way to have ResizeObserver only trigger on width changes and not on height changes? For example: const resizeObserver = new ResizeObserver(updateLayout); resizeObserver.observe(layout.current); const updateLayout = () => {/*perform action*/ ...

What could be causing errors in converting Vue.js code into a single HTML file?

I discovered a Vue.js sample project on this website and now I want to execute this code in a single HTML file using Vue.js. I attempted: <!DOCTYPE html> <html> <head> <title>My first Vue app</tite> <script type="te ...

When using Array() as a constructor in JavaScript, what type of array is generated?

What is the reason Array(42) does not function with an iterator, whereas Array (42).fill() does? ...

Internet Explorer 7, utilizing the JQuery UI Dialog feature

Using JQuery UI Dialog, I am validating a form and calling the function as follows: MessageBox('this is message', 'Error', OpenDialog()); Everything works correctly in Chrome, Firefox, IE8, and IE9; however, in IE7 only the dialog&apo ...

Firestore query failing to retrieve most recent data

I have implemented a route guard in Angular that validates a specific value in firestore before granting access to the designated route. The component is accessed only after an HTTP cloud function has finished executing. This cloud function generates an o ...

What's the best way to showcase customized text formats within select option tags?

I need to have the following text displayed inside a dropdown menu: 1 day - until Friday 18th Dec 1 10.50 2 days - until Saturday 19th Dec 2 20.50 continue .... However, it seems like there is an issue as the spaces are being removed an ...

Error! Unable to Inject ComponentFactoryResolver

Recently, I attempted to utilize ComponentFactoryResolver in order to generate dynamic Angular components. Below is the code snippet where I am injecting ComponentFactoryResolver. import { Component, ComponentFactoryResolver, OnInit, ViewChild } from "@an ...

"Dealing with sluggishness in Angular 6 when working with large tables

Could anyone offer some insights on the performance issues I am facing with Angular 6? My page contains a large table with 100 rows and 100 columns each. Despite not having any data exchange with the table, using libraries like ng-select or ng-bootstrap da ...

Implementing Navigator.replace with NavigationStateUtils in React Native + Redux: A Step-by-Step Guide

I have a question about my navigation implementation in navReducer.js. Currently, I have the logic to handle .pop and .push using NavigationStateUtils. However, I want to implement .replace functionality where it replaces the current scene with a new rou ...

Troubleshooting the lack of response in ASP.Net MVC when utilizing OfficeOpenXml for a new Excel file creation

Situation I have encountered an issue while generating an Excel file using OfficeOpenXml in C#. The generated file is not being returned to the browser. Any thoughts on why this might be happening? Code Snippets [C#] : public ActionResult ExportToExcel( ...

I attempted to utilize draggable.helper, but my intention was to include the helper in the DOM, which unfortunately caused an issue

The data sample I have been working on is located at this link: http://jsfiddle.net/aLnJG/3/ Basically, I am using draggable to create a helper which is an input box, and then when dropped, I want to append the helper to the canvas. Here is the code sni ...

Classes that are added dynamically cannot be referenced

I am facing a challenge with implementing a delete function on a page that is populated with data via an Ajax call. Prior to loading the data, my page looks like this: <span id="ShowSelectedCategories"></span> Once the Ajax call is made, I po ...

Be mindful of potential missing dependencies when utilizing event listeners and states within useEffect

Whenever I utilize the addEventListener() method alongside trying to access some state within the useEffect, I consistently face the same issue. Adding the state as a dependency is not an option, as that would result in multiple event listeners being creat ...

Redirecting CORS in Cordova: A Comprehensive Guide

My Cordova/Phonegap app is encountering an issue while trying to retrieve certain files using AJAX. The specific error message that I receive states: XMLHttpRequest cannot load https://docs.google.com/uc?export=open&id=.... Redirect from 'https ...

Incorporating the <script type="text/javascript" src="bootstrap-4.4.1/js/bootstrap.min.js"></script> file into the code

I am having trouble understanding the purpose of including 'bootstrap4.4.1/js/bootstrap.min.js' link in my HTML code within a script tag. Can someone please explain its significance? ...

Does the rendered ID of an ASPX control always appear the same in the source HTML code?

Let's say I have an aspx textbox with id="txtkms". In the HTML view source, it appears as ContentPlaceHolder1_Gridview1_txtkms_1. I'm curious if this control will always be rendered as ContentPlaceHolder1_Gridview1_txtkms_1 every time I run my as ...

Determine whether the keyCode value matches any items within a specific array

Currently, as I am in the process of creating a small typewriter application, I have encountered an issue with determining if the button pressed matches the value of specific span elements. To provide further context, there are two divs each containing sp ...

What is the best way to show an image within a div using HTML and fetching data from an API response?

I am attempting to showcase an image within a div using HTML. I utilized fetch to retrieve the JSON data from the following API: . The response contains JSON data with the image URL labeled "primaryImage". While I can display it as text, I am encounterin ...