Changing the depth buffer in a Three.js Shader Material

While working with Three js, I have implemented a vertex shader to animate a large geometry.

Additionally, I have incorporated a Depth of Field effect into the output. However, I am encountering an issue where the Depth of Field effect does not seem to recognize the altered positioning from my vertex shader. It is behaving as if the geometry is still in its original position.

Is there a way to update the depth information in my shader/material to ensure that the DOF functions correctly? I have explored the depthWrite property in THREE.Material, but it does not appear to be the solution...

For reference, the depth of field pass is executed as follows:

renderer.render( this.originalScene, this.originalCamera, this.rtTextureColor, true );

this.originalScene.overrideMaterial = this.material_depth;
renderer.render( this.originalScene, this.originalCamera, this.rtTextureDepth, true );

The rtTextureColor and rtTextureDepth are both WebGLRenderTargets. Oddly enough, rtTextureColor appears to be correct, whereas rtTextureDepth is not.

Here is the vertex shader I am working with:

int sphereIndex = int(floor(position.x/10.));

float displacementVal = displacement[sphereIndex].w;
vec3 rotationDisplacement = displacement[sphereIndex].xyz;

vNormal = normalize( normalMatrix * normal );
vec3 vNormel = normalize( normalMatrix * viewVector );
intensity = abs(pow( c - dot(vNormal, vNormel), p ));

float xVal = (displacementVal*orbitMultiplier) * sin(timeValue*rotationDisplacement.x);
float yVal = (displacementVal*orbitMultiplier) * cos(timeValue*rotationDisplacement.y);
float zVal = 0;

vec3 rotatePosition = vec3(xVal,yVal,zVal);

vec3 newPos = (position-vec3((10.*floor(position.x/10.)),0,0))+rotatePosition;
vec4 mvPosition;
mvPosition = (modelViewMatrix *  vec4(newPos,1));

vViewPosition = -mvPosition.xyz;
vec4 p = projectionMatrix * mvPosition;
gl_Position = p;

Answer №1

, the scene override material was set before rendering into the rtTextureDepth, causing the custom vertex shader to not be utilized by the renderer. The scene override material in this case is a THREE.MeshDepthMaterial, which comes with its own vertex shader. To resolve this issue, one suggestion is to create a THREE.ShaderMaterial that mimics the behavior of THREE.MeshDepthMaterial but incorporates your custom vertex shader. Though tweaking built-in shaders can be complex, you can start with a code snippet like this: var depthShader = THREE.ShaderLib['depth']; var uniforms = THREE.UniformsUtils.clone(depthShader.uniforms); var material = new THREE.ShaderMaterial({ uniforms: uniforms, vertexShader: /* your custom vertex shader */, fragmentShader: depthShader.fragmentShader }); Make sure to include the necessary uniforms for your custom vertex shader and configure the uniforms for the built-in depth shaders as well. You can refer to the MeshDepthMaterial section in WebGLRenderer.js within the three.js source for additional guidance.

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

JavaScript - Implementing dependency injection with promises

I am facing a challenge in passing an object around in JavaScript, with some jQuery involved. :) My goal is to execute the FlappyBarHelper.getUserPropertyCount() method once the promise.success function has completed. I attempted to pass this.FlappyBarHel ...

Create a variety of charts using the same data object in Highcharts

Is it possible to display two (or three) different charts on the same page using Highcharts without creating separate div elements and only utilizing one object? I have created a screenshot to illustrate my question: You can view the code example on JSFi ...

The Magnificent jQuery Widget Factory's _trigger Instance

When utilizing the _trigger function to initiate events, I often come across a recurring issue that I struggle to fully comprehend. The problem arises when there are multiple instances of my widget on the same page. In such cases, the most recently instan ...

Implementing mouse event listeners on dynamically created elements in Vue.js

I've been curious if anyone has attempted something similar to the following code snippet (created within a Vue.js method): for (let i = 0; i < this.items.length; i++) { let bar = document.createElement('div'); bar.className = this ...

Transmit a data point from JavaScript to PHP

I am looking to transfer the address value to a different PHP page <script> var userAddress = place.formatted_address; document.getElementById('af').innerHTML = userAddress; </script> ...

Activate Click, or Pop-up Box

I've been attempting to log in to the Udemy site using JavaScript, but I'm having trouble triggering the click action on the "log in" link. Unfortunately, the .click() method doesn't seem to be working when I try to select the element. The l ...

The $http.get in AngularJS is causing a problem by returning undefined and $http() is not being recognized

I am currently working on an app that is designed to load and display data dynamically from a database using AngularJS. However, I have been encountering errors when trying to access my API using both $http() and $http.get(). The specific errors I am recei ...

Verify if the arguments of a Discord bot command are in accordance with the format outlined in the commands configuration

I am looking to create a script that verifies if the arguments given for a command align with what the command expects them to be. For instance; When using the config command, the first argument should be either show, set, or reset Additionally, if se ...

Exploring the functionality of arrays in Jest's cli option --testPathIgnorePatterns

Looking to exclude multiple folders, one in the src folder and another in node_modules, when using the --testPathIgnorePatterns option. Does anyone have an example of how to use this pattern effectively? I am unable to configure an array in the package.js ...

What is the best way to tailor my functions to apply only to specific objects within an array, instead of affecting them all?

I am facing an issue where my functions showMore and showLess are triggering for all objects in the array when onClick is fired. I want these functions to be called individually for each object. Both of these functions are responsible for toggling betwee ...

Is it possible to load modal content using ajax bootstrap pagination without having to refresh the main page?

Whenever I utilize the bootstrap modal and pagination for modal content, clicking the next/prev button causes the entire page, including the main window, to reload. Here are the scripting lines: $("#ajax_process_page").html("<%= escape_javascript(rend ...

What could have caused my javascript file to disappear from npm?

After creating a small library consisting of a .js file with commonly used functions, I placed it in the node_modules directory alongside my other packages. Everything seemed to be going well. A few days later, I decided to add a new package using npm ins ...

Steps to Transform String Array into Prisma Query Select Statement

I have a requirement to dynamically select Prisma columns based on client input: ['id', 'createdAt', 'updatedAt', 'Order.id', 'Order.Item.id', 'Order.Item.desc'] The desired format for selection ...

The fetch function consistently executes the then() block, regardless of any errors, resulting in an undefined response

I'm encountering an issue where the catch block doesn't seem to be firing in my code, even though I am throwing a new error. However, the then block with an undefined response always fires. Can anyone help me understand why this is happening? Ca ...

Creating a primary php file in Apache without the use of SQL or any database: is it possible?

Forgive me if this comes across as rude, but I'm struggling to grasp the concept of apache, PHP, and servers in general. To help myself understand better, I want to create a very basic website that assigns an ephemeral ID to each user (not a session). ...

Transform the string response in a websocket message into JSON format

When receiving a websocket message, I often encounter a response in the form of a string like this: odds.1:[{"id":1,"marketType":10,"name":"Double chance","status":"HandedOver","specifiers":"","Outcomes":[]},{"id":2,"marketType":11,"name":"Draw no bet", ...

Utilizing AngularJS within a Captive Network Assistant (WISPr) on iOS and MacOS devices

In my past experiences with projects, it has been observed that Apple's Captive Network Assistant (also known as WISPr client) operates with a restricted browser. For further information, you can refer to How can I debug the browser in Captive Portal? ...

What is the best way to incorporate modal window parameters into this code snippet?

JavaScript function: function loadBlockEditor(block, username) { var blockInfo = $.ajax({ url: "in/GameElement/BlockEditor.php", type: "GET", data: 'block=' + block + '&nick=' + username, dataType: "html" }); b ...

ThreeJS - Visualizing mesh as wireframe in object structure

I'm currently working on displaying a wireframe of an object file that has been loaded using OBJLoader(). Below is the snippet of code I am implementing: var loader = new THREE.OBJLoader(); loader.load( filePath, function ( object ) { objec ...

"Sliding through pictures with Bootstrap carousel placed beneath the

Currently, I am working on a website that requires a background image, although I personally do not prefer it. The client's preference is to have the navbar transparent so that the background image shows through it. Now, I would like to incorporate a ...