Creating a two-dimensional perspective in Three.js

I am new to utilizing three.js and I am currently attempting to create a 2D visualization using these 3D tools for layered sprites. I am looking for guidance on the arguments for PerspectiveCamera() and camera.position.set(). I have received some helpful hints from this answer to a related question, suggesting to set the z coordinate as 0 in camera.position.set(x,y,z).

Below is a code snippet that I am modifying from one of stemkoski's three.js examples. I am struggling with determining the values for VIEW_ANGLE, x, and y. If I intend to achieve a flat camera view on a plane the size of the screen, how should I assign these variables? I've experimented with various values but it is difficult to decipher the visual outcome. Thank you in advance!

var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;   
var VIEW_ANGLE = ?, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
scene.add(camera);
var x = ?, y = ?, z = 0;
camera.position.set(x,y,z);
camera.lookAt(scene.position);

UPDATE - perspective vs orthographic camera:

Thank you @GuyGood, I understand that I need to decide between a perspective camera and an orthographic camera. I now realize that even in this 2D context, the PerspectiveCamera() would offer effects like parallax, while OrthographicCamera() would provide consistent rendering sizes regardless of layer depth in the 2D element. I am leaning towards using the PerspectiveCamera() for subtle parallax effects between sprite layers (indicating my project may not be entirely 2D!).

Therefore, ensuring that all sprite layers are parallel to the viewing plane is crucial, and that camera.position.set() signifies the orthogonal viewpoint line to the center of the field of view. This concept might be fundamental to many individuals here, but it feels like a whole new realm to me!

I am still grappling with understanding the significance of VIEW_ANGLE, x, and y, as well as the distance between the camera and the near and far viewing planes in a 2D visualization. With the orthographic camera, this factor seems insignificant - there just needs to be enough depth to include all desired layers and a suitable viewing plane scale for the sprites. However, with the perspective camera, the depth and viewing field affect the parallax effect; are there any additional considerations to contemplate?

UPDATE 2 - Angle of view and other variables:

After further exploration into comprehending the Angle of View (Field of View, or FOV) for the camera and the x,y,z arguments for camera position, I found a useful video overview about Field of View in game design (relevant to answering questions for my 2D visualization project). Alongside this Field of View tutorial for photographers, which was informative (albeit slightly cheesy), these resources provided insights into selecting a Field of View for my project and the implications of wide or narrow Fields of View (measured in degrees out of 360). Optimal outcomes involve a mix of natural human field of vision based on screen proximity and relative scale differences between foreground and background elements in the visualization. Wider fields diminish the background, while narrower fields accentuate it - a similar though less pronounced effect compared to an orthographic camera. I hope this information proves as beneficial to you as it has been for me!

UPDATE 3 - Further reading

If you are interested in delving deeper into camera specifications across various applications, check out chapter 13 of Computer Graphics Principles and Practice. It has been invaluable in addressing my previous inquiries and more.

UPDATE 4 - Considerations for the z dimension in the Orthographic camera

During my ongoing project, I opted for the orthographic camera to increment the z dimensions of my sprites to prevent z-fighting without them appearing progressively distant. Contrastingly, adjusting size creates the illusion of a sprite receding into the distance. Nevertheless, I encountered a minor error today that I wish to highlight to save others from similar issues. Although the orthographic camera does not depict diminishing sizes over distance, remember there is still a past where objects will be removed from view due to a back frustrum plane. Today, I mistakenly exceeded the z values for several objects and couldn't identify why they weren't visible on screen. The z-coordinate is easy to overlook when working with the orthographic camera.

Answer №1

What is your objective? If you don't require perspective distortion, opt for the orthographic camera instead. Be sure to refer to the documentation as well:

The concept of View Angle/Field of View is quite straightforward, but if you're unfamiliar with it, consider doing some further research.

When it comes to the x, y, and z values, it ultimately hinges on the dimensions of your plane and its distance from the camera. You have the option to adjust either the camera's position or the plane's position while maintaining the camera at (0,0,0). Visualize a plane within 3D space. You can determine the camera's position based on your plane's size or simply experiment until you find the ideal arrangement. For guidance on utilizing an orthographic camera, take a look at this post:

Three.js - Orthographic camera

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

Ensure that the sidebar automatically scrolls to the bottom once the main content has reached the bottom

I am facing an issue with a sticky sidebar that has a fixed height of calc(100vh-90px) and the main content. The sidebar contains dynamic content, which may exceed its defined height, resulting in a scrollbar. On the other hand, the main content is lengthy ...

What is the purpose of assigning controller variables to "this" in AngularJS?

Currently, I am analyzing an example in CodeSchool's "Staying Sharp with Angular" course in section 1.5. Here is the code snippet: angular.module('NoteWrangler') .controller('NotesIndexController', function($http) { var contro ...

conditionally trigger one observable in rxjs before the other

I am in need of assistance or guidance regarding a challenge I am facing with rxjs that I cannot seem to resolve. In essence, my goal is to trigger an observable and complete it before the original one is triggered. Scenario: I am currently working on a ...

Updating the object does not result in the interpolation value being updated as well

I am facing an issue with this v-for loop: <tr v-for="(product, index) in products" v-bind:key="products.id"> <div v-on:click="decrementQtd(index)" class="action-qtd-button-left"> <strong>-</strong> </div> < ...

What is the function of the OmitThisParameter in TypeScript when referencing ES5 definitions?

I came across this specific type in the ES5 definitions for TypeScript and was intrigued by its purpose as the description provided seemed quite vague. /** * Removes the 'this' parameter from a function type. */ type OmitThisParameter<T> ...

NodeJs - Issue: Headers cannot be changed once they are sent & TypeError: req.next is undefined

My goal is to retrieve data from a MySQL database by calling methods to insert and read information. The reason I am doing this is because node.js operates asynchronously. Here is my code: exports.list = function(req, res){ var moduleRows; req.getCo ...

Unknown individual, yet revealed by the investigator

I'm attempting to develop a dynamic list in react.js generateList = () =>{ return this.state.user.map((u)=>{ console.log(u); return <li onClick={this.handleClick} id={u} name={u}>{u}</li>; }); } The hand ...

Highcharts integration with YQL finance data in JSON format

Currently, I am utilizing jQuery and highcharts.js to develop a single line chart on my website that displays historical financial data for any company specified by the user. I have been experimenting with YQL and employed this query to fetch some quotes i ...

What could be causing the element.style.FontSize to not be effective on classes that have been looped through and stored in an array created with querySelectorAll beforehand?

Greetings Stackoverflow Community, Today, I'm facing an issue related to JavaScript and WordPress. I have a JavaScript script named setDynamicFontHeight.js, as well as PHP documents named header.php and navbar_mobile.php, which simply executes wp_nav_ ...

When calling an API endpoint, nodeJS is unable to access the local path

I've encountered a strange issue with my code. When I run it as a standalone file, everything works perfectly fine. However, when I try to utilize it in my API endpoint and make a request using Postman, it doesn't seem to function properly. What ...

Custom transaction settings for Mongoose

When conducting transactions, we often have the ability to customize certain options. For example, we can specify transaction options like the ones shown below: const transactionOptions = { readPreference: 'primary', readConcern: { level: &ap ...

Tips for modifying the border of an entire column in react-table

My goal is to adjust the style according to the screenshot below. This is what I expect: However, this is what I currently have: As you can see, the border bottom of the last column is not applied as expected. I have tried using the props provided by r ...

Ways to access PromiseValue in XMLHttpRequest with JSON

Is there a way to access an array that is within the PromiseValue instead of receiving Promise {<pending>} When I use .then(data => console.log(data)), I can see the array in the console log. However, my requirement is to display this array on an ...

What causes the selected option to be hidden in React?

I created a basic form demo using react material, featuring only one select field. I followed this link to set up the select options: https://material-ui.com/demos/selects/ With the help of the API, I managed to display the label at the top (by using shri ...

Tips for creating a two-tier selection filter system

I'm having an issue with this code. My objective is to create two filters for this table. The select element with id="myInput" should determine which rows appear in the table and apply the first filter. Here is the JavaScript code: function myFunctio ...

Troubleshooting: jQuery addClass() function not functioning properly in conjunction with attr("href", something)

I am trying to implement a unique feature for a link using a Bootstrap button, where it only works on the second click. On the first click, the appearance of the link should change slightly. To achieve this, I am utilizing the .addClass(newClass), .removeC ...

I'm finding it difficult to grasp the purpose of $inject within controllers

I'm feeling completely lost when it comes to understanding inject in Angular. I can't seem to grasp where it should be utilized and its purpose. Is it specifically tied to factory methods, as outlined here? myController.$inject = ['$scope&a ...

Tips for Showing Websocket Response On Web Browser Using Node.js

Just starting out with NodeJS and here's my code snippet: const webSocket= require('ws'); const express = require('express'); const app=express(); Var url = "wss://stream.binance.com:9443/BTCUSDT@trade`" const ws = new webS ...

Retrieving data from a dynamic form using jQuery

Can someone assist me with the following issue: I have a dynamic module (generated by a PHP application) that includes input fields like this: <input type="text" class="attr" name="Input_0"/> <input type="text" class="attr" name="Input_1"/> ...

Refreshing Angular 9 component elements when data is updated

Currently, I am working with Angular 9 and facing an issue where the data of a menu item does not dynamically change when a user logs in. The problem arises because the menu loads along with the home page initially, causing the changes in data to not be re ...