How can you determine which of three intersecting three-dimensional objects should be displayed?

Let's imagine a scenario where multiple objects intersect at the same location. How can we specify which one to display in order to avoid constant flickering?

http://jsfiddle.net/GYQ5v/187/

var scale = 1;

var scene = new THREE.Scene();

var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 100, 1000000);
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 100 * scale;
var renderer = new THREE.WebGLRenderer({
    antialias: false
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

var light = new THREE.AmbientLight(0xFFFFFF);
scene.add(light);

var material = new THREE.MeshBasicMaterial({
    transparent: false,
    side: THREE.DoubleSide,
    fog: false,
    color: 0xFFFF00,
    opacity: 1.0
});

var cubeGeometry = new THREE.PlaneGeometry(50 * scale, 50 * scale, 1, 1);
var cubeMesh = new THREE.Mesh(cubeGeometry, material);
cubeMesh.position.set(50, 0, 0);
scene.add(cubeMesh);

var cubeGeometry = new THREE.PlaneGeometry(50 * scale, 50 * scale, 1, 1);
var cubeMesh = new THREE.Mesh(cubeGeometry, material);
cubeMesh.position.set(-50, 0, 0);
scene.add(cubeMesh);

var cubeGeometry = new THREE.PlaneGeometry(50 * scale, 50 * scale, 1, 1);
var cubeMesh = new THREE.Mesh(cubeGeometry, material);
cubeMesh.position.set(0, -50, 0);
scene.add(cubeMesh);

var cubeGeometry = new THREE.PlaneGeometry(50 * scale, 50 * scale, 1, 1);
var cubeMesh = new THREE.Mesh(cubeGeometry, material);
cubeMesh.position.set(0, 50, 0);
scene.add(cubeMesh);

var material = new THREE.MeshBasicMaterial({
    transparent: false,
    side: THREE.DoubleSide,
    fog: false,
    color: 0xFF0000,
    opacity: 1.0
});

var cubeGeometry = new THREE.PlaneGeometry(100 * scale, 100 * scale, 1, 1);
var cubeMesh = new THREE.Mesh(cubeGeometry, material);
scene.add(cubeMesh);

function render() {
    var time = Date.now() * 0.5;
    camera.position.x = Math.sin(time / 1000) * 150 * scale;
    camera.position.y = 0;
    camera.position.z = Math.cos(time / 1000) * 150 * scale;
    camera.lookAt(scene.position);
    renderer.render(scene, camera);  
    requestAnimationFrame(render);
}
render();

Attempted to include renderer.sortObjects = false; - but observed no changes

Answer №1

The issue you are witnessing is known as z-fighting: check out this informative resource on z-fighting: https://en.wikipedia.org/wiki/Z-fighting.

To improve z-buffer precision, consider adjusting your camera's near and far plane values to 0.1 and 1000, respectively. Using a value of 1000000 is excessive.

For related inquiries, you may find these questions helpful: How to correctly render coincident polygons in OpenGL (ES) and three.js - Overlapping layers flickering.

Answer №2

If you wish to modify the position of the cube, you should replace

cubeMesh.position.set(50, 0, 0); 

with

cubeMesh.position.set(50, 0, 1);

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

Creating an XML response to generate an HTML dropdown menu in PHP

My onChange function in javascript calls a PHP file to fetch UPS rates and update an HTML dropdown list. Everything was working fine until I needed to add an item to the options list based on a comparison. Javascript: function fetch_UPS(el){ var zip = ...

The Parcel build was unsuccessful due to an error from @parcel/resolver-default: The file '../../../../../css/main.css' could not be loaded within the './src' directory

I utilize [email protected]. Whenever I attempt to load an external css or javascript file in my index.html, Parcel consistently fails to build with the following error: Build failed. @parcel/core: Failed to resolve './css/main.css' from & ...

Guide on switching the theme color in c# aspnet core using a toggle button within a partialview

Description I am looking to change the color scheme of my Bootstrap 5.3 theme by clicking a button within a partial view. The toggle button is customized to meet my specific requirements, and the chosen value is stored in a cookie for future reference. ...

Trouble updating outside controller data while using AngularJS directive inside ng-repeat loop

I am currently working with a isolate scope directive that is being used inside an ng-repeat loop. The loop iterates over an array from the controller of the template provided below: <!DOCTYPE html> <html> <head> <link rel="sty ...

Loop through the JSON data to generate clickable links in the innerHTML

I've been searching through various resources for a solution to the issue I'm facing. My goal is to iterate through a JSON object and extract all the ids and corresponding dates from each top_worst section. The structure of the JSON data is as fo ...

The positioning of the menu icons varies

When it comes to displaying the search icon in the WordPress toggle bar, I use a custom JavaScript file. This setup is mainly focused on website design and menu functionality. Additionally, I have integrated a newsletter subscription plugin for managing su ...

JQUERY inArray failing to find a match

I'm at my wit's end working with inArray and I'm really hoping for some help. I am using AJAX to fetch userIDs from my database, which come through as a JSON-encoded array. However, no matter what I try, I always get a -1 when trying to mat ...

Using AngularJS to retrieve DOM elements within a directive's controller

Currently, I am in the process of creating a custom image carousel directive using only AngularJS and no additional libraries. simpleCarousel.$inject = []; function simpleCarousel() { var directive = { restrict: 'E', templat ...

Every time I try to call the event in jQuery for the second time, it fails to work

I am currently working on a website that features an image gallery powered by the jquery wookmark plugin with filtering capabilities. Recently, I integrated the colorbox plugin (which was provided as an example by wookmark) to enhance the user experience. ...

Enhance the attributes of a collection of objects using values from a different array

Looking for a way to enhance a set of objects with properties sourced from another array? I have two arrays at hand. The first one contains a series of objects, while the second one consists of integers. My goal is to attribute a new property to each objec ...

Positioning oversized images in a React Native application

Looking to showcase two images side by side using React Native, where I can customize the screen percentage each image takes up. The combined size of the images will exceed the horizontal screen space available, so I want them to maintain their original di ...

Enhancing Performance with Web Workers in Angular CLI

Lately, I've been tackling a challenging issue with my Angular app - heavy computation on the client side leading to UI blockage. My solution? Utilizing Web Workers in my Angular CLI project to separate UI tasks into one thread and heavy processing in ...

Insert a numerical value into a list to make a series of numbers whole

I currently have an array of objects that looks like this: var arr = [ { "code": "10", }, { "code": "14", } ] My goal is to expand this array to contain 5 elements. The numbers should ran ...

Error encountered in AngularJS when utilizing the Flickr API with the parameter "nojsoncallback=1": Unexpected token Syntax

My AngularJS application is trying to access the Flickr API. I need the data in RAW JSON format without any function wrapper, following the guidelines provided in the documentation by using &nojsoncallback=1. However, I keep encountering a console er ...

Unable to link to 'mdMenuTriggerFor' as it is not a recognized attribute of 'button'

During the execution of my angular application using ng serve, I encounter the following error: Can't bind to 'mdMenuTriggerFor' since it isn't a known property of 'button' Despite importing all the necessary components, I a ...

Having trouble with a JavaScript function and tag that don't seem to be working in Chrome, but function well in Mozilla Firefox. Can anyone provide assistance with

Below is the code for creating two dropdowns: one for "Property Type" and the second for "Property Sub Type". This code works fine in Firefox but not in Chrome. When selecting "residential" as the property type, only residential values will appear in the ...

Using Angular Material for creating tabs with identical content

I am using material with angularjs and have encountered an issue with my two md-tabs. Both tabs have a similar DOM structure, but contain some unique content. Instead of duplicating the common DOM twice, I am looking for an alternative solution. Is it poss ...

Organizing an array in JavaScript that includes both version numbers and letters, with each letter representing a specific numerical value

If given an array similar to this one: input = [ "1.1", "1.c", "1.b", "1", "D", "b", "4", "2.1.2", "5.1", "3", & ...

reduce the size of the image as the browser width decreases

Struggling with a webpage layout that features an image on the left and content area on the right, both with fixed widths. When reducing browser width, the image should not shrink but be cropped from the left side instead. Any solutions for this issue? ...

Combine all div elements to create a unified image and then proceed to save it as a jpg file before uploading to the server

These are the divs below: <div style="width:800px; height:420px; position:absolute; top:0px; left:0px; z-index:10; background-image: url('https://3t27ch3qjjn74dw66o1as187-wpengine.netdna-ssl.com/wp-content/uploads/2016/05/052516-800x420-vege-Wallp ...