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. Is there a way to convert pixel values into the correct float numbers for THREE.js using a helper function?

Apologies if I am not using the correct terminology regarding numerical systems, this is my current frame of reference.

The only thing missing below is creating the scene, but everything else is there, as shown in the image.

Is there a helper function that can take pixel values and return the appropriate float number for use with THREE.js?

This is the code for my arrow:

//scene.remove(cube); scene.remove(group);

// create a new object
var sphere = createMesh(new THREE.SphereGeometry(5, 10, 10));
var cube = createMesh(new THREE.BoxGeometry(6, 6, 6));

sphere.position.set(controls.spherePosX, controls.spherePosY, controls.spherePosZ);
cube.position.set(controls.cubePosX, controls.cubePosY, controls.cubePosZ);

// add objects to the scene.

// create a group for rotation
var group = new THREE.Group();
group.add(sphere);
group.add(cube);

scene.add(group);
controls.positionBoundingBox();

var arrow = new THREE.ArrowHelper(new THREE.Vector3(0, 1, 0), 0, 10, 0x0000ff); //arrow pointing up
scene.add(arrow);

https://i.sstatic.net/Fc1eV.jpg

I have JavaScript objects with dimensions in pixels, but how do I convert these to usable units in 3D space? https://i.sstatic.net/DyL29.jpg

Answer №1

The relationship between lengths in three-dimensional space and two-dimensional space is not straightforward, especially when perspective projection is taken into account.

For example, consider two arrows with the same length and orientation in 3D. When projected onto a 2D plane, these arrows will appear to have different lengths depending on their distance from the camera. The arrow closer to the camera will appear longer than the one that is farther away.

To maintain a consistent pixel length for a specific arrow, it would be necessary to constantly adjust the arrow's 3D length based on various factors such as camera position, orientation, field of view (FOV), as well as changes in the arrow's own position or orientation. While this level of precision is achievable (as mentioned by @WacławJasper), it can be quite complex.

If you provide more context about your overall objective, there may be a simpler solution to address your needs.

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

Spin mesh as the page is scrolled in three.js

Seeking assistance to rotate an object based on page scrolling. Having trouble getting it to function correctly. Currently, this code updates the object's rotation on page load: mesh.rotation.y = window.pageYOffset; However, this only affects the ob ...

I am attempting to generate a displacement map effect to smoothly transition between three images using the ThreeJs library, but I have hit a roadblock

My journey with ThreeJS started a few months back, leading me into the fascinating world of webgl. I delved into numerous courses on YouTube, but most of them were centered around 3D compositions. While I have developed a basic understanding of how things ...

Looking to switch up the hide/show toggle animation?

Currently, I have a functioning element with the following code. It involves an object named #obj1 that remains hidden upon page load but becomes visible when #obj2 is clicked. #obj1{ position:fixed; width:100px; bottom:180px; right:10 ...

Guide on utilizing TypeScript interfaces or types in JavaScript functions with vscode and jsdocs

Is there a way to utilize types or interfaces to provide intellisense for entire functions or object literals, rather than just function parameters or inline @type's? For example: type TFunc = ( x: number ) => boolean; /** * @implements {TFunc} ...

What is the best way to transfer information from df ~ to my webpage?

I'm currently working on a pie chart that visualizes the disk space usage on my Linux machine. I need help figuring out how to properly parse this data onto a microservice URL. Any assistance would be greatly appreciated. Here's what I have so f ...

Instructions on how to automatically navigate to a different tab upon clicking an <a> element on a separate webpage, and subsequently display the designated tab on that

I have a button on my home page that, when clicked, should open a specific tab section on another page. The button is located on one page and the tabs are on a different page. On the second page, I have multiple tabs but will only mention one here, which ...

Insert a division into the table following every row

I'm working with a table that can be found here: https://codepen.io/anon/pen/bjvwOx Whenever I click on a row (for example, the 1st row 'NODE ID 1'), I want the div with the id #divTemplate to appear below that particular row, just like it d ...

I am having trouble inputting dat.gui into JSFiddle

I've developed a piece of code that functions flawlessly when executed on my local machine. Here is the direct link to it: https://jsfiddle.net/rand0mus3r/L3j7kz5a/ Upon clicking the mesh, a dat.gui instance pops up. However, there seems to be an is ...

JavaScript returns an undefined value when accessing a JSON array

I'm a beginner in the world of JavaScript and I find myself stuck on an issue. I've created a backend using Java SE, Jersey, and Jackson, everything seems to be running smoothly as my REST endpoint is up and running. However, I'm facing diff ...

Capture all div elements from the table and store them in an array

Check out this code snippet that includes draggable divs and a droppable table: http://jsbin.com/exetuk/1/edit When dragging a div to the table, copies of the div with the class .draggable appear. I am looking to create an array with divs from the table ...

Error with Google Maps Display

My goal is to achieve two things with the code snippet below: If the geocode process is unsuccessful, I want to prevent the map from being displayed (currently, I've only hidden the error message). If the geocode process is successful, I only want t ...

Implementing the if/shim binding in Knockout.js

In my project, I am looking to develop a unique custom binding that functions similarly to the if binding. However, instead of completely removing the element from view, I want it to be replaced with another element of equal height whenever it needs to be ...

How to delete an element from a session array using Jquery and Ajax techniques

In my table, each element in an array corresponds to a row. I'm attempting to delete an element when the delete image (with the id deleteRowButton) is clicked. Currently, nothing happens upon clicking the image. However, if I remove the line var index ...

Is there a way to execute a function immediately after an element has completed rendering?

I am facing a challenge where I need to dynamically set the height of one element based on the height of another element. Initially, when the target element finishes rendering, it's 490px tall. However, when I try to retrieve the height of the other ...

Is the object returned by the useParams hook maintained across renders?

The book that is displayed is based on the URL parameter obtained from the useParams hook. The selected book remains constant across renders unless there is a change in the value returned by the useParams hook. I am curious to find out if the object retur ...

Utilizing a Custom Hook for Updating Components in useEffect

I am facing an issue with the following code snippet: function checklogin(callback) { if (!state.user.authenticated) pushhistory("/accounts/login", function(){teamhome2_message();}); else callback(); } // TRYING TO CONVERT IT INTO ...

The jQuery .Text() method efficiently replaces the contents of an element with fresh

I am experiencing an issue where the .text() function in jQuery replaces both the text and HTML within an element. I'm looking for a solution that allows me to only modify the actual text and leave the HTML intact. Any ideas on how I can achieve this? ...

Swap out the default URL in components with the global constant

Could anyone offer some assistance with this task I have at hand: Let's imagine we have a global constant 'env' that I need to use to replace template URLs in components during build time. Each component has a default template URL, but for ...

Struggling to retrieve information using the filter() method in MongoDB

I am currently attempting to retrieve the tasks assigned to a specific user using this setup router.get('/all', auth, async (req, res) => { try { const assignments_raw = await Assignment.find({listP: listP.indexOf(req.user.userId)}) ...

jquery interval randomly selecting a new option instead of verifying the correct answer

Below is a jQuery game created with aspx that runs on a 3000 millisecond interval. $(document).ready(function () { var interval; //initialize timer interval = setInterval(function () { //create timer ...