How can ThreeJS utilize CSS3Renderer and WebGLRenderer to display two objects in the same scene and achieve overlapping?

JSFiddle :

http://jsfiddle.net/ApoG/50y32971/2/

I am working on a project where I am using CSS3Renderer to render an image as a background projected as a cube in the app. However, I also want to incorporate objects rendered using WebGLRenderer in the middle, which I plan to make clickable using ray detection.

Currently, I am facing an issue where the wireframe rendered by WebGLRenderer (the wireframe of a randomly generated square in the middle) only shows up when the background is rotated to have a white background. When the background is set to the CSS3Rendered clouds, the wireframe disappears. Is there a way to keep the wireframe visible regardless of the background?

My end goal is to have multiple floating objects (such as 3D text) with links as object properties that can be clicked using rays. I prefer using CSS3 renderer as it renders well on mobile compared to canvasrenderer. Any ideas or suggestions would be greatly appreciated as I am currently stuck on this. You can find my fiddle here:

http://jsfiddle.net/ApoG/50y32971/2/

var camera, scene, renderer;
var scene2, renderer2;
var controls;
init();
animate();

function init() {
  camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
  camera.position.set(200, 200, 200);
  controls = new THREE.TrackballControls(camera);
  scene = new THREE.Scene();
  scene2 = new THREE.Scene();
  var material = new THREE.MeshBasicMaterial({
    color: 0x000000,
    wireframe: true,
    wireframeLinewidth: 1,
    side: THREE.DoubleSide
  });
  //

  //
  for (var i = 0; i < 1; i++) {
    var element = document.createElement('div');
    element.style.width = '100px';
    element.style.height = '100px';
    element.style.opacity = 0.5;
    element.style.background = new THREE.Color(Math.random() * 0xffffff).getStyle();
    var object = new THREE.CSS3DObject(element);
    object.position.x = Math.random() * 200 - 100;
    object.position.y = Math.random() * 200 - 100;
    object.position.z = Math.random() * 200 - 100;
    object.rotation.x = Math.random();
    object.rotation.y = Math.random();
    object.rotation.z = Math.random();
    object.scale.x = Math.random() + 0.5;
    object.scale.y = Math.random() + 0.5;
    scene2.add(object);

    var geometry = new THREE.PlaneGeometry(100, 100);
    var mesh = new THREE.Mesh(geometry, material);
    mesh.position.copy(object.position);
    mesh.rotation.copy(object.rotation);
    mesh.scale.copy(object.scale);
    scene.add(mesh);
  }
  //
  var sides = [{
    url: 'http://threejs.org/examples/textures/cube/skybox/px.jpg',
    position: [-1024, 0, 0],
    rotation: [0, Math.PI / 2, 0]
  }, {
    url: 'http://threejs.org/examples/textures/cube/skybox/nx.jpg',
    position: [1024, 0, 0],
    rotation: [0, -Math.PI / 2, 0]
  }, {
    url: 'http://threejs.org/examples/textures/cube/skybox/py.jpg',
    position: [0, 1024, 0],
    rotation: [Math.PI / 2, 0, Math.PI]
  }, {
    url: 'http://threejs.org/examples/textures/cube/skybox/ny.jpg',
    position: [0, -1024, 0],
    rotation: [-Math.PI / 2, 0, Math.PI]
  }, {
    url: 'http://threejs.org/examples/textures/cube/skybox/pz.jpg',
    position: [0, 0, 1024],
    rotation: [0, Math.PI, 0]
  }, {
    url: 'http://threejs.org/examples/textures/cube/skybox/nz.jpg',
    position: [0, 0, -1024],
    rotation: [0, 0, 0]
  }];
  for (var i = 0; i < sides.length - 1; i++) {
    var side = sides[i];
    var element = document.createElement('img');
    element.width = 2050; // 2 pixels extra to close the gap.
    element.src = side.url;
    var object = new THREE.CSS3DObject(element);
    object.position.fromArray(side.position);
    object.rotation.fromArray(side.rotation);
    scene2.add(object);
  }

  //
  renderer = new THREE.WebGLRenderer();
  renderer.setClearColor(0xf0f0f0);
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);
  renderer2 = new THREE.CSS3DRenderer();
  renderer2.setSize(window.innerWidth, window.innerHeight);
  renderer2.domElement.style.position = 'absolute';
  renderer2.domElement.style.top = 0;
  document.body.appendChild(renderer2.domElement);
}

function animate() {
  requestAnimationFrame(animate);
  controls.update();
  renderer.render(scene, camera);
  renderer2.render(scene2, camera);
}

Answer №1

Ensure that you are using both CSS3DRenderer and WebGLRenderer on separate canvas elements.

Make sure that the background of the WebGLRenderer is set to be transparent by following this example:

renderer = new THREE.WebGLRenderer( { alpha: true } ); // must be included
renderer.setClearColor( 0x000000, 0 ); // default setting
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

renderer2 = new THREE.CSS3DRenderer();
renderer2.setSize( window.innerWidth, window.innerHeight );
renderer2.domElement.style.position = 'absolute';
renderer2.domElement.style.top = 0;
document.body.appendChild( renderer2.domElement );

Version: three.js r.120

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

In what way does ReactJS enable me to utilize constant functions before they are declared?

I'm intrigued by the concept of using a value before defining it in ReactJS. Let's explore this through an example: function CounterApp() { const [counter, setCounter] = useState(0); const increaseValueTwice = () => { increaseValue() ...

The function type '() => JSX.Element' cannot be assigned to the type 'ReactNode'

I am encountering an issue while trying to display a component using an object. The code is throwing an error: const Login = () => <>login</> const publicRoutes = [ { path: '/login', component: Login } ] function AppR ...

What is preventing me from choosing the particular Game object?

I am looking to enhance my gaming experience, but I am encountering an issue with my selection process that is causing an exception to be thrown. My desired SQL query is as follows: Select * from Games where userId = 3 order by createdAt desc gameControl ...

Determine the percentage using jQuery by considering various factors

I am facing an issue with the following code snippet: <script type="application/javascript"> $(document).ready(function () { $("#market_value").on("change paste keyup", function() { var market_value = par ...

Message displayed within Ng-repeat loop

Having trouble implementing a tooltip within an ng-repeat for each item in a td element. Whenever the mouse hovers over an item inside the td, I want a tooltip to display with more details. The code below shows my setup, using ng-if to prevent displaying e ...

Steps for retrieving a Unicode string from PHP using AJAX

In order to retrieve Unicode strings from PHP for my project, I figured that using AJAX would be the most suitable method. $.ajax({ url: './php_page.php', data: 'action=get_sum_of_records&code='+code, ...

What is the best way to establish a client.js file for socket.io in Node.js?

I am looking to separate the client script from the index.html file in the chat application example found at https://socket.io/get-started/chat/. Currently, I am facing an issue where the message "a user connected" is not appearing when I refresh the webpa ...

Guide to implementing sliding effects with Jquery

I recently dived into the world of JavaScript and jQuery. I crafted a piece of JavaScript code for client-side validation. document.getElementById(spnError).style.display = 'block'; Currently, I am displaying a span element if any validation er ...

React Hook Form: Reset function triggers changes across all controllers on the page

I encountered an issue with using the reset function to clear my form. When I invoke the reset function, both of my form selects, which are wrapped by a Controller, get cleared even though I only defined default value for one of them. Is there a way to pr ...

I am using a Next.js app where users can upload images through an API, and the API returns the images to me in an array. I am trying to figure out how

Instead of using CKEditor to upload images, I have implemented my own API for image uploading. When a user uploads an image on my website, I make a call to the API to safely upload the image and return a URL in the Public Domain. The API returns the follo ...

Verifying the presence of a value within an SQL table

I am currently working on developing a bot that requires me to save the commandname and commandreply in a database. Right now, I am using mySQL Workbench for this task. My goal is to verify if the commandname provided by the user already exists in the tab ...

Within the materia-ui table, I am facing an issue where clicking the button to expand a row results in all rows expanding. I am seeking a solution to ensure only the selected row expands

When a row is clicked, all rows in the data table expand to show inner data for each row. The issue is that clicking the expand button expands all rows rather than just the selected row. Each time I try to expand one specific row, it ends up expanding mul ...

Passing JSON data with special characters like the @ symbol to props in React can be achieved

Using axios in React, I am fetching JSON data from a database. I have successfully retrieved the JSON data and stored it in state to pass as props to child components within my application. However, the issue arises when some of the objects in the JSON s ...

Leverage nearby data using MediaSource

I am currently working on creating a local video player using nwjs (node-webkit). I have successfully played local files by setting their path as the src attribute of the video element. Now, I want to explore using MediaSource and potentially URL.createObj ...

Get multiple increment buttons functioning

I've managed to create an increment counter that updates the value in a .txt file on my server every time I click a button by +1. It's working flawlessly and here's how it looks like: <!DOCTYPE html> <html> <head> <meta ...

How can I adjust the vertical position of Material-UI Popper element using the popper.js library?

https://i.stack.imgur.com/ZUYa4.png Utilizing a material-ui (v 4.9.5) Popper for a pop-out menu similar to the one shown above has been my recent project. The anchorElement is set as the chosen ListItem on the left side. My goal is to have the Popper alig ...

The functionality of Vue is acting up with the HTML, unexpectedly refreshing when a button is clicked

I am currently experiencing an issue with my Vue application. When I click the button, it clears the input field (which it shouldn't) and doesn't perform any other actions. The variables "codigo" and "payload" do not display anything on the scree ...

What steps are involved in forming a JavaScript promise that resolves into a thenable object?

When using the faye browser client with promises, I encountered an issue with a function that creates a faye client after performing an asynchronous operation: function fayeClient() { return doSomethingAsychronous().then(function() { var faye_client ...

Fire an event in JavaScript from a different script file

I have created a JavaScript file to handle my popups. Whenever a popup is opened or closed, I trigger a custom event like this: Script File #1 $(document).trigger('popupOpened', {popup: $(popupId)}); If I want to perform an action when the tri ...

Unable to resubmit form via ajax more than once

Greetings to all, I seem to be encountering some issues with a supposedly simple form submission using ajax. Upon the initial submission of the form by the user, everything proceeds smoothly: The content within the div changes as expected and the PHP proc ...