Using more than one WebGLRenderer on a shared canvas

I am attempting to have multiple WebGLRenderer instances render on the same canvas. It seems to work initially for the first frame, but when it comes to animating, things start to break down. I'm curious if there are any limitations with this approach?

Below is the sample code that is causing issues when using requestAnimationFrame inside the update function.

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, WIDTH / HEIGHT, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
// + scene content

const scene2 = new THREE.Scene();
const camera2 = new THREE.OrthographicCamera(-WIDTH / 2, WIDTH / 2, HEIGHT / 2, -HEIGHT / 2, 0, 30);
const renderer2 = new THREE.WebGLRenderer({ canvas: renderer.domElement });
renderer2.autoClear = false;
// + scene2 content

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

https://codesandbox.io/s/l75w6qyw2m?module=%2Fsrc%2Findex.js

I'm uncertain about what outcomes to expect, but I am determined to get this working. I need separate renderers for rendering multiple scenes with different cameras, and I aim to achieve a more user-friendly interface for my plugin.

Answer №1

Having 2 WebGLRenderers on one canvas is not feasible due to the limitation of each canvas having only ONE WebGLContext. Creating a second context is not possible within this constraint.

If you are looking to render two scenes overlapping each other, it can be achieved with a single renderer by turning off its auto-clear property and rendering the scenes sequentially:

renderer.autoClear = false;

function update() {
  renderer.render(scene, camera);
  renderer.render(hudScene, hudCamera);
  requestAnimationFrame(update);
}

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

"Implementing a button in a flask data table: A step-by-step guide

Hello, I am facing an issue with adding a button to a Bootstrap datatable in Flask (Python) using JavaScript. Any tips or solutions would be greatly appreciated. I am trying to achieve something like this: https://i.sstatic.net/NtaB3.png I have attempted ...

Why does Safari browser keep showing NAN instead of a value?

In my quest to calculate the difference between two times in Safari browser, I encountered an issue. The code works perfectly fine in Chrome, but in Safari, it returns NAN. When I run the application for the first time, I save today's date. On subsequ ...

How can a producer know when it's time to send a message in NodeJS using ZeroMQ?

After conducting some research on patterns supported by zeromq, I have encountered an issue with the PUB/SUB pattern in my recent project as well as the PUSH/PULL pattern. I am using NodeJS for the zeromq implementation. In my examples (server.js & client ...

Tips for creating an Alexa skill that can vocalize JSON information using node.js

Currently, I am working on a project to develop an Alexa skill that can read out the headlines from the Google News API. I have obtained a JSON URL and now I am trying to create a function that can extract and read the titles using the Alexa device. Here ...

"Exploring the Power of ZF2 with Restful APIs and Image

I am currently in the process of developing a website utilizing Zend Framework 2 in combination with AngularJS. The backend consists of a restful webservice running on ZF2, while AngularJS is used on the client side to interact with this webservice. My ne ...

One way to retrieve this attribute with jQuery is by specifying the div element in question

I am facing an issue with a div that is defined within a particular context. Let's consider the div as shown in the code snippet below: <td itemid='desired number'> <div>div 1</div> <div class="action">div 2</ ...

Managing promise chain within the constructor of a React component

I am experiencing an issue with my API data fetching process. The challenge occurs because the rendering takes place before the constructor has completed fetching the data. As a result, the this.todo being sent to the List component ends up being empty. ...

Creating a factory class in Typescript that incorporates advanced logic

I have come across an issue with my TypeScript class that inherits another one. I am trying to create a factory class that can generate objects of either type based on simple logic, but it seems to be malfunctioning. Here is the basic Customer class: cla ...

Utilize OpenLayer3 to showcase Markers and Popups on your map

I am currently exploring how to show markers/popups on an osm map using openlayers3. While I have come across some examples on the ol3 webpage, I'm interested in finding more examples for coding markers/popups with javascript or jquery, similar to som ...

Tips for retrieving a selected date from an HTML textbox labeled as "Date"

My goal was to find the differences between two dates by utilizing an HTML Date textbox. <input type="text" name="datein" id="datein" value="" class="inputtexbox datepicker" style="display: none" is-Date/> <input type="text" name="dateto" id=" ...

Utilize a class method within the .map function in ReactJS

In my ReactJS file below: import React, { Component } from "react"; import Topic from "./Topic"; import $ from "jquery"; import { library } from '@fortawesome/fontawesome-svg-core' import { FontAwesomeIcon } from '@fortawesome/react-fontaw ...

Integrate Vue.js into a traditional multi-page application

I recently integrated Vue.js into my CMS-backed website to handle a specific UI feature involving tabs that switch content on the page. Now, I'm looking for a way to make these tabs deeplinkable so that a specific tab is active when the page loads. Wh ...

Click on a plane within a three.js environment to determine the X Y position within it

When using a raycaster, I can successfully obtain the current object (in this case, a plane) under the mouse. However, I am seeking a more precise X and Y value for the mouse position INSIDE the plane. var vector = new THREE.Vector3( ( event.clientX / win ...

The messaging feature is not functioning properly within the page tab

The iframe page tab is not compatible with the Send dialog. I followed the example on the 'Send Dialog' page https://developers.facebook.com/docs/reference/dialogs/send/ <html xmlns:fb="https://www.facebook.com/2008/fbml"> <body> ...

While using Ckeditor, typing or deleting text will cause an input checkbox to become unchecked and its value to be altered

Whenever I try to make any changes or use the backspace key in Ckeditor, the checkbox gets unchecked and the value resets to 0. Despite my efforts, I have been unable to find a solution to this issue. How can I resolve this problem? CKEDITOR.replace( &ap ...

Can someone help me figure out how to trigger my function after my jQuery 'each' function has completed along with all the AJAX calls it includes?

Hello all, seeking some guidance here. I have experimented with various solutions from SO and other sources. This particular one caught my attention as I tried to ensure that my function wouldn't execute until all ajax calls were completed. However, I ...

An error occurred stating "No matching closing tag found for "<%" when attempting to include the file

While attempting to include other .ejs files using the same syntax, everything works perfectly except when including my _show.ejs file. I am unsure where the issue lies, whether it is in the index.ejs or _show.ejs file. This is my index.ejs File <!-- i ...

The constructor error in ng-serve signalizes an issue in

Currently, I am developing an AngularJS application. However, when attempting to start the dev server, I encountered an issue with my ng serve command: https://i.stack.imgur.com/QujSe.png ...

How to assign a locally created array in a function to a globally defined array in JavaScript

Whenever I try to assign arrays, a problem arises: var arr = [ {'id': 1, 'size': 'large' }, {'id':4, 'size': 'small'} ]; function adjust(a){ var result=[{'id': 1, 'si ...

Save a CSV file into a list, locate a specific key within the list, and retrieve the fourth value associated with that key

After thorough revision, the question now reads as follows: Take a look at this content in database.csv: barcode,ScQty, Name , Qty ,Code 123456 , 3 ,Rothmans Blue , 40 ,RB44 234567 , 2 ,Rothmans Red , 40 ,RB30 345678 , 2 ,Rothmans Gre ...