Objects in Three.js Sparkling from Afar

Currently, I am tackling a project that involves working with scenes containing objects ranging from 10 to 1000000 in size. A recurring issue I have encountered is that when dealing with the larger end of this size spectrum, the objects seem to 'shimmer'. This shimmering effect only occurs when objects intersect, and it becomes more pronounced as the camera moves away from the object.

An image showcasing the problem can be viewed here: https://i.sstatic.net/wu1lz.jpg

While I am unsure of the root cause of this problem, I have come up with a couple of possible explanations:

Firstly, it is possible that I am working with sizes that are too large for three.js / webgl to handle efficiently.

The second potential issue could be related to the camera controls that I have implemented:

    if(mouseIsDown == true){
        if(this.movementSpeed < this.maxSpeed){
            this.movementSpeed += this.acceleration
        }else{
            this.movementSpeed = this.maxSpeed
        }

    }else{
        if(this.movementSpeed > this.minSpeed){
            this.movementSpeed = this.movementSpeed/this.deceleration
        }else{
            this.movementSpeed = this.minSpeed  
        }

    }

Here, this.minSpeed is set to 0, and the camera movement is controlled by this.movementSpeed through the following actions:

var actualSpeed = delta * this.movementSpeed;
this.object.translateZ( -actualSpeed * forwardOrAuto );
this.object.translateX( actualSpeed * sideSpeed );
this.object.translateY( actualSpeed * upSpeed );

Although I initially dismissed this as the cause, it is worth noting that the shimmering persists even when the movement speed is at extremely low values like 10^-20 or -30.

Additionally, it should be mentioned that I am using r.55 version.

Answer №1

It seems like the issue you're facing is related to precision problems. Rounding errors can be exacerbated by movement, especially when working on a model of the solar system (measured in meters) with three.js. I encountered similar problems with "flickering" textures/models in my own work. As gaitat rightly pointed out, it's likely due to z-buffer depth precision issues. My partner and I found a few ways to address this challenge.

The z-buffer is non-linear, as sjbaker's website referenced by gaitat illustrates. Most z-buffer precision is concentrated in the near range. If your objects are reaching sizes of up to 1000000 units, they and the space between them are already beyond the effective precision range. One common solution, used in many video games, is to move the world instead of the player (camera). This can help increase precision as objects approach the camera. This technique is crucial for avoiding texture flickering on large distant objects or for small meshes far from the origin, where rounding errors can cause meshes to jump out of view. Implementing this strategy can be challenging, as it requires real-time calculations to move everything relative to the player and manage rounding errors effectively.

Adjusting the near and far camera settings can also impact z-buffer precision. Setting a higher far number can minimize near precision loss and enhance mid-range precision. Even if the meshes are relatively small, setting a near camera value of 10 or 100 could provide some flexibility. Camera settings are not the sole solution to z-buffer issues.

polygonOffset - This feature dictates the z-buffer hierarchy, determining which material on a mesh should be rendered on top. While it can solve some problems, it may create others and require fine-tuning. Think of it as a z-index in CSS, but more nuanced. Adjusting the offset for one material to render over a distant object may inadvertently render it over a nearby object. This may necessitate setting offsets for multiple objects, with values typically ranging between -1.0 and 1.0 and requiring careful adjustment.

depthWrite=false - This directive instructs the renderer not to write to the depth buffer, useful for materials that should always appear behind other elements, such as skyboxes and backgrounds.

Despite utilizing the aforementioned methods in our project, which involved measurements as large as 40 Astronomical Units in meters (Pluto), we still encountered mediocre results.

This phenomenon is often referred to as "z-fighting," so persevere in addressing it!

Answer №2

If your ranges are significantly large, it may be due to setting your camera's near plane close to 0 and your far plane close to 1000000. However, this can result in not having enough z-resolution. For more information on this topic, check out .

Answer №3

One innovative approach that has the potential to be effective is utilizing two separate scenes along with two cameras.

The first scene could be designated for close-range objects, while the second scene could cater to distant elements. Each scene would be rendered using a camera configured with zNear and zFar values tailored to the specific requirements. For instance, the close scene could have a camera with zNear set to 0.1 and zFar set to 10,000, whereas the far scene could utilize a camera with zNear of 10,000 and zFar of 6,000,000.

However, the challenge arises when attempting to synchronize the scenes by integrating TrackballControls with both cameras.

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

What steps can I take to rearrange my return statement within an asynchronous function?

Imagine having a function that fetches data from a database. findById(id) { return Model.findById(id) } The goal is to rearrange the output from the user data in this format: { name: "Tom", age: 57 } To something like this: { message: ...

Top method for individually choosing multiple selections from a foreach loop output

Need help with a loop: foreach ($userItems_get as $item => $value) { if ($value['prefab'] == 'wearable') { echo $value['name'] . "</br>"; echo "<img src=\"{$value['image_invent ...

Interactive rotating display with constantly updating information

Recently I started learning angularJS and I must say, I am already hooked. I have developed a website that pulls data from JSON files (created by external scripts) locally to show real-time information on various pages. Now, I want to include a sliding car ...

What is the total amount within a specified date range when retrieved as JSON?

Consider the following JSON structure: { "timesheets": [ { "user": { "username": "erik", "first_name": "Erik", }, &q ...

Error! Unable to Inject ComponentFactoryResolver

Recently, I attempted to utilize ComponentFactoryResolver in order to generate dynamic Angular components. Below is the code snippet where I am injecting ComponentFactoryResolver. import { Component, ComponentFactoryResolver, OnInit, ViewChild } from "@an ...

Rendering with node.js express inside nested JS files

Imagine I have a basic view <html> <head> <title>something</title> </head> <body> <%= param %> </body> <script type="text/javascript" src="myscript.js"></script> </html> Be ...

Elegant method for politely asking individuals utilizing IE7 and earlier versions to leave?

TLDR: Politely ask IE6/7 users to switch browsers without accessing content. In essence, I don't want people using IE7/6 on my web app. I was considering using a doc.write function after loading to replace the page with a message stating "Sorry, your ...

Steps for obtaining images using lazy loading: <img loading="lazy"

After successfully utilizing the JavaScript-executer to locate the ImageElement, I encountered an error when attempting to extract the URL for downloading the image.svg. The error message reads "NoSuchElementException." Below is a snippet of my code: ((J ...

How to automatically close the menu on a Wordpress theme after clicking a link

I am currently using a Wordpress theme named ichiban, which can be viewed by following this link. Within this theme, I have created custom menu items that are designed to link directly to different sections on the same page. However, I am encountering an i ...

React does not allow for images to be used as background elements

I am currently working on a web page and I have attempted to use both jpg and png images as backgrounds, but they do not seem to display on the page. import './Entrada.css' const Entrada = () => { return( <div style={{ b ...

Making a single variable object as opposed to using multiple variables

In order to improve the code structure, I am looking to consolidate all properties into a JavaScript object instead of using multiple variables: // Method 1 // This method gives an error as _inp cannot be accessed by input_value // Uncaught TypeError: Can ...

Transmit console log data to a PHP webpage

Is there a way to pass the values collected from a Bootstrap table using console.log to a URL via POST method upon button click? var $table = $('#table'); var $button = $('#button'); function getRowSelections() { re ...

Deciding the optimal time to start a new JavaScript file and effectively organizing source code

Embarking on a substantial map application project involving approximately 5,000 lines of JavaScript, I am contemplating changing my approach to file organization. Traditionally, I would consolidate all code into a single file named 'main.js', bu ...

Tips on harnessing the power of PhantomJS and node.js for web scraping

After successfully installing node-phantom using the command npm install node-phantom, I encountered an error when running this code: Cannot find module 'webpage' var webpage = require('webpage').create(), url = "https://www.exampl ...

How can I write the code to enable dragging the button for navigating to the next page?

<a draggable="true" class="user" id="leonardo" ondragstart="dragUser(this, event)" aria-selected="undefined"> IPD</a> If I want the button to navigate to the next page when dragged, what code should I write? ...

Creating a function in Angular to locate each object based on its ID

Hello there, I am currently working on creating a method called findChildByIdInData(data:any, childId:string). This method will take in a JSON main node with children that have unique IDs, and the goal is to find a specific object based on the ID provided ...

Exploring the capabilities of multiple nested controllers in AngularJS

I am facing a challenge that I haven't been able to find a solution for online. My search form includes multiple instances of a common controller used for typeahead/autocomplete searches. Each controller is set up with different parameters for search ...

Update issue with Three.js mixer not responding to 'setTime' command

I've configured my threejs animation mixer like this: this.mixer = new THREE.AnimationMixer(this.object); this.mixer.timeScale = 2; //play at double speed this.mixer.addEventListener('finished', this.handlePullAnimFinished); this.pullAnim = ...

There appears to be a malfunction with WebRupee

I have incorporated the new rupee sign into my website to represent Indian currency. Below is the code snippet I used: For the script, I included " and also added the following: <span class="WebRupee">Rs.</span> 116,754.00 To style it using ...

HTML Element Split in Two by a Line in the Middle

Greetings to all, after observing for a while I have decided to make my first post here (and just to clarify, I am an electrical engineer, not a programmer). I am in search of creating a unique element in HTML where I can detect clicks on both its upper a ...