Dual Camera Toggle Functionality with Three.js Orbit Controls

I am facing an issue with two cameras in one scene, one viewport, and one renderer. I switch between cameras using HTML buttons.

ISSUES

Issue 1

When using camera1, there is no response from moving the mouse. However, when I switch to camera2, the orbit control works fine. Upon toggling back to camera1, the mouse movement still does not work.

To view the original problem where there was no response from camera1, refer to: jsfiddle.net/steveow/xu0k1z75/

UPDATE: Issue 1 has been fixed by Stallion - avoid setting the camera position to (0,0,0).

Issue 2

With camera1, there is a lingering problem where Pan and Dolly movements are very slow initially. They might speed up later but then become excessively fast.

To see the issue with camera1 Pan & Dolly being slow at first, visit: http://jsfiddle.net/steveow/uk94hxrp/

UPDATE: The slowness in Pan & Dolly movements is due to the camera being too close to the OrbitControls target position. By choosing a different .target position, this slowness can be avoided.

NOTES

I have tried creating a new THREE.OrbitControls object each time I switch cameras. Alternatively, I attempted to create two persistent THREE.OrbitControls objects during initialization and assign a general variable "controls" to the active one. Different methods like including controls.update() in the animation loop have also been tested.

Although it worked with either camera before, I seem unable to replicate that success now.

I have examined the OrbitControls code but remain puzzled by it.

CODE (same as original Problem, Issue 1 with slight modifications)

Below is the camera initiation code:

//... camera1
    // Camera1 specifications

// camera2
    // Camera2 specifications    

Animation, camera switching, and resetting code:

//----------------------------------------------------------------
function F_frame()
{
   //... Render
   
}//... EOF Frame().

//-------------------------------------------------------------
function F_Switch_Camera() 
{
    // Switching between cameras
    
}       
//----------------------------------------------------------------------
function F_Reset_Camera1()
{
    // Resetting Camera1 position and orientation
}
//----------------------------------------------------------------------
function F_Reset_Camera2()
{
    // Resetting Camera2 position and orientation
}

UPDATE

Credits to user Stallion for the straightforward resolution - avoid setting the camera world position to (0,0,0) and use (0,0,1) instead.

Answer №1

Here is a simple solution:

Adjust the position of camera1 by using:

camera1.position.set(0, 0, 1);

For more details, refer to this JSFiddle link

// Some JavaScript code...
#container {
  height: 100%;
  width: 100%;
  min-height: 100%;
  min-width: 100%;
}
<script src="https://cdn.jsdelivr.net/npm/three@latest/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@latest/examples/js/controls/OrbitControls.min.js"></script>

<h1 id="title_A">Camera Frustrum Box</h1>
<input type="submit" id="byBtn1" value="Switch Camera" onclick="F_Switch_Camera()" />
<input type="text" id="myTextField" value="camera1" />
<input type="submit" id="byBtn2" value="Reset Camera1" onclick="F_Reset_Camera1()" />
<input type="submit" id="byBtn3" value="Reset Camera2" onclick="F_Reset_Camera2()" />
<div id="container"></div>

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

When utilizing a React styled component, it functions smoothly during development but triggers a build error when in production

Recently, I encountered a strange issue with my code. I have a styled component div that wraps around another component in this manner: <ContentWidget> <BookDay /> </ContentWidget> (The Bookday component returns an empty div so there ...

What's causing the failure in the execution of the "Verify version" step?

Upon reviewing the 3DSecure GlobalPay documentation, my team decided to integrate it using JSON, incorporating our own client-side implementation. This decision was made because we already have another integration with a different 3DS verification service ...

Format a JSON file using clang-format

I'm facing an issue with my json file formatting. When I run clang-format on it, the output is ugly as it treats it like code. { "name" : "My great app", "description" : "It's really cool.", "version" : "0 ...

Aligning an element perfectly at the top within a column

I have two columns side by side. The column on the left contains HTML that I cannot control. On the right side, I need to display a comment box that should align with the text clicked on the left hand side. Is it possible to position an element absolutely ...

Classify JavaScript Array Elements based on their Value

Organize array items in JavaScript based on their values If you have a JSON object similar to the following: [ { prNumber: 20000401, text: 'foo' }, { prNumber: 20000402, text: 'bar' }, { prNumber: 2000040 ...

Steps for Building and Exporting a Next.js Project Without Minification and Optimization

Is there a way to build and export a Next.js project without minifying and optimizing the output files? ...

Is the ajax success function failing to work properly in JavaScript?

Although I've searched extensively on Stack Overflow, none of the similar questions seem to solve my issue. The problem I'm facing is that the success function is not triggering the alert message and I can't seem to figure out why. <form ...

Achieving the Menu Hover Effect - step by step guide to recreating this stylish effect

I recently came across a stunning hover effect on the main menu of a website (red rectangles above menu). It's quite impressive! I found it here: Now, I want to incorporate this same effect into my own website. Unfortunately, there doesn't seem ...

MVC - The challenge of users repeatedly clicking the Submit button

In my MVC application, there are multiple pages where users submit a form by clicking a Submit button. Occasionally, users may click the button multiple times if they don't see an immediate response, causing the form to be submitted twice. To address ...

Is There a Sparse Array Element in JavaScript?

Is there a more efficient method to check for the presence of an array element within multiple layers of a structure? For example: if (typeof arr[a][b][c] === 'undefined') { ...do something... } If [a] or [b] are missing, we cannot accurately t ...

Execute PHP script through jQuery request within the context of a Wordpress environment

I want to replicate a specific functionality in WordPress. In this functionality, jQuery calls a PHP file that queries a MySQL table and returns the result encapsulated within an HTML tag. How can I achieve this? <html> <head> <script ...

Unable to successfully transfer parameters from AJAX to PHP

I successfully utilized Jquery UI to update the position of my table. Now, I am trying to pass a parameter from AJAX to PHP in order to update my database with the current table position. However, I encountered an issue where I receive a TypeError: data=nu ...

Ensure that each child component receives a prop

Can you transfer a prop with a function to any child component? Specifically, I want to send a function from a parent component to each {children} element: <> <Navbar /> <main>{children}</main> <Footer /> </&g ...

Can an image map area be identified using radial coordinates?

While I have experience with online Imagemap Generators for rectangular or circular areas in HTML pages, things get a bit tricky when dealing with pie-shaped sections. When trying to make each pie slice a separate area, the image map code generated by thes ...

Selenium is throwing an ElementNotInteractableError indicating that the element is not able to be

Today I decided to dive into learning Selenium, but I've hit a roadblock while trying to click on an element that looks like this: <a rel="nofollow" style="" href="javascript:void(0);" time="" itemid="15 ...

What is the process of invoking a secondary "external" function with Nodejs, Expressjs, and bluebird?

Struggling with creating a nodejs application, a new area for me. I've managed to work with Promises and fetch data from a database. Take a look at the code below: myModel.js var express = require('express'); var app = express(); var Promi ...

Getting a specific element from an Ajax response may involve using JavaScript methods to target the desired

Within my PHP file, there are multiple HTML elements that I am fetching in an AJAX response. Currently, all these HTML elements are being returned in my drop area. How can I adjust the code to only retrieve a specific element, such as an image, like so: P ...

Is there a way to stop a music track from playing?

function playMusic(){ var music = new Audio('musicfile.mp3'); if (music.paused) { music.play(); } else { music.pause(); } } <input type="button" value="sound" onclick="playMusic()" ...

Interpret a JavaScript array response

Currently, I am working on an API request where I receive an HTTP response that contains an array with a single JSON object. However, when attempting to parse or stringify it using the following code: var response = http.response; try{ var json = J ...

Looking for a sophisticated approach in Spring MVC to manage the escaping and unescaping of strings being passed to the Front End

Currently, I am in the process of developing a web application utilizing Spring MVC. This project involves retrieving multiple objects from the Database, each containing strings as attributes. It's worth noting that I have no control over the format o ...