Using Three.js to implement a shader for blurring a geometric shape

After spending the past day diving into ThreeJS, I've hit a roadblock with Shaders.

My goal is to blur a specific geometry, but my attempts at using Depth Of Field resulted in unintended blurriness of foreground objects. I want to isolate one object and blur only that.

Currently, I have a mesh created with LambertMaterial as shown below:

    var material = new THREE.MeshLambertMaterial({
        color: 0x5c5c5c,
        emissive: 0x000000,
        shading: THREE.FlatShading,
        transparent: true,
        opacity: 1
    });
    var mesh = new THREE.Mesh(geometryJson, material);

    scene.add(mesh);

I came across two shaders online for horizontal and vertical blur effects. But how can I apply them while maintaining the existing color settings?

Horizontal blur shader

Vertical blur shader

I attempted to use a ShaderMaterial like this:

    var material = new THREE.ShaderMaterial( {
        uniforms: THREE.UniformsUtils.clone( HorizontalBlurShader.uniforms ),
        vertexShader: HorizontalBlurShader.vertexShader,
        fragmentShader: HorizontalBlurShader.fragmentShader
    } );

It seems to be working now (especially after exporting my model with UVs), but not quite as expected.

Instead of blurring the object, it renders semi-transparent based on the face angle. How can I achieve the desired effect of blurring the object with the correct color from the original material and also incorporate the vertical shader?

Answer №1

Blurring a single object in WebGL is not as straightforward as one might hope. The blur and depth of field examples in three.js utilize post processing effects, similar to applying filters in Photoshop after rendering the image.

This doesn't mean it's impossible to blur a single object, but it certainly presents a challenge. One approach could involve rendering whether or not to blur an object into a separate channel, such as the alpha channel. However, this method may not be perfect due to overlapping objects causing blurring to bleed past the overlap areas.

Another technique would be to render each object onto its own render target, apply the blur effect, and then combine all the render targets together. Depth values may need to be stored for each render target to ensure proper compositing with depth information.

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

Discovering distinct colors for this loading dots script

Looking for assistance with a 10 loading dots animation script. I'm trying to customize the color of each dot individually, but when I create separate divs for each dot and control their colors in CSS, it causes issues with the animation. If anyone ...

Remove hidden data upon moving cursor over table rows after a delay

Hey there! I'm in need of a little assistance, so I hope you can lend me a hand. Here's the scenario: Within my category table, there are numerous rows, each containing a hidden textbox with an empty value and a unique ID. As users navigate t ...

Guide to creating a synchronous wrapper for jQuery ajax methods

I've made the decision to switch from synchronous ajax calls to asynchronous ones due to lack of support in most modern browsers. My code is currently reliant on synchronous (and dynamic) ajax calls to client-side functions that must be completed befo ...

Copying an object from one to another using Angular JS within a loop

Attempting to transfer a portion of a json object into another json object (acting as a filter), inside a for loop and under a conditional statement, proves unsuccessful. The following method works when creating an array: $scope.candidats=[]; for (i=0;i& ...

What is the best way to create a reliable and distinct identifier in React while using server-side rendering (

Currently, I am utilizing SSR within Next.js. My goal is to create a unique ID within a component in order to use it as an attribute for a DOM element's id. Since this component might be utilized multiple times on a single page, the ID needs to be dis ...

JavaScript FormData class does not recognize disabled and checked states on Bootstrap 5 switches

I am implementing a Bootstrap 5 switch input that I need to be mandatory, meaning it should be checked by default and not uncheckable. The official documentation suggests combining the attributes checked and disabled. However, it seems like the Javascrip ...

Modifying background with JavaScript and AJAX技术

I currently have a table structured like the following; <table style="width: 100%; border: solid 1px #666600; min-width: 800px" cellpadding="0" cellspacing="0"> <tr> <td id="aaa">&nbsp;</td> ... Using jQuery's ajax functi ...

Find and conceal the object within the list that includes the term "Ice"

The teacher's assignment is to create a radio button filter that hides items with the word "Ice" in them, such as "Ice Cream" and "Iced Tea." Here is the current code I have been working on: <!DOCTYPE html> <html> <head> <me ...

Innovative text input recommendation provider

Is there a tool in bootstrap or jQuery that can provide suggestions as I type in a textarea? Similar to autocomplete, but without replacing the entire text when clicking on a suggestion. I'm looking for something like an SQL editor where typing a tabl ...

Copying text from an iframe using HTML and JavaScript

As someone who is relatively new to web development, I am currently attempting to transfer text from an iframe to a textarea located on a Bootstrap-html webpage. You can view an example of the code I am working with here: https://jsfiddle.net/fe5ahoyw/ J ...

Utilizing Vue JS to set an active state in conjunction with a for loop

Currently in Vue, I have a collection of strings that I am displaying using the v-for directive within a "list-group" component from Bootstrap. My goal is to apply an "active" state when an item is clicked, but I am struggling to identify a specific item w ...

What are the steps needed to establish a distinct data scope in AngularJS along with the application of filters?

I’m currently practicing my skills in AngularJS and I have been experimenting with creating reusable components. At this stage, I have successfully created a component using the directive method. However, I now have some doubts that I need clarification ...

Creating a password with two distinct numbers using regular expressions

In javascript I am struggling to create a password that meets the criteria of having at least eight characters, including two SEPARATE digits, one uppercase and one lowercase letter, as well as one special character (-, @, #, $, &, *, +) but not /, !, ? ...

The issue with sRGB encoding persisting in THREE.EffectComposer has not been resolved

In my threejs scene, I am attempting to apply post-processing effects. I am utilizing EffectComposer for this purpose, but I am experiencing difficulties enabling sRGBEncoding in the renderTarget. const renderTarget = new THREE.WebGLRenderTarget( sizes ...

JavaScript: How can I remove it when I click anywhere on the webpage with onClick?

A challenge I'm facing involves creating a form with an input field that changes its border color when clicked. My goal is for the border to return to its original color when clicking anywhere else on the page. -HTML <form action=""> ...

The use of functions and parentheses in Javascript

I'm having trouble understanding how the f() functions work. Could someone please explain why it prints two '1's? I know it prints '1' for every '()' after f(f), but I don't understand the reasoning behind it. fun ...

Issue with Javascript function not triggering on Django dropdown selection change event

I have a question. I have a JS function that I downloaded from the internet and is being used in a Django project. In my template.html file, I have a <select class="select_filter" onchange="myFunc(this.value);"></select>. Ad ...

Is there a way to efficiently process multipart/formdata, application/json, and text/plain within a single Express handler?

Operating an express demo server that mirrors the client's POST requests back to it is a part of an educational practice. In this exercise, the client makes a POST request using the fetch API, like so: fetch('http://localhost:5000/', { m ...

Encountering a glitch while iterating through function results in failure after the initial modification

I am facing some errors with the script provided below: var sections = ["#general_info", "#address_records", "#employment_history", "#driver_experience", "#military_experience", "#eeo_survey", &qu ...

Tips on adding line breaks after periods that are not at the end of a sentence in HTML text nodes with regular expressions

Looking to craft a regex that can identify all periods not enclosed in quotes and not followed by a '<'. This is for the purpose of converting text into ssml (Speech Synthesis Markup Language). The regex will be utilized to automatically inse ...