Configuring the camera matrix manually within a ThreeJS environment

I am currently attempting to manually adjust the matrix of a camera in a basic three.js scene. Despite trying various methods such as using the matrix.set function combined with matrixAutoUpdate = false, the scene renders initially but does not update over time as expected. I have also experimented with setting the matrix using camera.matrix = with similar results. It seems like I may be overlooking something when it comes to applying these manual values to the object. Additionally, I've explored applyMatrix but it appears to have a different effect altogether.

Any insights or advice would be greatly appreciated - thank you!

Feel free to check out this code in action on CodePen:

http://codepen.io/heyscam/pen/phflL

Here is the JavaScript portion of the code:

var WIDTH = 640;
var HEIGHT = 360;

var VIEW_ANGLE = 31.417;
var ASPECT = WIDTH / HEIGHT;
var NEAR = 0.1;
var FAR = 10000;

var $container = $('#container');
console.log($container);

var renderer = new THREE.WebGLRenderer();
renderer.setSize(WIDTH, HEIGHT);
$container.append(renderer.domElement);

var scene = new THREE.Scene();

var camera = new THREE.PerspectiveCamera(
  VIEW_ANGLE,
  ASPECT,
  NEAR,
  FAR
);
scene.add(camera);

var cube = new THREE.Mesh(
  new THREE.CubeGeometry(200, 200, 200)
);
scene.add(cube);

var frame = 0;

animate();

function animate() {
    camera.matrixAutoUpdate = false;
    camera.matrix.set(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 500 + (frame * 10), 0, 0, 0, 1);
    render();
    frame++;
    requestAnimationFrame(animate);
}

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

Answer №1

Once you have configured the camera matrix, it is essential to execute the following method:

camera.updateMatrixWorld( true );

This practice is strongly discouraged.

three.js was not intended for this type of manipulation. It is advised to avoid direct manipulation of an object's matrix -- unless you are fully knowledgeable about it and comprehend the internal operations of the library.

Instead, focus on adjusting the camera's quaternion, rotation, position, and scale, allowing the library to handle the matrix update process.

three.js r.60

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

The Comparison of Time Complexity between Dynamic Array Pushing and Assigning to a Fixed Size Array in JavaScript

Consider the following example with dynamic arrays: let numbersArray = []; for(let i = 0; i < 5; i++) numbersArray.push(i+1); and then we have another array: let valuesArray = Array(6); //Keep in mind that this array has a size of 6 compared t ...

Displaying retrieved data following AJAX request in ASP.NET MVC is currently not functioning

I have a situation where I need to populate two <p> fields with text Below is the HTML code snippet: <p id="appId" style="visibility: hidden;"></p> <p id="calculationId" style="visibility: hidden;"></p> I am making an AJAX ...

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 ...

Ember 2: Display a loading message only if the IDs were part of the initial response

I frequently use the following code snippet in my projects: {{#each model.posts as |post|}} <div>post.title</div> {{else}} <div>Loading the posts...</div> {{/each}} However, I sometimes face uncertainty regarding whether t ...

Issues with d3.js transition removal functionality not functioning as expected

Having an issue with a d3.js visualization that involves multiple small visualizations and a timeline. When the timeline changes, data points are supposed to be added or removed accordingly. Here is the code snippet responsible for updating: var channels ...

Looking for a Helper Function to Convert Lengths from WebGL / Three.js to Pixels

I'm trying to understand how WebGL / Three.js determines the heights and widths of objects. What numerical systems are used to set x, y, z coordinates? For example, when the arrow is pointing straight up with Y set to 1, it appears as 15-200 pixels. ...

Ways to showcase an array within another array using VueJS

I encountered an issue with an API call that returns its result as an array within an array. Each nested array contains card transactions that need to be displayed. Upon examining the console output, I found: Array [ Object { "data": Array ...

Can you help me figure out how to retrieve the index of a CSS element during a 'click' event?

I have a collection of images all tagged with the class thumb. When a user clicks on one of these images, I need to determine which image was clicked within the array of thumbs. Essentially, I am looking for the index of the clicked image within the thumbs ...

The Significance of Server-Side JavaScript

How common is the use of server-side JavaScript? What are the advantages of using it over other server-side scripting languages? Can you point out specific use cases where it outperforms other options? I'm also unsure about how to start experimenting ...

Fade out the div element when clicked

For my game project, I needed a way to make a div fade out after an onclick event. However, the issue I encountered was that after fading out, the div would reappear. Ideally, I wanted it to simply disappear without any sort of fade effect. Below is the co ...

Is there a way to restrict access to my website to only be opened in the Chrome browser?

Is there a way to prevent my web application from loading when the link is opened in browsers other than Chrome? Can this be achieved using Javascript or Java? I want to restrict the usage of my web application to only Chrome. Any assistance would be appre ...

Retrieve the latest inserted ID in a Node.js application and use it as a parameter in a subsequent query

I am currently working with an SQL database that consists of two tables, namely club and players. These tables are connected through a one-to-many relationship. Although the query in my node.js code is functioning properly, I am facing an issue retrieving ...

Executing a POST request with AJAX in jQuery to communicate across different domains and fetching XML data as

Is it possible to send an AJAX request using the POST method and receive XML as a response text? If so, please provide me with the steps to achieve this. For example: url : "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate" data ...

Unable to locate the aws-sdk package after running `npm install -g aws-sdk`

I recently set up aws-sdk in a Ubuntu 20.04 container using the following command: sudo npm install -g aws-sdk However, when I try to run a script that relies on this package, I encounter the following error message: Error: Cannot find module 'aws- ...

Create a timestamp with Javascript rendering

Looking to convert a Unix timestamp into a human-readable format without adjusting for my browser's timezone. For example, if the timestamp is 1400167800 (05 / 15 / 14 @ 3:30:00pm UTC) and my timezone is +2, how can I display this timestamp as ' ...

Is there a way to display one of the divs in sequence each time the page is reloaded?

Is there a way to display the divs sequentially when the page is refreshed? For instance, on the initial load, only div 1 should appear, and with each refresh it should cycle through divs 2, 3, 4, and 5 before starting over. Below is an example I'm c ...

The cookie's expiration time is being set to 1 hour, rather than the specified maxAge duration

My file contains a cookie that consistently sets to 1 hour as the default, even when I specify a maxAge of 12 seconds. res.cookie("my-cookies", req.body.id_token, {maxAge: 12000, httpOnly: false}); ...

Getting the first array from a fetch promise is a process that involves accessing the

When working with a fetch promise, I need to return a JSON object that contains all of my data. The challenge is that I am not sure what the object name will be. What I do know is that there will always be one object present. Below is an example of my cod ...

Switch back emulation when moving away from page - using angular javascript

I have a unique scenario in my component.ts file on a specific page where I need to incorporate custom CSS for printing purposes: @Component({ selector: "dashboard", templateUrl: "./dashboard.component.html", styleUrls: ["./d ...

What is the standard practice in AJAX for determining a specific state during page loading?

Since I started diving into the world of serious ajax operations, one question has been on my mind. Let me illustrate with an example. Imagine fetching a standard HTML page for a customer from the server. The URL could look like this: /myapp/customer/54 ...