Ways to instruct tween.js to pause until a texture is shown in three.js

I encountered an issue while using three.js alongside tween.js. The problem arises when the timer for a tween.js animation begins before the texture of a sphere is actually displayed.

Currently, I am developing a panorama viewer with three.js specifically for Firefox. In this viewer, different panorama images are mapped onto spheres with the camera positioned at the center. Users can virtually navigate between rooms by switching between these spheres, each displaying a unique texture. When transitioning to a new room, an animation utilizing tween.js dims the light before illuminating it again with the new image.

The challenge lies in the delay experienced when loading and displaying an image for the first time in a room. Since the image is not preloaded into memory, there is a noticeable lag before it appears on screen.

This delay wouldn't pose a problem if the room stayed dark until the entire panorama was loaded and ready for display. However, the tween.js animation kicks in before the image is fully rendered.

As a result, instead of a gradual lighting effect as intended, the new panorama briefly appears and then the light abruptly switches on without any animation. The animation duration is set to 500 ms, which aligns with the time taken to load the panorama image.

Is there a way to determine if a texture has been rendered so that I can synchronize the animation accordingly?

The version of Three.js being used is r71.

//after dimming the light, switch panorama and illuminate scene
tweenDarkenLight.onComplete( function() { 

  roomModule.setRoomVisible(); //reveals new room

  tweenEnhanceLight.start(); //initiates illumination animation
});

All textures for the panoramas/rooms are preloaded and attached to spheres upon launching the application. However, they remain hidden until a user selects them.

texture = new THREE.Texture();

loader = new THREE.ImageLoader( manager );

//the URL for each panorama is stored in roomAssetArray
loader.load( roomAssetArray.panorama, 

  function ( image ) {

  texture.image = image;
  texture.needsUpdate = true;
});

Answer №1

To ensure seamless integration, it is important to connect your room visibility code with the tween.start() function.

If you have a loadTexture() function in your code, make sure to include a callback as the third argument. This callback will be executed once the texture is successfully loaded. Here's an example:

texture = THREE.ImageUtils.loadTexture( "room.jpg", {}, function() {
    // room texture has been loaded

    // perform the tween operation
}

Update:

An alternative approach could involve adding a render call just before or after the tween, or after any code that modifies the visibility of the spheres. This will ensure that the textured geometry is processed at least once.

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

Extract the price value from the span element, then multiply it by a certain number before adding it to a div element

On the checkout page of my website, I have the following HTML: <tr class="order-total"> <th>Total</th> <td><strong><span class="woocommerce-Price-amount amount"> <span class="w ...

What is the best way to postpone the loading of an image and show a loading.gif for a few seconds before it appears on the screen? (with the help of jQuery or JavaScript)

My dilemma involves two images: -loading.gif (a loading animation) -scenery.jpeg (the image I want to display after the loading process); Since scenery.jpeg is a small file, using the load() function will simply skip over loading.gif So, when I click t ...

Angular is not providing the anticipated outcome

I'm new to Angular (7) and I'm encountering an issue while trying to retrieve the status code from an HTTP request. Here's the code snippet used in a service : checkIfSymbolExists() { return this.http.get(this.url, { observe: 'res ...

How is it possible for the javascript condition to be executed even when it is false?

Despite the condition being false, the javascript flow enters the if condition. Check out the code snippet below: <script> $(document).ready(function() { var checkCon = "teststr2"; console.log("checkCon: "+checkCon); if(checkCon == " ...

Angular 4: Unhandled error occurred: TypeError - X does not exist as a constructor

I am currently developing a project in Angular 4, and I encountered an error while running the application. The specific error message is as follows - ERROR Error: Uncaught (in promise): TypeError: index_1.EmployeeBase is not a constructor TypeError: in ...

My HTML file is not able to load my Javascript file

I'm currently working on integrating a JavaScript code for a show more and show less feature on my page. The goal is to limit the description so that it shows only a certain number of characters, with an option to expand or collapse the text. However, ...

Switching from Ldapsearch to ldapjs transformation

I've been experimenting with converting a ldapsearch query from ldapsearch to an ldapjs script: ldapsearch -H ldap://ldap.berkeley.edu -x -b 'ou=people,dc=berkeley,dc=edu' objectclass=* Here is the ldapjs script I have been working on: va ...

Unable to retrieve a state property within a Vue template

Embarking on my Vue journey, I've been immersing myself in online videos to grasp the essence of this framework. One intriguing observation that has piqued my curiosity is the difference in behavior when I switch from a template to a render function i ...

Using JavaScript to navigate options in an HTML select element

I've implemented jqtransform to customize the appearance of form selects. However, I am unable to navigate and select options using arrow keys. Is there a way to select options using keys? Any solutions? Below is my HTML code snippet: <form> & ...

How come the directional light isn't impacting the entire scene?

For further clarification, here is a demo link - http://jsfiddle.net/vnr2v6mL/ var scenario = new THREE.Scene(); var cam = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); var renderer = new T ...

When transmitting a file to the client via HTTP, make sure to include the file name in the HTTP headers

I've encountered a situation where I have an array in JS that needs to be converted to CSV format on the server and sent to the client with the content type set as text/csv However, when it's sent to the client, the browser automatically downloa ...

Execute an xmlhttprequest and then redirect to a different URL

I'm a beginner when it comes to AJAX and JavaScript. My goal is to first show a confirmation box, then send an XMLHttpRequest if confirmed. After that, I want to redirect the user to a new page. Below is my current code: <script type="text/javascr ...

Pass the output of canvas.toDataURL function in JavaScript to the $mail->addAttachment method in PHP

Currently, I am working on a web application that allows users to customize a pool by dragging different icons onto a blueprint (such as stairs, skimmer, light...). Once the customization is complete, users can click on a button to receive the blueprint v ...

Using MeshStandardMaterial with Metal Texture in three.js

I believe it should be feasible to create metal-like materials in Three.js using MeshStandardMaterial and following a pbr-roughness-metalness workflow, but I am struggling to find good examples demonstrating how to do so. I am restricted from using Phong ...

Calculate the difference in days between two selected dates from an Ajax Datetime Picker using JavaScript

How can I use JavaScript to count the number of days between two selected dates from Ajax date time pickers in my aspx page? The script should automatically input this calculated value into a designated "Number_Of_Days" text box without requiring a page ...

Selection List dictates the necessity of JavaScript

I need to create a JavaScript code that will require the comment field (textarea) when a selection is made from a list. Currently, I have added a required icon next to the comment section but I am stuck at this point. See the code below. Thank you in adva ...

D3.json request falls flat

Is failure inevitable in this scenario? d3.json("https://api.kraken.com/0/public/Time", function(){ console.log(arguments); }); I'm encountering a familiar CORS hurdle XMLHttpRequest cannot load https://api.kraken.com/0/public/Time/. No ...

Retrieve the most recent information from the API using axios and React hooks

I have access to an API that provides data, but I am only interested in the most recent information. The newest data is always located at the end of the dataset. For instance, if there are 50 points of data, the latest would be number 50. Can someone adv ...

Mastering the Art of jQuery Post with Iteration and Result Utilization

I am facing an issue with my code function fetchInfoFromDB() { let body = ""; let text = ""; $("tr").each(function (index) { let code = $(this).children("td:nth-child(2)"); $.post("http://urltogetdatafromdatabase.com/getinfo.ph ...

What is the safest method to insert data into a div element?

After utilizing ajax to retrieve a JSON file from the web, I successfully assigned one of the keys to a variable. Now, my question is how can I display the contents of that variable within a div tag? My initial approach was: theJsonKey = jsonObject.key; ...