Showcasing a variety of scenarios where resources are being used across several canvases

I am currently working on a project using three.js to develop an interactive web application, but I have hit a roadblock:

Within the design, there are numerous canvases enclosed in draggable divs on the page. Each canvas is meant to showcase a 3D object with a unique material applied, utilizing custom shaders. These materials all rely on a shared texture (varying from blue-tinted to desaturated).

The quantity of canvases may fluctuate, commonly exceeding 20, which makes resource sharing, especially for large textures, extremely advantageous.

Thus far, I have been employing multiple renderers, cameras, and scenes, which has worked well except when attempting to utilize the same texture across various scenes.

Much of the materials use shared uniforms and attributes to prevent redundant information and maintain synchronization between them (i.e., changes in some materials should reflect uniformly across all).

I am curious if there is a way to share textures among different scenes/renderers/canvases? When attempting this, I receive the following error message:

WebGL: INVALID_OPERATION: bindTexture: object not from this context 

In my quest to troubleshoot this issue, I stumbled upon the suggestion that creating multiple viewports could resolve it, although I am uncertain how to display distinct viewports over diverse canvases.

TL/DR;

Is it possible to:

  • Show the same scene on different canvases?
  • Utilize the same uniforms (including a texture uniform) across different scenes, renderers, and/or canvases?

Thank you in advance!

Griffork

Answer №1

Regrettably, it is currently not possible to share resources across canvases. However, there are a few alternatives you can explore:

  1. Render the different viewports in different sections of the same canvas.

    For example:

  2. Create a canvas that spans the entire window, use placeholder elements to determine where to draw, utilize getClientBoundingRect to establish viewport & scissor settings for drawing scenes in each element.

    For example: Is it possible to enable unbounded number of renderers in THREE.js?

  3. Render the scene to an offscreen canvas and then transfer it to visible canvases.

    <canvas id="c1"></canvas>
    <canvas id="c2"></canvas>
    <script>
    var webglCanvas = document.createElement("canvas");
    var canvas1 = document.getElementById("c1");
    var ctx1 = canvas1.getContext("2d");
    var canvas2 = document.getElementById("c1");
    var ctx2 = canvas1.getContext("2d");
    

    ... draw the scene into webglCanvas from one view...

    // copy scene to canvas1
    ctx1.drawImage(webglCanvas, 0, 0);
    

    ... draw the scene into webglCanvas from another view...

    // copy scene to canvas2
    ctx2.drawImage(webglCanvas, 0, 0);
    

    Here's a live example (please note: It may be slow on Windows in Chrome26, FF20, but hopefully this will be resolved in future browser updates)

  4. Utilize OffscreenCanvas, transferToImageBitmap, etc. (note: this feature has not been implemented in any browsers as of 2018/1/8, but it can be accessed through a flag in Firefox)

const one = document.getElementById("one").getContext("bitmaprenderer"); 
const two = document.getElementById("two").getContext("bitmaprenderer");

const offscreen = new OffscreenCanvas(256, 256);
constr gl = offscreen.getContext('webgl');

// ... some drawing for the first canvas using the gl context ...
gl.clearColor(1,0,0,1);
gl.clear(gl.COLOR_BUFFER_BIT);

// Commit rendering to the first canvas
let bitmapOne = offscreen.transferToImageBitmap();
one.transferImageBitmap(bitmapOne);

// ... some more drawing for the second canvas using the gl context ...
gl.clearColor(0,1,0,1);
gl.clear(gl.COLOR_BUFFER_BIT);

// Commit rendering to the second canvas 
let bitmapTwo = offscreen.transferToImageBitmap();
two.transferImageBitmap(bitmapTwo);
<canvas id="one"></canvas>
<canvas id="two"></canvas>

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

Discover the steps to integrating jsfiddle into my local development environment

If you want to check out the code for this example, head over to jsfiddle at this link. And below is a snippet of the HTML file: <!DOCTYPE html> <html> <head> <script src="http://www.jacklmoore.com/colorbox/jquery.colorbox.js" type= ...

What is the best way to transform a Map<string, boolean> into an array of objects with key-value pairs {key: string, value: boolean

I'm currently working on a React project that incorporates TypeScript, and I've successfully implemented dynamic permission creation on the user creation page by utilizing this particular code snippet. The permissions being used on the page are m ...

Karma is unable to locate the module within the specified relative path

I'm facing a challenge with Karma not being able to load a specific file. As a novice in Karma, I dedicated the entire day to researching and checking documentation for similar issues with no luck. Upon initiating the karma process, it encounters an ...

Updating props in a recursive Vue 3 component proves to be a challenging task

I am facing an issue with two recursive components. The first component acts as a wrapper for the elements, while the second component represents the individual element. Wrapper Component <template> <div class="filter-tree"> &l ...

Trigger controller redirection in AngularJS 1.5.0 by clicking on <tr> tag

Currently, I am facing an issue with my web page where I use the ng-repeat directive to populate a table with a list of companies. My goal is to have a functionality where clicking on a table row opens another page with a form to edit the selected company. ...

Employ the useEffect hook to make a constant post request

I have encountered a perplexing issue while working on a basic To-Do list web application that utilizes a MongoDB database. Despite having the functionality in place, I noticed that my useEffect hook is continuously sending an excessive number of post requ ...

Utilizing JQuery's .Toggle function in conjunction with a stylish CSS transform button

I am trying to create a toggle menu that shows and hides itself without requiring the button to be clicked twice. I have added e.preventDefault(); which solves the issue, but now the button does not transform as intended. $(document).ready(function() { ...

Create a new button dynamically within an HTML table row using pure JavaScript programming techniques

Currently, I am retrieving JSON data from an API and then displaying this data in an HTML table using plain JavaScript. My goal is to dynamically add a button at the end of each row for additional functionality, but so far, I have been unable to figure out ...

Display HTML content in autocomplete using jQuery UI

I implemented a search feature on my website using jQueryUI, similar to how it works on Facebook. Below is the jQuery code: //search main function split( val ) { return val.split( ); } function extractLast( term ) { return split( term ).pop(); } ...

Unraveling nested elements with the array map() method in Angular2 and Typescript: Fixing the issue of undefined property reference while mapping

Hey there! I'm currently working with Angular 4 and I have a piece of code that parses data from an API into a TypeScript array of rows. It's important to note that the code functions properly if elements like 'item.tceCampRun' and &apo ...

How to create a vertically scrollable div within another div by utilizing scrollTop in jQuery

I need assistance with scrolling the #container div inside the .bloc_1_medias div. The height of #container is greater than that of .bloc_1_medias. My goal is to implement a function where clicking on the "next" and "previous" text will scroll the #contai ...

Express: Improperly formatted MIME type for background graphics

Currently encountering an issue with displaying background images specified in my CSS. Upon checking the web inspector, the following message is logged: Resource interpreted as Image but transferred with MIME type text/html. The background image functio ...

What is the best way to manage files in Vue.js?

I am faced with a challenge in storing image files and video files as Blob data. One thing I like is that the uploaded video and image files display instantly, which is pretty cool. I'm not entirely sure if the code below is correct - how can I navi ...

Navigating through nested objects in JSON when working with D3: a guide

Currently, I am in the process of learning D3 using various tutorials. Here is the code snippet I have been working with: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title ...

Comparing unique values between objects in JavaScript with varying keys

I'm facing a peculiar issue that I can't seem to solve. I have two variables named nft and web3.givenProvider. Both of them have an address value of "0x0000000000000", but they are stored under different keys - nft is under .seller and ...

The issue with the functionality of position absolute in CSS when used with JavaScript

<html> <head> <style> #wrapper{ position: absolute; top : 250px; width: 500px; height: 500px; border: 1px solid red; } .tagging{ position: absolute; border: 1px solid black; width : 20px; height: 30px; } & ...

Utilizing CSS Grid to arrange child elements inside their respective parent containers

I am currently working on a grid container that contains multiple divs. Each div houses different elements such as headings, paragraphs, buttons, and logos. I have utilized Flexbox to evenly distribute the logos across the container's width. However, ...

Transforming JSON data into a visual flowchart using VUE.js

In the project I am currently working on, I am faced with the challenge of importing a JSON file (exported from a flowchart drawing project) and converting its data into an actual flowchart. Unfortunately, I have not been able to find any leads on how to c ...

How can I make TypeScript mimic the ability of JavaScript object wrappers to determine whether a primitive value has a particular "property"?

When using XMLValidator, the return value of .validate function can be either true or ValidationError, but this may not be entirely accurate (please refer to my update). The ValidationError object includes an err property. validate( xmlData: string, opti ...

Leverage AngularJS to effectively parse JSON data using scope variables

I am trying to JSON parsing in AngularJS using $stateParams in the controller below: rerunApp.controller('rerunCategoryListCtrl', function($scope, $http, $stateParams) { var stpNameCat = $stateParams.nameCat; $http.get(JSON UR ...