Using Three.JS to render textures directly onto a canvas without the need for creating a plane, in a billboard style

I am currently implementing a custom texture using a WebGLRenderTarget. My goal now is to directly draw the resulting renderTarget.texture onto the canvas, without having to add it to a plane first.

The motivation behind this approach is that my camera is constantly orbiting and moving. I want the texture to stay fixed in the top-right corner like a billboard, without lagging behind.https://i.sstatic.net/Qvzn3.jpg

This is what my pseudo-code looks like:

// Create renderer 1
var renderer1 = new THREE.WebGLRenderer();
renderer1.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer1.domElement);

// Create renderer 2
var renderer2 = new THREE.WebGLRenderer();
renderer2.setSize(100, 100);
document.body.appendChild(renderer2.domElement);

var renderTarget = new THREE.WebGLRenderTarget(100, 100);

// Create texture for renderTarget
renderer1.render(textureScene, ortoCam, renderTarget, false);

// Render textureScene on the second renderer to visualize texture
renderer2.render(textureScene, ortoCam);

// Render the rest of the scene with a dynamically moving camera perspective
renderer1.render(scene, perspCam);

This solution functions as intended but seems somewhat inefficient since it requires creating an additional renderer to accommodate a 100x100 canvas. Is there a way to access the renderTarget.texture and overlay it on the first canvas so that the use of a secondary renderer can be avoided? I need the renderTarget output to generate height maps while also displaying it in a billboard style at the top-right corner.

Update: I attempted to create a 2D canvas context and utilize

context.drawImage(this.renderTarget.texture.image, 10, 10, 20, 20);
only to find that renderTarget.texture.image returns undefined. Any insights into why I'm unable to retrieve image data from it?

Answer №1

It is recommended to use just one renderer.

To create an inset overlay, you can follow this pattern:

const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.autoClear = false; // Make sure to set autoClear to false
document.body.appendChild(renderer.domElement);

Within the render() function,

renderer.clear();

// Render to a texture
renderer.render(texture_scene, ortho_camera, renderTarget, true);

// First, render the main scene
renderer.setViewport(0, 0, window.innerWidth, window.innerHeight);
renderer.render(scene, camera);

// Then, render the inset
renderer.clearDepth(); // Clear the depth buffer
renderer.setViewport(10, window.innerHeight - insetHeight - 10, insetWidth, insetHeight);
renderer.render(inset_scene, ortho_camera);

Using three.js r.84 version

Answer №2

Recently, I developed an npm package designed to display a screen quad by utilizing shaders and a user-friendly high-level interface (including parameters like width, height, top, left).

Currently, the fragment shader does not incorporate texture reading. However, there is a basic fragment shader provided in the readme for that purpose.

Find out more about the npm module here

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

Ways to deactivate the submit button using Cookies, sessions, or local storage

Although cookies and storage can be deleted, they can still help prevent many human spammers who are unaware of this. How can I temporarily disable the form submit button on a specific page for just one day? I want to create a code that saves "Submitted" ...

Alter text within a string situated between two distinct characters

I have the following sentence with embedded links that I want to format: text = "Lorem ipsum dolor sit amet, [Link 1|www.example1.com] sadipscing elitr, sed diam nonumy [Link 2|www.example2.com] tempor invidunt ut labore et [Link 3|www.example3.com] m ...

Generate text in a random spot effortlessly

After doing some research on various development platforms, I stumbled upon this JSFiddle that seems to have a solution for my requirements. The only thing missing is the ability to input a specific word (without user input) and automate the process at fix ...

Implement the CSS styles from the Parent Component into the Child Component using Angular 6

Is there a way to apply CSS from the Parent Component to style the child component in Angular 6? Please advise on how to approach this issue. How can we inherit the css styles from the Parent Component? <parent> <child> <p>hello ...

AngularJS making use of multiple checkboxes to filter data from a nested JSON structure

Hi everyone, I came across a helpful resource on implementing multiple checkboxes filters in AngularJS: angular js multiple checkbox with custom filters I am facing a similar issue but with a multilevel JSON structure. Here is an example of what I am deal ...

In React, the error message "Joke.map is not a function" indicates that

export default App I am encountering an error in this code which says joke.map is not a function. Can someone please assist me in finding a solution? I have verified the api endpoints and also checked the function. import { useEffect, useState } from &ap ...

How to dynamically set a value in an AngularJS text field

Clicking "edit" will show the form in HTML below: <div ng-repeat="item in items"> <input type="text" placeholder="{{item.questionPlaceholder}}" ng-model="form.g1tags[item.tags_index]" required> <input ng-class="{'blockInput&apo ...

TypeScript Generic Functions and Type Literals

Everything seems to be running smoothly: type fun = (uid: string) => string const abc: fun = value => value const efg = (callback:fun, value:string) =>callback(value) console.log(efg(abc, "123")) However, when we try to make it generic, we e ...

Divide a collection of q promises into batches and execute them sequentially

In order to achieve my objective of copying files while limiting the number of files copied in parallel based on a defined variable, I decided to divide an array of promises using calls to fs.copy into packets. These packets are then executed in series by ...

Javascript loop woes

I have a number array ranging from 1 to 20. My goal is to create a loop that extracts every nth element from the array, starting with (1, 6, 11, 16). After the initial loop, it should then extract every 5th element, starting from 2 (2, 7, 12, 17). My atte ...

Learn how to incorporate conditions into your mapping function in React to retrieve a single result

Hello everyone, I have a task where I need to compare two collections. If the comparison returns true, I want to display solved, otherwise I want to display Available. First collection : (5) [{…}, {…}, {…}, {…}, {…}] 0: creatorUserI ...

Activating the delete event trigger for a tinyMCE object

How can I create a function that activates when a user deletes an object in tinyMCE? I want to remove uploaded images from a cache folder whenever a user deletes an image from the tinyMCE editor. If they upload an image and then delete it, I would like an ...

Asynchronous callback in mongoose with an undefined value

I am utilizing 'mongoose' and 'async'. While my search functionality is operational, I encounter an issue where my data does not get passed in the final callback. The variable always ends up being undefined. Despite referencing the mong ...

The jQuery selector threw an Uncaught SyntaxError due to an Unexpected identifier

Just starting out with programming, I'm attempting to send data using my Python script. I'm unsure of what the error message is indicating. $(document).ready(function() { $("tags").keyup(function({ var search = $("tags").val() $.post(" ...

Moving a row from one Jquery Datatable to another table

I'd like to merge the rows from table 2 into table 1. If I check a row in the checkbox column and click on "add user," that row will be added to table 1. Next to the "add user" button, there is an option to select all or none. If I click on "select ...

Comparing Redux and MVC models in software architecture

A new project is on the horizon, and the Product Owner has suggested using Redux for state management. However, I am hesitant to embrace this suggestion as I fail to see the advantages compared to a model-based approach. For instance, instead of utilizin ...

Struggling to Align NAV buttons to the Right in React Framework

My current Mobile Header view can be seen here: https://i.stack.imgur.com/CcspN.png I am looking to achieve a layout similar to this, https://i.stack.imgur.com/pH15p.png. So far, I have only been able to accomplish this by setting specific left margins, ...

Tips for showcasing a drop-down menu using Jquery

I am currently utilizing jQuery to showcase a drop-down menu. It is successfully working for a single menu and displaying the appropriate drop-down. However, when I attempt to use more than one menu, it displays all of the drop-down menus simultaneously. I ...

jQuery-powered one-page site - Information in HTML or JavaScript

I am currently learning JavaScript and jQuery, with a strong grasp on HTML/CSS. I am working on developing a single-page front-end website using Bootstrap for layout design. My main focus now is on implementing functionality. Here is the scenario: There a ...

Showing events from MySQL database on Vue.js Fullcalendar

I am trying to fetch events from my MySQL database and pass them to my Vue component to be displayed on the FullCalendar. However, the event array is being populated with a full HTML document. Below is my EventController: public function getEvents() { ...