Issue with the dependency between THREE.ShaderPass and THREE.EffectComposer in Three.js

I am relatively new to three.js and trying to implement a bloom effect on my object using UnrealBloomPass.js through the effect composer function. However, I have encountered some errors related to THREE.ShaderPass in the Effect Composer that I am unable to resolve due to my lack of experience. Can anyone offer assistance?

Below is my code:

// Setting up Canvas and Renderer
const canvas = document.querySelector("#canv") 
const width = window.innerWidth;
const height = window.innerHeight;
const renderer = new THREE.WebGLRenderer({canvas});
renderer.setSize( width, height );
renderer.setClearColor( 0x111, 1);
const fov = 100;
const aspect = width/height;
const near = 0.1;
const far = 5;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.z = 2;  
const scene = new THREE.Scene();

// Loading Geometry
const loader = new THREE.GLTFLoader().setPath("./");
loader.load("assets/model.glb", (gltf) =>{
    root1 = gltf.scene;
    scene.add(root1);
});

loader.load("assets/model2.glb", (gltf) =>{
    root2 = gltf.scene;
    scene.add(root2);
});

// Lighting Setup
const light1 = new THREE.DirectionalLight(0x404040);
const light2 = new THREE.AmbientLight(0x404040);
light1.position.y = 2;
light1.position.z = 2;
light1.intensity = 2;
light1.shadowDarkness = .1
light2.position.y = -2;
light2.position.z = -2;
light2.intensity = 2;
light2.shadowDarkness = .1
scene.add(light1);
scene.add(light2);

// Setting Controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableZoom = true;
controls.enablePan = false;
controls.maxDistance = 2;
controls.minDistance = 1.3;
controls.maxPolarAngle = 2.5;
controls.minPolarAngle = .5;
controls.enableDamping = true;
controls.update();

// Implementing Effect Composer
const composer = new THREE.EffectComposer(renderer);
composer.addPass(new THREE.RenderPass(scene, camera));
const bloomPass = new THREE.UnrealBloomPass( new THREE.Vector2( window.innerWidth, window.innerHeight ), 1.5, 0.4, 0.85 );
bloomPass.threshold = 0;
bloomPass.strength = 3;
bloomPass.radius = 1;
composer.addPass(bloomPass);

// Rendering Scene
function animate() {
    var delta = clock.getDelta();
    requestAnimationFrame( animate );
    
    root1.rotation.y += 0.002;
    root2.rotation.y += 0.0035;
    controls.update()
    // renderer.render( scene, camera );
    composer.render(delta);
}
animate();
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <canvas id="canv"></canvas>

    <script src="build/three.min.js"></script>
    <script src="build/OrbitControls.js"></script>
    <script src="build/GLTFLoader.js"></script>
    <script src="build/EffectComposer.js"></script>
    <script src="build/RenderPass.js"></script>
    <script src="build/UnrealBloomPass.js"></script>
    <script src="exp.js"></script>

</body>
</html>

Answer №1

The issue lies in the EffectComposer.js script's inability to access the necessary elements (e.g. THREE.ShaderPass).

To resolve this issue, you simply need to import the required files for the EffectComposer script into your JavaScript/HTML file so that the missing elements can be accessed. Some of these imported files may themselves require other elements (e.g. ShaderPass.js needs elements from Pass.js). Make sure to import them in the correct order so that the files requiring additional elements are read last and those needed by other scripts are read first.

Below is the modified code snippet (particularly focusing on the HTML file):

// Canvas Setup
const canvas = document.querySelector("#canv") 
    const width = window.innerWidth;
    const height = window.innerHeight;
const renderer = new THREE.WebGLRenderer({canvas});
renderer.setSize( width, height );
renderer.setClearColor( 0x0c023d, 1);
    const fov = 12;
    const aspect = width/height;
    const near = 0.1;
    const far = 20;
// Rest of the code continues...
<!DOCTYPE html> 
<html lang="en">
<head>
    // Head content...
</head>
<body>
    
    <canvas id="canv"></canvas>
    
    <script src="build/three.min.js"></script>
    <!-- Required for ShaderPass -->
    <script src="build/Pass.js"></script>
    <!-- Both scripts required for EffectComposer  -->
    <script src="build/ShaderPass.js"></script>
    <script src="build/CopyShader.js"></script>
    <!-- Required for UnrealBloomPass -->
    <script src="build/LuminosityHighPassShader.js"></script>
    <!-- Main functions needed -->
    <script src="build/OrbitControls.js"></script>
    <script src="build/GLTFLoader.js"></script>
    <script src="build/EffectComposer.js"></script>
    <script src="build/RenderPass.js"></script>
    <script src="build/UnrealBloomPass.js"></script>
    <!-- Main three.js script -->
    <script src="exp.js"></script>

</body>
</html>

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

Issue with Github actions: Failure in mark-compacts process due to ineffective operation near heap limit causing allocation failure - running out of memory in

Whenever I try to build my library files, I keep encountering an out of memory error. In my local setup, I was able to resolve this problem by executing the command export NODE_OPTIONS="--max-old-space-size=8192". Unfortunately, no matter how mu ...

Create a new variable to activate a function within the parent component in Vue

I want to invoke a function in the parent component from its child component. In my Vue project, I have designed a reusable component that can be used across multiple pages. After the function is executed in this component, I aim to call a specific functi ...

I am looking to implement a required alert for a radio button that mimics HTML5. How can

When using the input type text, adding the required attribute prevents the form from submitting and prompts the browser to focus on the required field with an alert instructing to fill it out. On the other hand, when applying the required attribute to t ...

Ways of extracting specific information from a JSON file with the help of jQuery

I am currently attempting to parse a JSON file that is stored locally on my system using jQuery. I am specifically interested in retrieving certain data from the file, which is structured like this: {"statements":[{"subject":{"uriString":"A","localNameIdx ...

Tips for adjusting the language settings on a date picker

Is there a way to change the language from English to French when selecting a month? I believe I need to configure something in the core.module.ts. How can I achieve this? https://i.sstatic.net/Cpl08.png @NgModule({ declarations: [], imports: [ Co ...

Incorporate an external object not native to the Angular framework within a factory

We're in the midst of a debate and I'm hoping you can help us reach an agreement. Imagine I have a basic factory set up like this: angular.module('myModule', []) .factory('Fact', function() { var Fact = function() { ...

The exported JSON file from the editor is malfunctioning

After importing the obj file into the editor and exporting the geometry to a js file, I encountered an error in Firefox debug saying "Type Error: t1 is undefined". Interestingly, when I changed the model to the .js file exported by MaxExporter with both "E ...

Is there a way to implement a one-second delay before displaying a drop-down hover menu?

I've been trying to use the setTimeout function, but so far I haven't been able to figure out the correct way to do it. Any ideas on what I might be missing? JSFIDDLE $('.drop-down').click(function() { $(this).hide(); }); $(&apo ...

Error: The property 'rows' cannot be read because it is undefined

While working through the steps outlined in Getting started with Postgres in your React app, specifically when processing and exporting the getMerchants, createMerchant, and deleteMerchant functions, I encountered an error that says: "TypeError: Cannot rea ...

Jasmine - verifying variable invocation in test function

As part of my testing process, I am exploring a scenario where a service method is called using a local variable within an angular controller. In this particular case, when the items array is empty, a modal for creating a new item will be triggered via th ...

Switch the style sheets after the content

How can I create a toggle effect on the :after property, switching between + and - each time the link is clicked? I've attempted to achieve this using JavaScript, CSS, and HTML, but the code only changes + to - and doesn't switch back when the l ...

Steps to cancel an AJAX call in C# .NET:

In the ajax.cs class, I am calling methods from the javascript side. To register this on the default.aspx page load, I did the following: Ajax.Utility.RegisterTypeForAjax(typeof(ajax)); Occasionally, some methods take a long time to execute. In such case ...

Trouble arises when attempting to use JavaScript, JSP, and JSON in conjunction with

I am in the process of creating a client-server application where I send a request from the client to the server using a JSON object for registration. However, despite receiving a JSON response with an "OK" field as expected, the client keeps triggering th ...

Arrows indicating the direction of a LineString feature in Openlayers 4

I'm attempting to create a LineString with arrows at the end of each line to indicate the direction of the route. I found an example on the official site: The example code in the link creates arrows through user drawing, but I need arrows for a give ...

Executing a mysql select query with the help of Node.js

I'm currently working on a code to check if a player is already registered in a tournament by using their Discord Tag and the Tournament ID. However, the query I created is not returning any results. Thank you. function verifyPlayerNotRegistered(tourn ...

Updating ngRepeat Array Data in AngularJS using Events

I'm currently in the process of transitioning my jQuery application to AngularJS. One of the tasks I need to accomplish involves updating the Data Array whenever a scroll event occurs. How can I achieve this? Here is the existing jQuery code at Plun ...

Securing Email and Password Data in Cypress Tests

Greetings! I trust everyone is in good spirits. My dilemma lies in the fact that I am hesitant to include email and passwords in version control. I am considering using environment variables in my cypress tests and utilizing secrets for runtime value pro ...

Guide to creating a function that assigns a value with the assignment operator in jQuery

Consider the function below: function ObjDataSet () { this.header = ""; this.dataIdx = 0; this.DataRows = []; this.CountRow = 0; } .... ObjDataSet.prototype.NameString = function(argIdx, argName, argValue) { var arrObj = this.DataRows[argIdx-1]; ...

Is it possible to find a match by searching through multiple arrays for a corresponding variable name?

Hi there, I'm currently working on a function that triggers an alert if the name of an array of images matches the data attribute of the selected element. This is just a test phase before proceeding with my main project, but I've hit a roadblock. ...

What is the process for transferring a JavaScript file (containing multiple functions) from the server to the client side?

I am looking for a way to hide the server-side functionality from the end user in order to maintain privacy. I have not been able to find a solution for this issue thus far. Currently, I have a .js file containing functions that are used within an html5 f ...