Utilizing the zIndex property in webGL programming

I am currently working on a three.js program

My main goal is to display a 2D panel in front of the three.js scene for debugging purposes.

  • I plan to achieve this by creating a div element and adjusting its z-index to show it above the three.js canvas.

Unfortunately, my 2D panel is not appearing as expected.

My question is:

Is my approach of using a div element correct for displaying content over webGL, or should I consider integrating the panel directly into the webGL rendering?

Below are snippets of my source code:

import React,{useRef,useState,useEffect} from 'react'
import axios from 'axios';

import styles from "../css/test-styles.module.css";

import * as THREE from 'three';
import TrackballControls from 'three-trackballcontrols';
import three_test from "../images/three_test.jpg";


function ThreeDebugInfo(){
    return <div style={{width:"500px",zIndex:100}}>testtesttest</div>
}

function GlCanvas() {
    const refContainer = useRef(null);
    useEffect(() => {
      // === THREE.JS CODE START ===
      var scene = new THREE.Scene();
      var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
  
      var renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth /100 * 80, window.innerHeight / 100 * 80);

      refContainer.current && refContainer.current.appendChild( renderer.domElement );

      camera.position.z = -500;
     
      const loadTex = async () =>{
          const texLoader = new THREE.TextureLoader();
     
          var  texture = new THREE.TextureLoader().load(three_test);
          console.log("check texture is loaded:",texture);
          var ground_geo = new THREE.PlaneGeometry( 3309, 2339, 8, 8 );
          ground_geo.translate(0, 0, -50);
          var ground_mat = new THREE.MeshBasicMaterial({ map: texture });

          var ground = new THREE.Mesh(
              ground_geo,
              ground_mat
          );

          scene.add( ground );
  
          var cube_geo = new THREE.BoxGeometry(100, 100, 100);

          var cube_mat = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
          var cube = new THREE.Mesh(cube_geo, cube_mat);
          scene.add(cube);
         
          

          const controls = new TrackballControls(camera, renderer.domElement);
       
          camera.position.z = 5;
          controls.rotateSpeed = 5.0;
          controls.zoomSpeed = 1.2;
          controls.panSpeed = 0.3;
          controls.update();
           
          const animate = function () {

            requestAnimationFrame(animate);
           
            controls.update();
            renderer.render(scene, camera);
          }
          animate();
      
  
      }
      loadTex();
  
    
    }, []);
    return (
        
        <div style={{zIndex:1}} ref={refContainer}></div>
   
    );
}

function ThreePage() {
    return (
        <React.Fragment>
        <GlCanvas></GlCanvas>
        <ThreeDebugInfo></ThreeDebugInfo>
        </React.Fragment>
    );
}

export default ThreePage

Answer №1

If the z-index value isn't working as expected, try changing the CSS position to something other than static on the element that has the z-index property.

position: static

When the position is set to static, the element follows the normal flow of the document and the properties like top, right, bottom, left, and z-index have no effect.

This is the default behavior for elements.

Source: https://developer.mozilla.org/en-US/docs/Web/CSS/position#static

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

Utilize Object by referencing a string

I am trying to access an object from my node backend in React by using a string as a key. For example, if the string is 'Cat', I want to access the Cat object along with its corresponding key/value pairs. Here is an illustration of what the code ...

Is there a reason for the absence of the Revit category attribute in the JSON response retrieved from the GET request :urn/metadata/:guid/

After receiving the information from the endpoint regarding Revit Models uploaded to my bucket, I noticed that the JSON response contains multiple objects. These objects seem to represent Revit elements, each with all parameters except for the Revit Categ ...

Tips on preventing unexpected growth of rect width or height when snapping in Konva

While trying to resize a rectangle in order to make it into a perfect square, I encountered an issue where the height or width of the rectangle would unexpectedly grow. This can be seen in the GIF below: https://i.sstatic.net/SBFZR.gif You can view the c ...

How can I attach events to newly generated elements without using jQuery?

If I want to dynamically add a form to my webpage (through AJAX or other JavaScript methods), how can I attach event listeners to these elements before they actually exist on the page? When using jQuery, it's simple to bind the events to a parent ele ...

Why does Jquery refuse to accept hyphens in the JSP page?

I received a field from SERVICE that contains a hyphen. For example, first-name (as a JSON object). However, when I attempt to retrieve the value of this field in my JSP file, I encounter a script error. Can you please advise me on how to properly access ...

Leveraging Firebase for data storage in creating XML files

According to my understanding of the firebase documentation, data that is inputted is converted into JSON format. The easiest way to connect to firebase is through the use of javascript. However, is it feasible to utilize the JSON data in firebase to gen ...

Having difficulty automatically populating a textarea with the chosen option from a datalist

Is there a way to automatically populate the Address field of a client in a textarea based on the input of the client's name in an input field? I have created a 'for loop' to retrieve a datalist of client names. For the address, I retrieved ...

Exploring the integration of query parameters in Postman using mongoose

In my code, I have a driver.js file that holds a driver schema, and a driverController.js file with REST methods including GET, POST, DELETE, and PUT. What I want to achieve is to send a GET request to http://localhost:3000/drivers?available=true This re ...

Struggling to connect with PouchDB within my HTML-based Web application

I am looking to integrate pouchDB into my WebApp so that upon clicking a button, the data from a JSON file will be saved to pouchDB. In the initial stage in my index.html, I included the following: <script type="module" src="pouchdb/packa ...

Reformatting function transforms the structure of an array containing objects

I have been struggling with certain issues and would like to find the most effective solution. Despite using map and reduce, I have not been able to achieve the desired results. Any help would be greatly appreciated. Consider the following INPUT Array str ...

Interactive table on click

Seeking assistance with an issue involving a combobox (select) and a dynamic table generated from PHP. The table displays names along with their Firstname, Lastname, and ID (hidden). Upon clicking on a row in the table, I should retrieve the ID of that spe ...

Is it possible to arrange a react map based on the length of strings?

I am working on a React function that loops through an object and creates buttons with text strings retrieved from the map. I want to display the text strings in order of their length. Below is the code snippet for the function that generates the buttons: ...

Order of execution and loading of JavaScript files in Webkit threading

I'm currently working on creating an XSS widget and I'm encountering some issues specifically with Webkit browsers when loading external JavaScript files that I'm appending to the DOM. Here's how it's set up: Widget.js appends 3 ...

Incorporate a line break into a document using JavaScript

Is there a way to make all the items inside document.get on new lines without using paragraph elements? I have tried br/ and \n, but they do not work as expected. They either cause issues with JavaScript execution or fail to create new lines. <scr ...

Following a successful login, the User ID fails to store in the session

I am a beginner in the world of coding and I am currently working on one of my first apps. I have encountered an issue with my log in function. After user registration, the user id is successfully saved in the session object. However, it does not save into ...

The term 'TextInput' is not recognized in React Native and cannot be found

I'm currently working on setting up a login screen for a social media app that I am developing. The issue I am facing is that I have a LoginScreen.js and RegisterScreen.js with forms for email and password, but when I try to render them, I encounter a ...

Ways to rearrange items in the navigation bar?

I am working with a Bootstrap template and I am trying to adjust the layout to be right-to-left (RTL). Currently, when I set the site title to RTL in the navbar, the buttons in the navbar also switch to RTL alignment. This is not the desired layout I want. ...

Absence of reactions visible

I am eagerly awaiting responses to a message. The member begins typing, then the bot responds, and the user is expected to react to the bot's message. I attempted to execute this code, however, when it reached the console.log("OK"), nothing happened. ...

The Angular single-page application is experiencing issues with the ngResource dependency and

When trying to use ngResource in Angular, I encountered an issue where adding the dependency caused blank pages to display. I have included the script reference, but it's still not functioning correctly. What steps should I take to resolve this? /*va ...

The number field is persisting with its content even after submitting, while the other fields successfully clear up

I am facing an issue where all fields clear up after submission except for the number field. I would appreciate any help on why this is happening and how to fix it. Thank you. Textfield number: const [number, setNumber] = useState(""); const han ...