Generate a dynamic animation showcasing transparency effects, uncovering the intricate Mesh structure within threejs

Is there a way to achieve a linear animation in THREE.JS that reveals a complex 3D mesh from the bottom to the top, similar to a slideUp effect in 2D jQuery objects?

After searching for options such as opacity channels or maps, it seems that they are not possible solutions for this task.

Using sprites of textures and adjusting offsets may not be ideal due to the complexity of my UV map.

Are there any methods available in THREE.JS that can help create this desired effect?

Answer №1

Transform the complete scene into the first framebuffer (texture).
Transform only the mesh into the second framebuffer (texture).

Display a fullscreen rectangle that utilizes the two textures mentioned earlier, using a variation of the following code:

uniform sampler2D texScene;
uniform sampler2D texMesh;

uniform vec2 uResolution;
uniform float time;

void main() {
    vec2 uv = gl_FragCoord.xy / uResolution;

    vec3 s = texture2D( texScene, uv ).xyz;
    vec4 m = texture2D( texMesh, uv );

    // slide up effect
    float percent = clamp( time, 0, endAnim ) / endAnim;  // endAnim is the time animation ends (assuming animation starts at time=0)

    vec3 color = s;
    if( uv.y > (1.0 - percent) ) {
        color = s * (1.0 - m.a) + m.xyz * m.a;
    }
    gl_FragColor = vec4( color, 1.0 );
}

The functioning of the code should be easily understood. Based on the passed time, it determines the current stage of the animation and decides whether to incorporate the mesh's color or display only the background color.

I trust this explanation is useful.

Answer №2

Another option is to render the building on the screen by using the code

gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
and creating an alpha gradient for the building (from top to bottom).

When drawing to any output, WebGL considers the source information and the destination information (what has already been drawn to the output) and combines them according to your instructions.

The process of drawing to an output can be summarized as follows:

SOURCE_VALUE * [SOURCE_FACTOR] [BLEND EQUATION] DESTINATION_VALUE * [DESTINATION_FACTOR];

The default equation is:

SOURCE_VALUE * 1 + DESTINATION_VALUE * 0;

This equation essentially replaces all existing information in the buffer with the new information. What we aim to achieve is to retain the existing information where we are not drawing and only incorporate the new information where we are drawing. This modifies the equation to:

SOURCE_VALUE * SRC_ALPHA + DESTINATION_VALUE * ONE_MINUS_SRC_ALPHA;

If one fragment of your building is 20% transparent, it will display 20% of the building's color and 80% of the color of what lies behind the building.

This method of rendering semitransparent objects takes into account the depth buffer.

Answer №3

I came up with a different approach.

Instead of using multiple textures with repeated patterns for the building, I decided to use one texture for the entire structure. I arranged the UVs vertically in a progressive manner, aligning the faces from bottom to top on the texture. To add animation, I utilized a canvas texture and filled it with a transparent rectangle.

// x - current step
// steps - number of steps

var canvas = document.getElementById('canvas-texture'),
    ctx    = canvas.getContext('2d');

ctx.beginPath();
ctx.drawImage(image, 0, 0);
ctx.globalCompositeOperation = 'destination-out';
ctx.fillRect(0, 0, width, height/steps * x);
ctx.closePath();

Since I needed a solution quickly, I plan to experiment with your suggestions over the weekend. I can also create a demo showcasing my approach if you're interested.

Thanks to everyone for your assistance.

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

typescript is failing to update CSSRule with the newly assigned background-color value

I am currently working on dynamically changing the CSS style (specifically background-color) for certain text on a webpage. To achieve this, I have been attempting to modify CSSRule by accessing the desired CSSRule based on selectedText. Here is the code s ...

The feature of THREE.MultiMaterial has been discontinued and developers should now utilize an Array instead

I recently designed a building model in SketchUp and saved it as a .dae file along with the texture. I'm trying to showcase it on a web browser using three.js from https://github.com/mrdoob/three.js/, but I encountered an error stating "THREE.MultiMat ...

I encountered a 429 error while trying to make a request to the Spotify API

I recently encountered an issue with my website where I am trying to retrieve and display my playlists. Everything was working fine yesterday, but this morning I started getting error 429 every time I tried to retrieve data from the api, even though I have ...

What could be causing the appearance of a mysterious grey box hovering in the upper left portion of my image and why is the code only adjusting the size of the grey box instead of the entire

I've been working on embedding some JavaScript into my Showit website to create a drag-and-drop feature that displays a collage/mood board effect. Everything is functioning correctly, but I'm running into issues with resizing the images. There&a ...

Modifying material in Three.js

My method for selecting geometry involves using the numbers on my keyboard, as shown below: if(keyboard.pressed("1")){ obj = torus; } This allows me to toggle their visibility: if(keyboard.pressed("a")){ THREE.SceneUtils.traverseHierarchy( obj, fu ...

The implementation of local JSON instead of external JSONP in Angular

I am exploring the option of storing a json-file on the same server as my Angular app. I am wondering about how I can modify this code to read from a locally stored json file: ergastAPI.getDrivers = function() { return $http({ method: 'GET&apos ...

Tips for replacing an element in DOM with an updated HTML string

My current task involves updating a stringify HTML element using an element from the DOM. This stringify HTML element is stored in an array within the localStorage. First, I convert it into manipulatable HTML like this: let toBeUpdated = document.createEl ...

A mysterious JavaScript snippet that automatically scrolls down when the webpage is loaded

Recently, I encountered an issue with a mysterious JavaScript script that automatically scrolls down when entering the page (I suspect it's JavaScript). I've tried to investigate using Firebug, examining the code, deleting scripts, and even remov ...

I am looking to showcase the JSON output on a ReactJS interface

Here is the output of my JSON data I am using the Map function in ReactJS to display elements from a JSON array. I have attempted the code below and encountered an error stating "Cannot read property '_currentElement' of null." Can someone pleas ...

How can you troubleshoot code in NextJS API routes similar to using console.log?

Incorporating nextjs API routes into my upcoming project has been a priority, and I recently encountered an issue with code execution upon sending a POST request. Debugging has proven to be challenging since I am unable to use conventional methods like co ...

Strategies for re-rendering a React component when the useState value remains the same or retains its previous value

I am currently using a useState hook to store the value of selectFolderId: const [selectFolderId, useSelectFolderId] = React.useState(documentStore.id) Despite trying to update selectFolderId with the new value from DocumentStore by using a useEffect hook ...

The depth buffer in Webgl FrameBuffer is not being cleared properly

Currently, I am working on developing a 2D sprite renderer that utilizes render textures for custom compositing. However, I have encountered an issue where the depth buffer on the FrameBuffer is not clearing properly. Due to this, all the sprites leave a p ...

Ways to terminate a NOHUP process for Node.js started through SSH

I have initiated a node.js server on port 3000 using nohup for background running, but now I need to stop the server on the same port and start it again. How can I stop the process? To identify the process in order to kill it, I can use this command: ps ...

What is the best way to send data from a child functional component to a parent class component?

I am facing a situation where I have a parent class component and a child functional component. While there are examples available on passing values from a child class component to a parent class component, I am wondering how to pass a value from this type ...

"React Bootstrap column showing gaps on the sides instead of filling the entire

In my current project with React and React Bootstrap 4.0, I am aiming to create a collapsible side-bar of 300px width on the left side of the screen, with the content displayed in the remaining space. The main parent component for the side-bar and content ...

Guide to making a semicircular sphere using three.js

I'm a beginner when it comes to three.js and I'm attempting to generate a hemisphere, basically half of a sphere. Unfortunately, I can't locate the appropriate geometry for this specific shape. Would I need to create a custom one and if so, ...

Error Encountered in jQuery UI SelectMenu

Struggling to integrate the jQuery UI SelectMenu, I keep encountering the same error in the browser console. Below is the HTML Code: <select name="files" id="files"> <optgroup label="Scripts"> <option value="jquery">jQuery.js</ ...

How to use jQuery Animate to create a step-by-step animation with image sprites?

I'm working on creating an image sprite using jQuery and I'm curious if it's feasible to animate it in steps rather than a linear motion. I initially tried using CSS3 animations, but since IE9 is a requirement, the animations didn't wor ...

I'm encountering an undefined JavaScript variable, how should I proceed?

$(function(){ //Location was "set". Perform actions. $("#geocodesubmit").click(function(){ var geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': address}, function(results, status) { if (status ...

Unique markers for Google Maps

I have been working on creating custom Google maps with the concept of incorporating 3 distinct markers such as forests, rivers, and lakes. While I have successfully added these custom markers using a helpful tutorial, I am now facing a challenge with sor ...