Turn off selective bloom in ThreeJS for the background of the scene

I am using ThreeJS to create a selective bloom effect to showcase glowing parts on an object using an emissive map as a filter. I recently enabled my environment map and noticed that the bloom is affecting the appearance of the scene.background.

This is the code I use to generate the environment map:

function createEnvironment( hdr ) {
    new RGBELoader().setDataType( THREE.UnsignedByteType ).load( hdr, function ( texture ) {        
    
        const pmremGenerator = new THREE.PMREMGenerator( renderer )
        pmremGenerator.compileEquirectangularShader()
        
        const envMap = pmremGenerator.fromEquirectangular( texture ).texture
        scene.background = texture;
        scene.environment = envMap
        
        texture.dispose()
        pmremGenerator.dispose()
    } );
}

Currently, the environment map is glowing excessively. I have tried to find a solution, but since the scene.background does not utilize a material, I am unable to resolve the issue.

I am currently at a standstill. How can I prevent the selective bloom from affecting the scene.background?

Here is the code for the selective bloom effect I am using

body{
  overflow: hidden;
  margin: 0;
}
<script type="x-shader/x-vertex" id="vertexshader">
  varying vec2 vUv;
  void main() {
    vUv = uv;
    gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  }
</script>
<script type="x-shader/x-fragment" id="fragmentshader">
  uniform sampler2D baseTexture;
  uniform sampler2D bloomTexture;
  varying vec2 vUv;
  void main() {
    gl_FragColor = ( texture2D( baseTexture, vUv ) + vec4( 1.0 ) * texture2D( bloomTexture, vUv ) );
  }
</script>
<script type="module">
// JavaScript code here
</script>

Answer №1

If you want to enhance your render loop, consider using scene.background instead of renderer.setClearColor.

Creating different scene backgrounds

let backgroundOptions = {
   option1: new THREE.Color(0x000000),
   option2: new THREE.Color(0x404040) // You can also use a cubetexture, for instance.
}

Then in your render loop:

renderer.setAnimationLoop( _ => {
    
  scene.background = backgroundOptions.option1; // Should be pure black
  uniforms.globalBloom.value = 1;
  
  bloomComposer.render();
  
  scene.background = backgroundOptions.option2; // Display anything else you desire
  uniforms.globalBloom.value = 0;
  
  finalComposer.render();
})

PS: This code is purely theoretical and has not been tested. :)

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

Execute the code from https://github.com/akella/webgl-mouseover-effects on your local environment

I'm looking to create an image hover effect similar to the one on the website . The closest example I've found so far is on https://github.com/akella/webgl-mouseover-effects. I've downloaded and extracted the files, but when I try to run ...

Can jQuery help me identify which <input type='image> was clicked within a form containing several submit images?

Consider this hypothetical scenario: <form> <input type="text" name="some_name" value="val" id="some_name"> <input type="image" value="action" alt="Add To Cart" name="add" src="/images/submit.gif"> <input type="image" value="act ...

Issue with Bootstrap checkbox/switch

Trying to determine the status of a checkbox - whether it's checked or not. The default state is unchecked, but I need to perform actions when it is checked and also when it gets unchecked. Currently using bootstrap 5.3.0 alpha html <div clas ...

Using AngularJS and ServiceStack webservice, perform the "get('url')" operation

I'm completely new to AngularJS and I'm attempting to retrieve some JSON items from a webservice I quickly set up using ServiceStack. When I enter the URL in the browser, I can see the JSON object without any issues. However, for some reason Angu ...

Encountering Issues with Formatting InnerHtml Text using RegEx

Technology: React.js I have been working on a custom function in JavaScript to highlight specific words within a code block. The function seems to be functioning correctly, but the highlighting isn't staying after the function is completed. Any ideas ...

Fulfill the promise in AngularJS and assign it to a different factory

Presenting my factory below: .factory('UserData', ['User', '$q', function(User, $q) { var deferred = $q.defer(); return { user: null, get: function() { var _this = this; _this. ...

Is there a way to make this AngularJS service wait until it has a value before returning it?

Multiple controllers call a service that loads data into an object named categories: .service('DataService', ['$http', '$q', function($http, $q) { var categories = {}; // Public API. return({ setCategory ...

When invoking a Javascript function within an onclick event, make sure to avoid creating a new

There's a function named save in my code that is responsible for saving data from a timer. The Model class looks like this: var Schema = mongoose.Schema; var Timer = new Schema({ Time: {type: Number}, Desc: {type: String}, Doc: {type: S ...

The variable that was posted is not being successfully transferred within the query

There seems to be an issue with retrieving data from the ajax call in ajaxcall.php and assigning it to $place = $_POST['place']; in listplace.php. Despite this problem, the code runs smoothly otherwise. I've tried numerous times to get the ...

The functionality of a generated button has been compromised

My goal is to create a webshop that doesn't rely on MySQL or any database, but instead reads items from JSON and stores them in LocalStorage. However, I've encountered an issue where the functionality of buttons gets lost after using AJAX to gene ...

Jquery form submission is failing, and a JavaScript warning appears in the console stating that 'body.scrollLeft is deprecated in strict mode' instead

In my JavaScript file, I've written the following code: function PS_SL_HandleEvent() { $(document).ready(function() { $('#form').removeAttr('onsubmit').submit(function(e) { if(acceptCGV()) { ...

What are the steps to implement server side routing using react-router 2 and expressjs?

If you're looking to delve into server side rendering with react-router, the documentation linked here might fall short in providing a comprehensive understanding. In particular, it may not offer enough insights on how to leverage react-router 2 for r ...

Retrieve information from an SQL database using user input in a Classic ASP form without the need to refresh the page

Currently, I am in the process of incorporating a new function into an existing Classic ASP system. This feature involves allowing users to scan a barcode that will automatically populate a text field within a form inside a bootstrap modal. The scanner has ...

Every time I launch my express application, I encounter this error message

Encountering a type error with Express Router middleware. See below for the code snippets and error details. Any assistance is appreciated? The application is functioning properly, but when attempting to access the URL in the browser, the following error ...

Tips for eliminating fade effect during hover in networkD3 chart in R

Currently, I have been exploring the usage examples of networkd3 in r I am curious if there is a way to eliminate the hover effect where everything else fades when hovering over a specific node in the graph. For reference, check out "Interacting with igra ...

Is the Router.refresh() function failing to refresh within the next 13 seconds?

'use client'; import { useRouter } from "next/navigation"; export default function Todo({ todo }) { const router = useRouter(); return ( <> <li key={todo.id}> <input type=&apo ...

Amazon Lex: Transforming Speech to Text with Audio Technology

Is there a JavaScript SDK provided by Amazon for converting audio files to text using Amazon Lex? I am currently working on a Node.js application and would like to achieve this functionality. ...

Mastering the art of throwing and managing custom errors from the server to the client side within Next.js

I'm in the process of developing a Next.js application and I am faced with the challenge of transmitting customized error messages from the server to the client side while utilizing Next JS new server-side actions. Although my server-side code is func ...

The AJAX call for fetching logged in users is coming back as undefined

Hey there! I'm currently diving into the world of AJAX and am working on enhancing the user experience on my admin system. One thing I want to achieve is to update information like online users, messages, tasks, etc. every 30 seconds or so (for testin ...

Is there a way to incorporate content onto a webpage solely through the use of CSS and JavaScript?

Currently, I am in the process of developing a real estate website that utilizes a paid service for displaying real estate listings. The unique aspect of this setup is that my website operates as a subdomain of the main listings website, resulting in a URL ...