Creating an overlay-section in three.js with orbit-controls that dynamically responds to the main view's movement

I'm looking to incorporate a section in the corner of my main view that displays a miniature version of the main view.

Currently, I am developing a webpage using JavaScript with three.js. The main view window showcases various geometries that can be rotated and moved using OrbitControls.

My goal is to have a separate section in the corner of the main view where a small cube can be displayed, rotating in sync with the main view without zooming in or out when the main view is zoomed.

var orientationGeometry = new THREE.Mesh( geometry, material );
camera.add( orientationGeometry );

// in animate function:
orientationCube.rotation.x = controls.getPolarAngle();
orientationCube.rotation.y = controls.getAzimuthalAngle();

While this code successfully rotates the small cube and keeps it fixed on the screen by adding it as a child to the camera, zooming in or out causes the cube to move away from the camera.

Is there a method to create an additional section as depicted in the following image?

https://i.sstatic.net/56Zp0.png

Answer №1

If you want to display a second viewport on the screen, you can achieve this by enabling WebGLRenderer.ScissorTest, setting the desired scissor, adjusting the viewport, and rendering the scene again. Make sure to remember to call clearDepth() or else nothing will be rendered.

To have a different camera behavior, simply add a second camera to the scene and update it based on your requirements. To have it rotate and move in sync with the full-screen camera, update the necessary parameters accordingly.

If you wish to maintain a fixed zoom state, calculate the normalized position of the camera and multiply it by the predetermined distance.

function animate() {

    // rendering the full scene.
    // ...

    // setting up the scissor viewport.
    renderer.clearDepth(); // essential!
    renderer.setScissorTest( true );
    renderer.setScissor( 20, 20, insetWidth, insetHeight );
    renderer.setViewport( 20, 20, insetWidth, insetHeight );

    // updating the second camera.
    camera2.position.copy( camera.position );
    camera2.position.normalize().multiplyScalar( distance );
    camera2.quaternion.copy( camera.quaternion );

    // rendering the small scene.
    renderer.render( scene, camera2 );

    renderer.setScissorTest( false );

}

Here is a functional example https://jsfiddle.net/qwb39spx/

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

What is the method for determining the actual line width from 2D geometry?

Is there a way to transfer the line width from a 2D sheet in order to create a custom line with the correct width or a proportionate original line width? When using the VertexBufferReader, only the two points are returned without the line width informatio ...

Having trouble getting a constructor to function properly when passing a parameter in

Here's the code snippet I'm working with: import {Component, OnInit} from '@angular/core'; import {FirebaseListObservable, FirebaseObjectObservable, AngularFireDatabase} from 'angularfire2/database-deprecated'; import {Item} ...

JavaScript library error: "Uncaught ReferenceError: numjs is undefined"

Hi there! I've recently started learning JavaScript and am currently exploring the functionalities of NumJS. However, I've encountered an error that's got me stumped. Even though the file "numjs.js" is included locally within the HTML, I kee ...

Use the map function to find the highest number within each array

function each(collection, func) { if (Array.isArray(collection)) { for (var i = 0; i < collection.length; i++) { func(collection[i], i); } } else { for (var key in collection) { func(collection[key], key); } } } functi ...

Swagger is unable to locate a user schema when utilizing Yaml files

Hello, I am a beginner using Swagger and trying to create simple endpoint documentation. However, I am facing an issue with my schema type and cannot figure out what's wrong. To start off, I organized my folder structure in the root src directory whe ...

JavaScript / Ajax / PHP - A Minor Bug That's Bugging Me

I'm in need of some help, as I feel like I'm losing my mind right now. I've developed a code using php/js/ajax to verify if an email address exists when it's submitted. After testing the ajax, I can confirm that the correct values are ...

Tips for validating date input in a TextBox using JQuery on an ASP.NET platform:

Here is some code I wrote: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="datetime.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <tit ...

What is the best way to access all of the "a" elements contained within this specific div?

Chrome websites are built using the following structure: <div class="divClass"> <a class="aClass"></a> <a class="aClass"></a> <a class="aClass"></a> </div> Utili ...

JavaScript Loading Screen - Issues with window.onload functionality

I am currently working on creating a loading screen for my project. I need to use JavaScript to remove the CSS property "Display: none" from the page, but for some reason, my code is not functioning as expected. The Issue: I discovered that using window. ...

How to clear a particular select option in react-bootstrap

I am looking for a solution to clear the second select when the first one selects option B. The clearing should involve removing the value, disabling it, and setting the default option to Select.... import React, { useState } from "react"; import ...

Angular confirmation page following successful HTTP POST request to Web API

First question here... I have been given the task of improving an Angular application, even though I am starting with zero experience in Angular. While I do have some background in JavaScript, I mostly work with Java (JSP's and yes, JavaScript). Despi ...

Sharing data between ReactJS components: Passing variables between components

I am currently working on a ReactJS project and I have the need to transmit a variable from one component to another. class Layout extends React.Component{ constructor(props) { super(props); this.state = {keyword: &apos ...

Is it advisable to clear the bootstrap tooltips array in order to conserve browser memory once I have finished rendering new tooltips?

My apologies if this question has been asked before, but I searched online first and couldn't find a helpful answer. Beautiful Views Recently, I was working on a project where I needed to display search results, each accompanied by a button that trig ...

Unable to access Session state in Web Service through ajax on the client side

I'm facing an issue where the system is generating a new session for each user when I run an ajax query, instead of having just 1 session per user. Strangely, there were no issues when I tested it in debug mode on my computer, but problems arose on th ...

Tips for automatically filling out a div class form:

I currently have an Autoresponder email form featured on my webpage. Here is a snippet of the code for the section where customers enter their email: <div id = "af-form-45" class = "af-form" > <div id = "af-body-45" class = "af-body af-standa ...

Toggle the availability of links based on the presence of route parameters

My routing configuration appears as follows (for readability, I've omitted the second parameter in when()): app.config(function($routeProvider, $locationProvider){ // Prepare for html5 mode $locationProvider.hashPrefix('!'); $l ...

Encountered an issue while installing the "sharp" module on MAC M1

When I run npm run dev (gatsby develop) on my MacBook Pro M1 chip, it exits with the error message: Error: Something went wrong installing the "sharp" module However, when I run npm run dev on a MacBook Pro with an Intel chip, everything works fine. I&ap ...

Utilizing useEffect Hooks to Filter Local JSON Data in React Applications

For my engineering page, I have been developing a user display that allows data to be filtered by field and expertise. Fields can be filtered through a dropdown menu selection, while expertise can be filtered using an input field. My initial plan was to ...

Utilizing additional PHP files to handle the AJAX post request and subsequently execute the PHP script

I have been tasked with a complex assignment that I am struggling to comprehend. Let me provide a brief overview of what is required of me. There are three files involved in this task: 1. An HTML file that sends an AJAX post request to passwrapper.php. 2. ...

Enhance the lighting effects in Three.js with the implementation of shadows for area lights through Deferred

I am currently working with WebGLDeferredRenderer on a project that consists of a simple scene, featuring a cube acting as a room and two smaller cubes for testing purposes. However, I am encountering difficulties in displaying shadows generated by the are ...