Make the object disappear once the position tweening is completed

I want to animate the position of this object using tweening, and once the animation is complete, fade it out. Currently, I am just hiding it when the animation finishes.

this.tweenBox2.onUpdate(function () 
{
            that.box.position = a;
            that.box.Show();
});
this.tweenBox2.onComplete(function () {
            for (var i = 0; i < that.box.children.length; i++) {
                 that.box.children[i].visible = false;
            }
            that.box.position = new THREE.Vector3().copy(storagePos);
});

Here is what is inside box.children[0]

THREE.Mesh

webglActive : true __webglInit : true _modelViewMatrix : THREE.Matrix4 _normalMatrix : THREE.Matrix3 castShadow : false children : Array[0] eulerOrder : (...) frustumCulled : true geometry : THREE.Geometry id : 4672 material : THREE.MeshLambertMaterial matrix : THREE.Matrix4 matrixAutoUpdate : true matrixWorld : THREE.Matrix4 matrixWorldNeedsUpdate : false name : "" parent : Box position : THREE.Vector3 quaternion : THREE.Quaternion receiveShadow : false renderDepth : null rotation : THREE.Euler rotationAutoUpdate : true scale : THREE.Vector3 up : THREE.Vector3 useQuaternion : (...) userData : Object uuid : "9C6DC789-20D0-4F9F-88B6-CDA9A2C372B9" visible : true __proto : THREE.Object3D

The material is created in the following manner:

var box = boxModel.scene.children[3].children[0].clone();
box.traverse(function (child) 
{
    if (child instanceof THREE.Mesh) 
    {
        child.material = child.material.clone();
    }
});

Answer №1

Setting the transparency of your material to true is crucial when creating the content for your box:

material = new THREE.MeshPhongMaterial( {color: 0x00ff00, transparent: true} );

The choice of using a PhongMaterial may vary.

In your onUpdate function, adjusting the opacity of the material can be done similarly to changing its position:

this.tweenBox2.onUpdate(function () 
{
            that.box.position = a;
            //This will gradually fade the opacity from 0 to 1
            //Please note: I assume "box" is a THREE.Mesh
            that.box.material.opacity = 0;

            that.box.Show();
});

An onComplete function might not be necessary.

Update 1: A global variable has been added to store the opacity value:

var opacityVar = 1;

Below is the implementation of the fadeOut function:

function fadeOut(){

    if( opacityVar >= 0){

        mesh.material.opacity = opacityVar;
    }
}

Within the render function, the opacity variable is updated and the fadeOut function is called. Make sure to call this in your onComplete function:

function render() {

    camera.lookAt( scene.position );

    renderer.render( scene, camera );

    opacityVar -= 0.01; //Adjust opacity variable

    fadeOut(); //Initiate FadeOut 

}

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

Enhance Shipping Options on Your Woocommerce Cart

I am facing a challenge with providing delivery options for three different countries in my e-commerce store. I want the customer to be able to select their country and instantly see the available delivery methods without having to refresh the entire page ...

jinja2.exceptions.TemplateSyntaxError: instead of 'static', a ',' was expected

My current project involves using Flask for Python, and I encountered an error when running the project from PyCharm. The error message points to line 192 in my home.html file: jinja2.exceptions.TemplateSyntaxError: expected token ',', got &ap ...

Slide in parts gradually by scrolling up and down, avoiding sudden appearance all at once

I have implemented a slider on my website using jQuery functions. For scrolling down, the following code snippet is used: jQuery("#downClick").click(function() { jQuery("html, body").animate({ scrollTop: jQuery(document).height() }, "slow"); ...

Is there a way to send numerous results using these ajax functions through a POST request?

I currently have a function that sends one output, which is 'OPTIONS', as JSON using AJAX. Now I am looking to modify this function in order to POST multiple outputs, let's say two values. How can I adjust this function to handle two values ...

Can you provide instructions on how to utilize JavaScript in order to write to a separate webpage?

I need help with a JavaScript function that is supposed to take user input from one web page and write it to another existing web page within the same domain. The function iterates through a for loop correctly and builds the necessary information, but it ...

Fixing: Discord.js error - role is undefined

I need assistance with adding a role to a member when they join my server. Below is the code that I am using: I am encountering an issue which states "ReferenceError: roles is not defined". Can someone please assist me in resolving this problem? ...

The object does not have a property named 'fetch' and therefore cannot be read

Struggling to integrate a REST datasource into my Apollo Server. I've created a class that extends RESTDataSource for handling API requests. However, when attempting to call the login method from my GraphQL resolver code, an error is being thrown. An ...

Ziggeo video - Creating a responsive design for videos

Trying to achieve full responsiveness with Ziggeo videos has been challenging. Despite numerous attempts, the only result I'm getting is a fixed width. What I really need is for Ziggeo videos to adapt to 100% width and display correctly on various mob ...

Alter the hue of a solitary shade in the linear gradient

Is there a way to modify the initial color in a CSS linear gradient solely using Javascript? div { background: -webkit-linear-gradient(top, #F6F6F6, #E9E9E9) } I am specifically looking to alter the hue "#F6F6F6" while leaving the other color unchang ...

Creating a Selectable Child Form in ReactJS that Sends Data to Parent Form

Sorry for the lack of details, I'm struggling to explain this situation clearly. I'm currently learning ReactJS and JS. In a project I am working on, I have the following requirements: There is a form where users can input text and numbers. The ...

What is the most secure method for conditionally wrapping input with state?

I used to check for errors and wrap the input and error message in a div conditionally. However, I faced an issue where the input would lose focus when re-rendered. Is there a way to wrap the input conditionally without losing focus on re-render if the err ...

Globalization of an Angular2 Meteor Web Application

Creating a web application using Meteor and Angular2 is my current project. With the need for multi-language support in mind, I am referring to Uri Goldshtein's boilerplate at https://github.com/Urigo/angular2-meteor-base as my foundation. What appro ...

Traversing through pair of arrays simultaneously using forEach loop in JavaScript

I am trying to create a for loop that simultaneously iterates through two variables. One is an array named n, and the other, j, ranges from 0 to 16. var n = [1,2,3,5,7,8,9,11,12,13,14,16,17,18,20,21,22]; var m = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; ...

modifying a JSON document

After exploring various options, questions, and answers, I have come to the conclusion that none of them were suitable for my current project. My project involves a shopping cart system created using PHP sessions, which is functioning well. Users can add ...

Traversing a hierarchical JSON structure reminiscent of a tree object

I am working with a complex object structure, var cObj = { name: 'Object1', oNumbers: 3, leaf: [ { name: 'Inner Object 1', oNumbers: 4, leaf: [] }, { name: 'Inner Object 2', oN ...

Adjust the size of the textarea to accommodate the number of lines of text

<script type="text/javascript"> function AutoGrowTextArea(textField) { if (textField.clientHeight < textField.scrollHeight) { textField.style.height = textField.scrollHeight + "px"; if (textField.clientHeight ...

How can I locate the vertices of a face using its index in Three.js?

I'm working on a Three.js project where I have a mesh that uses buffergeometry. By using raycaster, I am able to find the intersection of a ray with this mesh on a specific face and determine its index. Now my question is: How can I find the positio ...

Challenges encountered when uploading files

Within my react form, I have multiple files that are uploaded to be sent to the backend. Below is a snippet of code for the form submit button: const handleSubmit = async (e) => { e.preventDefault(); setDisabled(true); const ...

Calling a JavaScript function using string parameters

Lately, I've stumbled upon an issue when attempting to execute a function with multiple arguments. <button type = "button" id = "clickmepls" onclick = killButton("clickmepls", "grave1")> Click me please </button> The definition of the fu ...

What steps can I take to stop Google Maps from resetting after a geocode search?

As a beginner working with the Google Maps Javascript API v.3, I have written some code to initialize a map, perform an address lookup using the geocoder, re-center the map based on the obtained lat long coordinates, and place a marker. However, I am facin ...