The Three.js scene is failing to render properly on subsequent attempts

Currently, I am in the process of developing a web-based height map generator using Three.js. The project involves utilizing multiple canvases to display the generated height map, individual octaves that make up the map, and a separate canvas to showcase how the height map affects a plane mesh.

While everything appears to be functioning well initially with all the predefined parameters, the issue arises when the canvases fail to update after the first render pass. This renders my program ineffective as the main goal is to manipulate various parameters of the height map using sliders and observe real-time changes.

Unfortunately, only the canvas displaying the 'real' 3D scene with the plane mesh seems to update. However, it appears that data from the initial render pass remains, giving the impression that the noise parameter uniforms are updating correctly - unlike the other canvases.

Prior to rendering each scene, I ensure that the `needsUpdate` field of the material meshes is set to `true`, yet this method only works for the last initialized canvas. I am at a loss here, as I have been unable to find any relevant information related to this problem.

Answer №1

After hours of troubleshooting, I managed to locate the solution on my own. Surprisingly, the issue did not lie with three.js itself but rather with my handling of multiple planes within the scene instead of just one. This mistake stemmed from inadvertently calling the initScene() method multiple times without properly checking if all shaders were still loading (I utilized an async AJAX request for shader loading). My oversight was only checking for when shaders were either not loaded or completely ready.

Here is the corrected code:

/**
 * Adds geometry to the scene and attaches previously loaded material.
 * 
 * @param {Number} wavelength of the first octave of the height map
 * @param {Number} frequency of the first octave of the height map
 * @param {Number} number of octaves (must be an integer)
 */
HeightMap.prototype.initScene = function(wavelength, frequency, lacunarity, persistency, octaves) {
    this.material = HeightMap.settings.templateMaterial.clone();

    this.updateUniforms(wavelength, frequency, lacunarity, persistency, octaves);
    HeightMapOctave.prototype.addPlane.call(this);

    this.initialized = true;
};

/**
 * Updates values for HeightMap.
 * 
 * @param {Number} wavelength of the first octave of the height map
 * @param {Number} frequency of the first octave of the height map
 * @param {Number} number of octaves (must be an integer)
 */
HeightMap.prototype.updateScene = function(wavelength, frequency, lacunarity, persistency, octaves) {
    if (this.initialized) {
        this.updateUniforms(wavelength, frequency, lacunarity, persistency, octaves);
        Canvas3D.prototype.render.call(this);
    } else if (!this.initializing) {
        this.initializing = true;
        this.loadShaders(function() {
            this.initScene(wavelength, frequency, lacunarity, persistency, octaves);
            Canvas3D.prototype.render.call(this);
        });
    }
};

Although this may seem like a niche problem, I hope this post can assist others facing a similar issue. It took me nearly a week to identify and resolve this elusive bug...

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

Removing consecutive pipe symbols in JavaScript

Looking for a way to remove excess pipe characters before a certain pattern in JavaScript. var testString="||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||f_test!!3!!||f_test!!4!!||f_test!!5!!||"; output ="||f_test!!3!!| ...

Incorporate an additional retrieve functionality in your asp.net web api

I'm brand new to the world of asp.net web api. I have a solid understanding of get(), put(), post() and delete(). For my application, I need to implement two additional get() methods. Here is an overview- public class StudentController : ApiControll ...

Error: the function is not defined, therefore the variable is being passed

I'm attempting to pass a variable in the URL in order to automatically check a radio button on a different page depending on which link is clicked. However, I keep encountering an "Uncaught ReferenceError: radio_onload is not defined" error. Could th ...

Guide on determining if the value in a JSON object is a string or an array in Node.js

Running a Node.js application, I encountered the following JSON array structure. First JSON object: var json1= { bookmarkname: 'My Health Circles', bookmarkurl: 'http://localhost:3000/', bookmark_system_category: [ '22&apos ...

Implementing dynamic checkbox values depending on a selection from a dropdown menu in Angular

In my checkbox list, I have various Samsung mobile models and two offers available. $scope.offers = [ { id: "as23456", Store: "samsung", Offer_message:"1500rs off", modalname: "Samsung Galaxy You ...

Error in Angular: Trying to access property 'setLng' of a null component variable

Just starting out with Angular and I've come across the error message Cannot read property 'setLng' of null. Can anyone help explain why this is happening? import { Component, OnInit, Input } from '@angular/core'; @Component({ ...

What is the best way to switch out the characters 'a' and 'b' in a given string?

Let's say we have the following text. Lorem ipsum dolor sit amet The revised text should look like this: merol merol merol merol Is there a predefined function in JavaScript that can help us replace characters like this within a string? ...

The keyboard fails to open when trying to input text on a WKWebView

Is there a way to programmatically open the keyboard in a WkWebView for tel text input after a JavaScript function call? I want the keyboard to display for efficiency reasons when a certain input is activated, but it doesn't gain focus automatically. ...

The Google reCaptcha reply was "Uncaught (in promise) null"

When using reCaptcha v2, I encountered an issue in the developer console showing Uncaught (in promise) null message regardless of moving the .reset() function. Here is the console output: https://i.stack.imgur.com/l24dC.png This is my code for reCaptcha ...

Arrangement of watch attachment and $timeout binding

I recently encountered a component code that sets the HTML content using $scope.htmlContent = $sce.trustAsHtml(content). Subsequently, it calls a function within a $timeout to search for an element inside that content using $element.find('.stuff' ...

refresh polymer components or make an ajax request within a custom element

I've been spending days on this issue with no success! My application relies on cookies for session handling and includes multiple custom elements imported in the header. Some of these elements need to access information from the 'cookie session& ...

Exploring the proximity between the camera and a mesh using THREE.js

How can I maintain a certain distance from the mesh landscape in THREE.js while keeping the camera pointed down at it? For example, if there are peaks in the terrain, I want the camera to move further away. I've tried using raycasting to achieve this ...

Guide to setting up a TreeView with default expansion

When using a @mui TreeView, all nodes are initially collapsed by default. I am trying to figure out how to have all nodes expanded by default, but haven't been successful so far. I attempted to create a method called handleExpandAll, but it doesn&ap ...

Unlocking location data in React Router-DOM 6: A step-by-step guide

I am currently working on implementing a 'forgot password' feature, where I am attempting to transfer the email data from the 'login page' to the 'forgot password' page using a Link element. However, I am encountering an issu ...

Ways to retrieve text like innerText that functions across all web browsers

I need to retrieve the text from a Twitter Follow button, like on https://twitter.com/Google/followers Using document.getElementsByClassName("user-actions-follow-button js-follow-btn follow-button")[0].innerText correctly displays the text as: Follow ...

Encountering an error when attempting to generate a production build after running the npm install command with the

After adding the brotli-webpack-plugin as a devDependency, I encountered an issue when attempting to generate a production build using npm run build (which internally runs next build). The error message displayed was: Error: Cannot find module 'bro ...

Importing modules dynamically in NextJS allows for the loading of

I have a basic function that is responsible for loading a script: const creditCardScript = ( onReadyCB, onErrorCB, ) => { let script = document.createElement("script"); script.type = "text/javascript"; script.src = process.CREDIT_CARD_SCRIPT; ...

Associating mongoose object with a DTO object in an Express.js application

I am looking for a way to transform mongoose result objects into DTOs for my views. Here is an example query that returns a mongoose object: const returnedData = (err, result) => { //Result object is a schema from Moongose cb(err, re ...

Preventing jQuery parser from turning arrays into objects

My JavaScript data file has the following structure: data = { items : [ {name: 'ABC'}, {name: 'CDF'} ] } After passing this data to $.ajax(type: 'POST', data: data), the converted data appears like this: it ...

The JavaScript operator that functions in a manner akin to the SQL "like"

I am working on a jQuery function that needs to perform like the "like" operator. Can anyone help me achieve this? Your assistance is greatly appreciated. The function currently only works if I search for the whole word, for example: var vsearch = "home" ...