Is there a more optimal approach to preserving color in point material within Three.js while avoiding the detrimental effects of additive blending mode?

Struggling to incorporate a map into a point material with a colored texture in Three.js. Attempted using additive blending and the map attribute to create a rounded point rather than a square one, but it results in the texture color blending with the white color of the point. Is there a more effective way to blend the two elements to maintain the particle shape and texture color?

https://i.sstatic.net/qyUfT.png This is particle.png

let pointShape = new THREE.TextureLoader().load("./models/particle.png");
   
    m = new THREE.PointsMaterial({
      
      size: pointSize,
      map: pointShape,
      blending: THREE.AdditiveBlending,
      depthTest: false,
      opacity: 0.9,
      onBeforeCompile: shader => {
        shader.uniforms.tex = {value: new THREE.TextureLoader().load("./textures/earthlores.jpg")};
        shader.vertexShader = `
          varying vec2 vUv;
          ${shader.vertexShader}
        `.replace(
          `#include <begin_vertex>`,
          `#include <begin_vertex>
            vUv = uv;
          `
        );
        //console.log(shader.vertexShader);
        shader.fragmentShader = `
          uniform sampler2D tex;
          varying vec2 vUv;
          ${shader.fragmentShader}
        `.replace(
          `vec4 diffuseColor = vec4( diffuse, opacity );`,
          `
          vec3 col = texture2D(tex, vUv).rgb;
          col *= diffuse;
          vec4 diffuseColor = vec4( col, opacity );`
        );
        //console.log(shader.fragmentShader);
      }
    });
This code handles the application of the texture and particle to the point material.

The current outcome of the code presents a pale, colorless globe. https://i.sstatic.net/kLZwQ.jpg

Disabling additive blending restores the correct color but loses the desired point shape.

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

Is there a need to adjust the blending mode via custom blending, modify the shader, or perhaps explore another overlooked approach? Previous attempts with different blend modes have proven ineffective, and grasping custom blending remains a challenge. Any assistance or guidance provided would be greatly appreciated. Thank you!

Answer №1

After some exploration, I have managed to discover a solution using custom blending techniques. By utilizing the add equation and setting the alpha-factor as the source/destination for linear interpolation, I successfully combined the two elements without diluting the source color with the destination color. This technique resulted in the creation of beautifully rounded points using the provided texture.

 m = new THREE.PointsMaterial({
      
      size: pointSize,
      map: pointShape,
      blending: THREE.CustomBlending,
      blendEquation: THREE.AddEquation,
      blendSrc: THREE.SrcAlphaFactor,
      blendDst:THREE.OneMinusSrcAlphaFactor,
      depthTest: false
      });

Enjoy these delightful circular points: https://i.sstatic.net/XoF0p.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

Use Jquery to revert the chosen element back to its original base state

I have implemented a select box where selecting a value in the main select box "Domains" will reveal another select box called "Types" such as Diatoms or Flagellates. I have successfully achieved this functionality, but now I am facing an issue with resett ...

Using Ajax to implement the Modal IF functionality

Currently in the process of registering a domain, utilizing two modals in the process. Through Ajax, I have set it up so that if the response is 1, an alert stating that the domain is available is displayed. If the response is 0, an alert stating that the ...

Empty Ajax array sent to PHP

I am encountering an issue with my ajax form where the values in an array are coming out as null in my PHP response. I suspect that there may be a mistake in my .ajax call. Can anyone provide suggestions or insights on how to resolve this issue? My code sn ...

Data assigned to KendoUI chart must have keys that do not consist of numbers

When I input the following simple data into a KendoUI chart, the chart displays the data correctly. var data = [ {"state":"NY","abc":12312}, {"state":"AZ","abc":12312}, {"state":"CA","abc":12312}, ...

When accessing a method exposed in Angular2 from an external application, the binding changes are lost

In my code, I have a method that is made public and accessible through the window object. This method interacts with a Component and updates a variable in the template. However, even after changing the value of the variable, the *ngIf() directive does not ...

What causes the error when I use "use client" at the top of a component in Next.js?

Whenever I include "use client" at the top of my component, I encounter the following error: event - compiled client and server successfully in 2.5s (265 modules) wait - compiling... event - compiled client and server successfully in 932 ms (265 modules) ...

Update the jQuery script tag with new content - rewrite everything

I am facing an issue with jquery. I need to change the link to src only when "document.write" is present. Below is my code: myscript.js document.write("TEST ABCD"); test.html <html> <body> <button id="example">click me</button&g ...

I am facing a dilemma with Expressjs when it comes to managing numerous users simultaneously

I'm grappling with a conceptual dilemma as I delve into the world of building an expressjs app. My goal is to create a system where each user, upon starting the app, has their own temporary folder for storing various files. Despite my lack of experien ...

Can you explain the distinction between postMessage() and dispatchEvent() in relation to the origin policy?

Here is some code that I wrote. I tried setting the MessageEvent's origin to *, but I'm still getting an error in the console saying "Blocked a frame with origin "AAAA" from accessing a frame with origin "BBBB". Protocols, domains, and ports must ...

How do I directly display a variable in index.html using node.js?

Is there a way to retrieve user inputs from the index.html file, process them in Node.js, and then display the result back on the index.html page instead of redirecting to a new page? Form Handling with Express var express = require('express'); ...

How to eliminate textures on the top and bottom of a Three.js CylinderGeometry

Currently, I am utilizing Three.js to create a basic 3D cylinder rendering. To load textures asynchronously based on user interface interactions, I am using TextureLoader. While everything is functioning normally, my goal is to prevent these textures from ...

Retrieving relevant information using an API and displaying it in components with Next.js

I am currently working with Notion's API to retrieve a list of Places along with their corresponding Cities, and then displaying this information through various components. As someone who is relatively new to the world of JavaScript (v2023), I am fac ...

What is the best way to implement a conditional check before a directive is executed in Angular, aside from using ng-if

I am facing an issue where the directive is being executed before the ng-if directive. Is there a way to ensure that the ng-if directive is executed before the custom directive? Could I be making a mistake somewhere? Should I consider using a different ...

I encountered a ReferenceError while working on my Google Authentication code, specifically stating that the variable "id" is not defined

I've encountered an issue with authentication in my code and I'm having trouble retrieving the ID that is already created. Here's the code snippet: require('dotenv').config() const express = require("express"); const bod ...

What is the best way to display and conceal a loader in order to reveal additional posts?

How can I display a loader when the user clicks on the "load more posts" button, show it while the posts are loading, and hide it once the posts have been successfully loaded? Additionally, I want to show the loader again when the user clicks on the button ...

Clicking two times changes the background

I am facing an issue with three boxes on my website. Each box is associated with a corresponding div. When I click on any box, the div displays and the background color turns red. However, when I click on the same box again, the div disappears but the back ...

A current issue lies in the fact that node.js is failing to update

I'm currently working on creating a functionality that involves reading a CSV file and checking if the headers are correct before proceeding. Below is the code snippet: let validHeaders = false; fs.createReadStream(path) .pipe(csv.parse({headers : ...

Using JavaScript to create an array within an object and adding new elements to the array

I'm just beginning to learn programming and experimenting with React. I have a function called addComment that is triggered when a user adds a comment to a news article. At this point, I need to create a property called comments (an array) and either ...

Preventing selection of past dates with Material UI in ReactJS

I'm currently working on a date range picker using react material ui. The goal is to allow users to select a specific date and then disable all past dates from that selected date onward. How can I go about implementing this functionality in react mate ...

When using the RxJs Pipe with MAP in Angular and Ionic, it may not return a value in windows, but it works perfectly in Mac when used with HttpClient

I'm currently using RxJs Pipe with Map for a network request in my Angular/Ionic project. Interestingly, while I do receive a successful response in the network log, the MAP function fails to return any value. Notable Observations Strangely, this fu ...