Enhance your threejs project by incorporating post-processing effects alongside a captivating static background image

Stumbling upon an impressive code snippet by Andrew Berg, I discovered a method to create volumetric lights inside threejs. The mesmerizing effect can be witnessed here: https://codepen.io/abberg/pen/pbWkjg?editors=0010. This technique involves post-processing and shaders to produce a captivating ball of light.

An intriguing query arises - how can one modify the existing code to simultaneously render a static image in the background while utilizing the composer renderer to generate the volumetric light? Presently, it seems that either task can be accomplished individually but not both concurrently.

By eliminating the render(); call within the animate function and uncommenting the lines

//renderer.render(backgroundScene,backgroundCamera);
 //renderer.render(scene,camera);

The result displays the static background image successfully, yet the vital post-processing effects responsible for the Volumetric Light are omitted. Conversely, retaining the render(); function and leaving those same lines commented out allows the view of post processing effects along with the light ball shader.

main.js

var scene, camera, renderer,
    testcube, composer;

var backgroundMesh, backgroundScene, backgroundCamera,
    backgroundComposer;

var occlusionComposer, occlusionRenderTarget, 
    occlusionBox, lightSphere, sphereUniforms;

var OCCLUSION_LAYER, DEFAULT_LAYER;

var incr;

function init(){
    
    // Your custom initialization function
    
}

// Other functions and code snippets follow

shader.js

THREE.VolumetericLightShader = {
  // Shader details go here
};

THREE.AdditiveBlendingShader = {
  // Additive blending shader definitions
};

THREE.PassThroughShader = {
    // Pass-through shader details
};

Answer №1

To start, begin by creating a THREE.Scene for the background. Modify the background property of the scene using the background texture:

backgroundScene = new THREE.Scene();
backgroundCamera = new THREE.Camera();
backgroundScene.add( backgroundCamera );

var manager = new THREE.LoadingManager();
loader = new THREE.TextureLoader( manager );
loader.setCrossOrigin("");

var backTexture = loader.load(img/background.png,
    function ( texture ) {
        var img = texture.image;
        bgWidth= img.width;
        bgHeight = img.height;
    }
);
backgroundScene.background = backTexture;

Make sure that the transparent property of the final THREE.Material (material property) in the last THREE.ShaderPass is set to true:

pass = new THREE.ShaderPass( THREE.AdditiveBlendingShader );
pass.uniforms.tAdd.value = occlusionRenderTarget.texture;
composer.addPass( pass );
pass.renderToScreen = true;
pass.material.transparent = true;

Lastly, you need to render the background scene before rendering the THREE.EffectComposer:

renderer.render(backgroundScene,backgroundCamera);

camera.layers.set(OCCLUSION_LAYER);
occlusionComposer.render();
camera.layers.set(DEFAULT_LAYER);
composer.render();


Check out the Example:

... (The rest of the code has been omitted for brevity)

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 JavaScript executor in Selenium WebDriver to interact with list items by clicking on them

I am facing a challenge where I need to gather multiple links, click on each link, and extract specific information from the website to export into Excel. My approach involves collecting all the links in one list and attempting to click on each one based ...

Automating pie charts and bar graphs with selenium: A step-by-step guide

Is there a way to automate the chart actions in Selenium using Web-driver/Java (Kendo Ui)? I am looking for a method to click on the chart segments. The graph I am working with is similar to the one found at the following link: ...

Acquire the Dynamic HTML Tag from the source code of the page

I am currently using Selenium to develop a bot that will automate a task by fetching a report from a website. I must admit, I am not well-versed in HTML or Javascript, so please forgive any misuse of terms. Here is the problem at hand: Using Python, I wri ...

Example of utilizing touch and swipe events in Three.js

Have you come across this post before? Iphone - Javascript Events...for three.js Does anyone know of a real live example of a working three.js using touch and swipe events? ...

Utilize MetroUiCSS to effortlessly integrate a sleek calendar into your AngularJS application

I'm looking to incorporate a calendar from Metro Ui CSS into my project. Here is the link to the calendar: However, I am struggling with how to activate the calendar. I have already included all necessary scripts in my index.html (3 scripts) and have ...

AngularJS dilemma

Everything seems to be working fine on my application. However, when I click the back or forward buttons on Chrome, the URL changes, but all I see is a blank screen with an empty body tag containing the ng-view attribute. I've checked the controllers ...

Is there a way to verify the custom form when the braintree PayPal checkout button is clicked?

I am seeking a solution to validate a custom PHP form when the Braintree PayPal checkout button is clicked. Currently, the form redirects to the PayPal screen if it is not properly filled out. My goal is to prevent the PayPal popup window from opening if ...

Looking for a way to update a world map image by selecting multiple checkboxes to apply a flood fill color to different countries using Mogrify

Exploring different methods to achieve my goal, I am wondering if creating a web service where users can track visited countries on a world map can be done in a simpler way than using Javascript or Ajax. The idea is for users to mark the countries they hav ...

What is the most effective method for creating a personalized select in Angular?

I am currently working with the MEAN stack (Angular 6) and exploring different methods to build a custom and reusable <select> form control that utilizes an array of strings fetched from the backend server to generate all the <option> tags. For ...

Evaluate the script based on the number of words, not the number of characters

I have encountered an issue where I am only able to count the characters of a content, but what I really need is the word length. Specifically, I want the CSS to take action when there are words of 20 characters, but not if those 20 characters consist of m ...

The Next.js Image component is not compatible with an external URL as the image source

I've been struggling to use the image component in Next.js with an external URL as the source, but I keep encountering an error. I followed the instructions in the official Next.js documentation and updated the next.config.js file, but unfortunately, ...

Scrolling causes a fade effect with the opacity value adjusting

Can someone help me with my script? jQuery(document).ready(function(){ jQuery(window).scroll(function () { var scrollTop = jQuery(window).scrollTop(); var height = jQuery(window).height(); jQuery('.background_top_dis ...

jquery logic for iterating through all elements in a select menu encountering issues

In search of a solution to iterate through all options in a dropdown list using code, comparing each to a variable. When a match is found, I aim to set that particular value as the selected item in the dropdown and then exit the loop. Here's what I&ap ...

Angular.js: automatically select default option based on ID

In my angular.js single page application, I am retrieving initial data from a rest endpoint. The data consists of a list of IDs representing saved models and a tree of options for cascading dropdowns. How can I automatically set the default path in the t ...

Switch between light and dark modes with the MUI theme toggle in the header (AppBar)

I'm currently working on implementing a feature that allows users to switch between dark and light themes in my web app. The challenge I am facing is how to ensure that this theme change functionality is available throughout the entire app, not just i ...

Tips for creating a clickable popover within a window that remains open but can be closed with an outside click

Is there a way to ensure that my popover window remains clickable inside its own area without closing, but automatically closes when clicked outside of the window? This is the code I am currently using, which is triggered by a button click: if (response. ...

Error in Node.js: Unable to access 'text' property because it is undefined

I am currently developing a messenger bot using node.js and express. In order to organize my code better, I have decided to split my index.js file into two separate files. One of them is msg.js which contains the following code: 'const express = re ...

What is the most effective way to add images to a table using JavaScript?

Is there a way to insert images into the "choicesDiv" without having to make changes to the HTML & CSS? Here is the table code: <table id="choices"> <tr> <td><div class="choicesDiv" value="1"></div></td> ...

Tips for showing variables in Console directly from the Sources panel?

As I dive into debugging front-end code in Chrome, I am encountering a question that may seem basic to experienced developers. Specifically, while exploring the Sources panel within Chrome's Dev Tools, I find myself hovering over a variable labeled _ ...

Is it possible in Javascript to show identical content every time a checkbox is clicked?

I'm facing an issue with multiple checkboxes where I want them to display the same content when clicked. Currently, when I click on one checkbox, the content appears but remains hidden until I uncheck it, preventing the next checkbox from displaying t ...