OrbitControls altering the view of the camera

I'm currently immersed in an academic undertaking involving the use of THREE.js and Blender. Within my main.js file, I've crafted the necessary code to set up a scene, establish a renderer, position a camera, implement orbit controls, and more. However, there's a recurring issue where upon each webpage load, the camera is consistently fixated on a specific point designated by camera.lookAt(0,0,90). Yet, whenever I attempt to manipulate the object, the camera's focus shifts elsewhere.

Provided below is an animated gif showcasing the problem at hand: https://i.sstatic.net/luAOG.gif

The following snippet encompasses the code I am working with:

var scene = new THREE.Scene();
scene.background = new THREE.Color( 0xFFFFFF );
// Create a camera
var camera = new THREE.PerspectiveCamera(70, 600/530, 0.1, 500);

var canvas = document.getElementById('myCanvas');
// Set up a WebGL renderer with an 800x600 viewport and append it to the page
var renderer = new THREE.WebGLRenderer({ canvas: canvas, antialias: true });

renderer.setSize(600, 530); // Canvas size (black area)
renderer.shadowMap.enabled = true;
renderer.gammaOutput = true;
renderer.setPixelRatio(window.devicePixelRatio);

camera.position.x = 0;
camera.position.y = -5;
camera.position.z = -15;

// OrbitControls.js
var controls = new THREE.OrbitControls(camera, renderer.domElement);
camera.lookAt(0, 0, 90);

// GLTFLoader (BLENDER TO THREE.JS)
var loader = new THREE.GLTFLoader();
loader.load(
    'blender/backpack.glb', // File name .gltf
    function(gltf){     // Add the file to the scene for rendering
        scene.add(gltf.scene);
    }
);

// Add light sources
var lightPoint1 = new THREE.PointLight("white");
lightPoint1.position.set(5, -5, -5);
scene.add(lightPoint1);

// Additional light sources
var lightPoint2 = new THREE.PointLight("white");
lightPoint2.position.set(-5, -5, 5);
scene.add(lightPoint2);

// Another set of light sources 
var lightPoint3 = new THREE.PointLight("white");
lightPoint3.position.set(1, 1, 1);
scene.add(lightPoint3);

// Further light sources 
var lightPoint4 = new THREE.PointLight("white");
lightPoint4.position.set(5, 5, 5);
scene.add(lightPoint4);

// More light sources 
var lightPoint5 = new THREE.PointLight("white");
lightPoint5.position.set(-5, -5, -5);
scene.add(lightPoint5);

function animate(){ 
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
} 
animate();

Is there a way for me to maintain the initial viewpoint while manipulating the object?

Answer №1

When it comes to the lookAt() method, it can be a bit tricky to navigate. It's recommended to provide it with a vector object such as a Vector3(). If you're experiencing issues with the camera orientation flipping, there may be conflicts between the camera and OrbitControls in determining which direction is considered "up". Although this shouldn't normally be an issue. Additionally, it's worth questioning why you want the camera to focus on the position 0, 0, 90 when the object itself is located at 0, 0, 0.

Personally, I would opt for using the target property included with OrbitControls. Setting something like

controls.target = new THREE.Vector3(0, 0, 0);
should resolve any concerns.

Answer №2

Update: After some trial and error, I discovered that modifying the camera.lookAt() method within the loop function proved to be effective. It may not be the optimal solution, but it gets the job done for now.

function animate(){ 
    camera.lookAt(0, -4, 1)
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
} 
animate();

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

jquery sequential fade effect

I am trying to make each div fade in one by one. Visit this link for reference <div id="w"> <div class="one"></div> <div class="two"></div> <div class="three"></div> </div> $(function() { $(&a ...

Using Jquery to handle input data in a form

Using jQuery, I have set up an AJAX function to fetch data from a database in real-time as the user fills out a form with 5 input fields. Currently, my code looks like this: $("#searchtype, #searchtext, #searchtag, #daterangefrom, #daterangeto").on("load ...

Organize an array of JSON objects based on a specific key and eliminate any duplicated entries

I am grappling with the challenge of grouping an array of JSON objects by the id_commande attribute and eliminating any duplicates. I am finding it quite perplexing to figure out a solution to this issue without relying on a library like lodash. Ideally, I ...

The error message "props.text is undefined in React Native" indicates that there is an issue with accessing the property text within

//**// import { StatusBar } from 'expo-status-bar'; import {StyleSheet, Text, View, Button, TextInput, ScrollView, FlatList} from 'react-native'; import {useState} from "react"; import GoalItem from "./components/GoalItem"; export defau ...

Converting JSON object to a string

I have an object that contains the value "error" that I need to extract. [{"name":"Whats up","error":"Your name required!"}] The inspector displays the object in this format: [Object] 0: Object error: "Your name required!" name ...

Creating a dynamic shift in background color

Is it possible to use jQuery to slowly change the color of diagonal lines in a background styled with CSS, while also adding a fading effect? I have created a fiddle with the necessary CSS to display a static background. You can view it here: http://jsfid ...

When I use AJAX to load a PHP file, the function's content returns as [object HTMLDivElement]

Hello there, When I use AJAX to load a PHP file, the content of my function returns [object HTMLDivElement], but if I load my function without loading the PHP file, it displays normally. index.php <h1>API Football</h1> <nav> ...

Ways to customize a directive to show conditionally with no need for container elements

I have created a basic role-based security directive in angularjs. It's my first attempt at making a directive. My goal is to replace the following HTML: <authorize if-granted="GET_POSTS"> Hello WORLD!!!! {{name}} </authorize> with j ...

Manually assigning a value to a model in Angular for data-binding

Currently utilizing angular.js 1.4 and I have a data-binding input setup as follows: <input ng-model="name"> Is there a way to manually change the value without physically entering text into the input field? Perhaps by accessing the angular object, ...

Passing props in VueJS is a common task, especially while redirecting to another

I am working with two separate single-file components, each equipped with its own named route. In the Setup.vue component, there is a basic form that gathers and sends data to the Timer.vue component which expects certain props. Is there a way to navigate ...

An error is triggered by serializing a TinyBox POST form into an Array

When I implemented the code below, it performed as anticipated: TINY.box.show({url:target, post:$("form[name='currentSearch']").serialize(), width:650, mask:true, close:true, maskid:'boxMask', boxid:'popupBox', openjs:funct ...

Obtain data from an ajax request to be included in the form submission

Seeking assistance with my code to retrieve the selected value from ajax_details.php for submission in the form action process_details.php. Here is the code snippet: injury_details.php <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jqu ...

Tips for finding the position of a specific object within an array of objects using JavaScript when it is an exact match

I am working with an array of objects and need to find the index of a specific object within the array when there is a match. Here is an example of my current array: let x = [ {name: "emily", info: { id: 123, gender: "female", age: 25}}, {name: "maggie", ...

Beware, search for DomNode!

I attempted to create a select menu using material-ui and React const SelectLevelButton = forwardRef((props, ref) => { const [stateLevel, setStateLevel] = useState({ level: "Easy" }); const [stateMenu, setStateMenu] = useState({ isOpen ...

At times, jQuery cubes can occasionally overlap each other

Recently, I encountered an issue with my Pinterest-style website where the cubes were overlapping on page load, even though my jQuery script was designed to evenly space them regardless of browser size. After discussing with the developer who helped me cre ...

Display multiple values in a select dropdown using Bootstrap 5 properties

I am stuck with the following code<hr> HTML <select id="select" placeholder="Choose Select" class="selectpicker" multiple></select> <div class="container"> <div id="all" clas ...

Updating Information Using JQuery

In my view, there is a partial view that displays details of open IT Tickets. Clicking on an open ticket loads the partial view with ticket details and comments using JQuery/Ajax. However, I'm facing an issue where if a new comment is added to a tick ...

Routes are no longer being qualified by Express once a subdomain is incorporated

We had developed a compact app that was working flawlessly, but then we were tasked with transforming it into something accessible for others in our organization to utilize... and that led to everything breaking down. Our initial setup included a simple ex ...

The function FileReader() is not functioning properly within a Vue computed property

I'm attempting to display a set of image thumbnails by dragging images onto the screen. Here is an example of my data structure: data() { return { files: [Image1, Image2, Image3] } } ...where each Image is in a blob format. Below is my co ...

Understanding the differences between paths and parameters of routes in express.js

My express application has the following routes: // Get category by id innerRouter.get('/:id', categoriesController.getById) // Get all categories along with their subcategories innerRouter.get('/withSubcategories', categoriesControll ...