Blurred entities aligning in front of one another (THREE.JS)

While experimenting with three.js, I encountered an issue with objects appearing fuzzy when they overlap. The distortion looks like this. Can anyone provide assistance with this problem? Below is the code I'm currently using:

 // Initializing basic objects
var scene;
var renderer;
var camera;

var backgroundScene;
var backgroundCamera;

// Initializing supporting modules
var stats;
var axis;
var cameraMode = 1;

// Time and acceleration values
var milis = 0;
var scale = 170;
var acceleration = 10000 * scale;
var acceleratedTime;

// Initializing object properties and constants
var sunGeo;
var sunMat;
var sun;

var sunLight;

// Earth properties
var earthGeo;
var earthMat;
var earth;

// Ecliptic Axis
var eclipticAxisGeo;
var eclipticAxisMat;
var eclipticAxis;

// Start the code execution
function init() {
    // Create scene
    scene = new THREE.Scene();

    // Initialize renderer
    renderer = new THREE.WebGLRenderer({
        antialias: true
    });
    renderer.setClearColor(new THREE.Color(0x000000, 1.0));
    renderer.setSize(window.innerWidth, window.innerHeight);

    // Initialize camera
    camera = new THREE.PerspectiveCamera(42, window.innerWidth / window.innerHeight, 0.1, EARTH_RADIUS * 100);

    // Creating supporting modules
    stats = new Stats();
    stats = initStats();

    axis = new THREE.AxisHelper(100000000000000);
    scene.add(axis);

    // Creating the Background
    var backgroundMaterial = new THREE.MeshBasicMaterial();
    backgroundMaterial.map = new THREE.TextureLoader().load("resources/textures/sky.jpg");

    var background = new THREE.Mesh(new THREE.PlaneGeometry(2, 2, 2), backgroundMaterial);

    background.rotation.z += Math.PI / 2;
    background.material.depthTest = false;
    background.material.depthWrite = false;

    backgroundScene = new THREE.Scene();
    backgroundCamera = new THREE.Camera();

    backgroundScene.add(background);
    backgroundScene.add(backgroundCamera);

    // Creating the Sun
    sunGeo = new THREE.SphereGeometry(SUN_RADIUS, SUN_MERIDIANS_AND_PARALLELS, SUN_MERIDIANS_AND_PARALLELS);

    sunMat = new THREE.MeshPhongMaterial();
    sunMat.emissive = new THREE.Color(0xffffff);
    sunMat.emissiveMap = new THREE.TextureLoader().load("resources/textures/sun.jpg");

    sun = new THREE.Mesh(sunGeo, sunMat);

    sun.position.x = 0;
    sun.position.y = 0;
    sun.position.z = 0;

    scene.add(sun);

    sunLight = new THREE.PointLight(0xFFFFFF, 1.0, 0);
    sunLight.position.x = 0;
    sunLight.position.y = 0;
    sunLight.position.z = 0;
    scene.add(sunLight);

    // Creating the Earth
    earthGeo = new THREE.SphereGeometry(EARTH_RADIUS, EARTH_MERIDIANS_AND_PARALLELS, EARTH_MERIDIANS_AND_PARALLELS);

    earthMat = new THREE.MeshPhongMaterial();
    earthMat.map = new THREE.TextureLoader().load("resources/textures/earth.jpg");

    earth = new THREE.Mesh(earthGeo, earthMat);

    earth.position.x = EARTH_SEMI_MAJOR_AXIS;
    earth.position.y = 0;
    earth.position.z = 0;

    earth.rotation.y = Math.Pi / 2;
    earth.rotation.z += EARTH_AXIAL_TILT;

    scene.add(earth);

    // Creating the EclipticAxis
    eclipticAxisGeo = new THREE.CircleGeometry(EARTH_SEMI_MAJOR_AXIS, 10000);
    eclipticAxisGeo.vertices.shift();

    eclipticAxisMat = new THREE.LineBasicMaterial({
        color: 0xFF00FF
    });

    eclipticAxis = new THREE.Line(eclipticAxisGeo, eclipticAxisMat);

    eclipticAxis.rotation.x = Math.PI / 2;

    scene.add(eclipticAxis);

    // Camera positioning
    camera.position.set(earth.position.x, earth.position.y, earth.position.z);
    camera.position.y += 1000000000 * scale;
    camera.position.z += 1000000000 * scale;
    camera.lookAt(earth.position);

    document.getElementById("WebGL-output").appendChild(renderer.domElement);

    setInterval(function() {
        ++milis;
        acceleratedTime = milis * acceleration / 1000;
    }, 1);

    renderScene();
};

// Rendering the scene
function renderScene() {
    stats.update();

    sun.rotation.y = SUN_ANGULAR_ROTAION * acceleratedTime;

    earth.rotation.y = EARTH_ANGULAR_ROTATION * acceleratedTime;

    earth.position.x = Math.cos(EARTH_ANGULAR_REVOLUTION * acceleratedTime) * EARTH_SEMI_MAJOR_AXIS;
    earth.position.z = Math.sin(-EARTH_ANGULAR_REVOLUTION * acceleratedTime) * EARTH_SEMI_MAJOR_AXIS;

    if (cameraMode == 0) {
        camera.position.x = earth.position.x + 10000000 * scale;
        camera.position.y = earth.position.y + 10000000 * scale;
        camera.position.z = earth.position.z + 10000000 * scale;
        camera.lookAt(earth.position);
    } else if (cameraMode == 1) {
        camera.position.x = EARTH_SEMI_MAJOR_AXIS;
        camera.position.y = 1000000000 * scale;
        camera.position.z = 1000000000 * scale;
        camera.lookAt(new THREE.Vector3(EARTH_SEMI_MAJOR_AXIS, 0, 0));
    } else if (cameraMode == 2) {
        camera.position.x = sun.position.x;
        camera.position.y = sun.position.y + SUN_RADIUS * scale / 50;
        camera.position.z = sun.position.z;
        camera.lookAt(sun.position);
    } else {
        camera.position.x = earth.position.x;
        camera.position.y = 0;
        camera.position.z = 0;
        camera.lookAt(earth.position);
    }

    requestAnimationFrame(renderScene);

    renderer.autoClear = false;
    renderer.clear();
    renderer.render(backgroundScene, backgroundCamera);
    renderer.render(scene, camera);
};

// Initializing stats module
function initStats() {
    stats.setMode(0);

    stats.domElement.style.position = 'absolute';
    stats.domElement.style.left = '0px';
    stats.domElement.style.top = '0px';

    document.getElementById("Stats-output").appendChild(stats.domElement);

    return stats;
};

// Handling window resize
function onResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();

    renderer.setSize(window.innerWidth, window.innerHeight);
};

Answer №1

It seems like the problem you're experiencing could be related to the depth buffer. I found a very helpful example here that compares logarithmic and linear z buffers. According to the example, using a logarithmic z buffer is recommended for greater distances. Since you're working on modeling planets, this could be the root of the issue you're facing.

Check out the example and compare it to the left side (normal z buffer) of the sketch. If they look similar, then using a logarithmic z buffer like the one in the linked example might solve your problem. I hope this information is useful for you!

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

Execute an asynchronous request using Javascript to communicate with a Spring Controller

I've created a JSP page that includes some JavaScript code: function sendData(tableID) { var table = document.getElementById(tableID); var dataArray= new Array(); for (var i = 1;i<table.rows.length; i++){ var row = table. ...

Checking if a variable is true with jQuery

Check out this snippet of code: <main class="ok">My text</main> <script> $(document).ready(function() { if ( $('body').not('main.ok') ) { // or if ( Boolean ( $('main.ok') ) == false ) { ...

Error message "The camera provided is invalid and not an instance of THREE.Camera" appears when attempting to render a cube using Three.js

I've been working on a small "engine" project using three.js to easily create and position objects. So far, I have set up the scene, renderer, and camera successfully. However, when attempting to render and attach a cube to my scene, I encounter an is ...

having difficulty sending the username and password from the HTML page to the controller in AngularJS

In my AngularJS controller, I am having trouble retrieving the values of the username and password fields after submitting the login form. Here is the HTML code for the form: <form class="form-signin" action="" method="post"> ...

The functionality of the Jquery mobile panel ceases to work properly when navigating between pages within a multi-page

Within a multi-page template setup in a jQuery mobile application, an issue arises after navigating away from the first page using panel navigation. Upon returning to the first page through another form of navigation, the panel appears "hanged." Upon clos ...

Sharing specific information with a particular component instance in React using .map

I have set up multiple instances of a child component within a parent component using the following code: render() { return ( {this.state.accounts.map(account => <EachAccount key={account.id} curAccountData={account} /> ...

What is the best way to establish a new JavaScript data type that can be accessed using both key and index?

Looking to create a unique datatype or object in JavaScript that allows access by both index and key, as well as having a length property to display the number of items in the datatype. Something along the lines of: MyDataType[0].name="John" MyDataType[0 ...

I'm encountering problems when attempting to display the image/png response from an xmlHTTPRequest. Instead of the correct data, I

I have implemented the following code to integrate a captcha generating web service. The response data is successfully obtained, but when I attempt to display the result within a div element, the image appears as distorted text. var xmlHttp = new XMLHtt ...

Encountering a no-loops/no-loops eslint error in node.js code while attempting to utilize the for and for-of loops

While working on my nodejs application, I have encountered an issue with es-lint giving linting errors for using 'for' and 'for-of' loops. The error message states error loops are not allowed no-loops/no-loops. Below is the snippet of c ...

Utilize both a model and a boolean variable within expressionProperties in Formly configuration

I'm having trouble setting formly form fields to disabled based on model properties and a boolean variable. The code snippet I've tried doesn't seem to be working as expected. expressionProperties: { 'templateOptions.disabled' ...

How can I retrieve routing parameters in a Vue.js/Nuxt/TypeScript app?

In the process of developing my website based on the Nuxt TypeScript Starter template, I've encountered a challenge. Specifically, I have created a dynamically routed page named _id.vue within my pages folder and am looking to access the id property i ...

Is `send()` considered an anonymous function?

I am testing ajax on a local server, but encountering an issue where the console is showing that send() is being identified as an anonymous function and I am receiving a blank document. The console displays: XHR finished loading: GET "http://localhost/js ...

I'm new to Angular, so could you please explain this to me? I'm trying to understand the concept of `private todoItems: TodoItem[] = []`. I know `TodoItem` is an array that

//This pertains to the todoList class// The property name is todoItems, which is an array of TodoItem objects fetched from the TodoItem file. I am unable to make it private using "private todoItems: TodoItem[] = []," is this because of Dependency Injectio ...

React Native: Implementing scroll-based header animations in ScrollView

Currently, I am working on implementing an animated header with ScrollView on the screen. In this implementation, I need to set my view based on the Y position of the ScrollView during scrolling. This is how it's done: const onScroll = ({ nativeEvent: ...

Utilizing a JSON file for long-term storage of a compact database in JavaScript

As a newcomer to JSON, I wanted my webpage to display a small database of records in a table without using a traditional database like MySQL. Instead, I decided to read data from and write it out to a JSON file for convenient and persistent storage on my w ...

Guide on inserting HTML text box form input into Express route parameter

I'm currently working on implementing a feature that allows users to search through my mongo database using an endpoint structured like this: app.get('/search/:input', function(req, res){ console.log(`get request to: /members/${req.params ...

The issue of resolving NestJs ParseEnumPipe

I'm currently using the NestJs framework (which I absolutely adore) and I need to validate incoming data against an Enum in Typscript. Here's what I have: enum ProductAction { PURCHASE = 'PURCHASE', } @Patch('products/:uuid&apos ...

Discover the secrets to easily finding specific items within a table by harnessing the power of the table sorter plugin with

Currently, I am utilizing the tablesorter plugin and its corresponding tablesorter widgets to facilitate searching within a table. However, I encountered an issue with the tablesorterWidgets.js file when attempting to configure the number of rows displayed ...

Is there a way to automatically update the button text based on the route name without requiring user interaction?

I need to dynamically change the text on a button in my header based on the current route. When on the login page, the button should say "Signup" and when on the signup page, it should say "Login". I want this text change to happen without having to click ...

Utilizing an object map to pass objects as props without losing their keys

I have an array of objects named obj, structured as follows: { "root": [ { "key": "mihome", "label": "home", "action": "url", ...