Location of the observable space

I have set up a spherical mesh with a texture and positioned a perspective camera to enable a 360-degree view inside out.

this.camera = new THREE.PerspectiveCamera(this.fov, $(this.element).width() / $(this.element).height(), 0.1, 1000);
this.camera.setLens(this.fov);
this.mesh = new THREE.Mesh( new THREE.SphereGeometry( 500, 80, 50 ), new THREE.MeshBasicMaterial( { map: this.texture } ) );
this.mesh.scale.x = -1;
this.camera.lookAt(new THREE.Vector3(cx, cy, cz));
this.renderer.render( this.scene, this.camera );

I am interested in calculating the current view area and position of the viewer within the mesh, essentially determining the visible area of the plane.

When I refer to the viewable area, I am talking about what section of the image texture is currently visible in the browser window, such as

(0,0)(x,y)(400,500)(width,height)
. If the user moves the mouse, it might change to something like
(50,0)(x,y)(400,500)(width,height)
, and if they zoom in, perhaps
(50,40)(x,y)(300,400)(width,height)
. While I've come across formulas for calculating width and height based on the camera's field of view, I haven't been able to find a solution for determining x and y coordinates.

Answer №1

Use this script to calculate the u,v coordinates in your texture based on the x,y,z values of the camera's lookat vector:


    var u, v;

    // V coordinate
    if (x == 0 && z == 0)
    {
        if (y > 0)
            v = 1;
        else
            if (y < 0) v = -1;
    }
    else
    {
        v = 0.5 + Math.atan( y / Math.sqrt(x*x + z*z) ) / Math.PI;
    }

    // U coordinate
    if (z == 0)
    {
        if (x < 0)
            u = 0.5;
        if (x > 0)
            u = 0;
    }
    else
    {
        u = 0.25 - Math.atan(x/z) / (2 * Math.PI);
        if (z < 0) u += 0.5;
    }

To get precise pixel positions on the texture, multiply u and v by the width and height of the texture respectively.

Note that u is undefined when both x and z are zero.

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

A jQuery component with built-in event triggers

Here's an example that illustrates the concept: a widget needs to utilize its own method as a callback: $.widget("custom.mywidget", { _create: function() { this.invoke("mymodule", "myaction", {arg: this.options.value}, this.parseData); ...

I encountered a TypeScript error while utilizing the useContext hook in a React application

Initially, I set the value of createContext to an empty object {}. Afterwards, I provided a context with a new value of {username: 'sasuke1'}. However, when I attempt to access the property Context.username, TypeScript raises the following error: ...

Utilize Quasar CSS breakpoints within Vue expressions for responsive design

I am currently using the QDialog component from Quasar Framework and I want to set the value of the maximized property based on the current screen size, specifically being maximized for small screens only. Is there a way for me to reference a variable in ...

Adding a promise to an array using Javascript

I am facing an issue while attempting to create an array of promises and then calling them using Promise.all. The problem lies in correctly pushing the functions into the array. It seems like they are getting executed instead of being inserted and waiting ...

Navigate through a nested JSON structure and update the data points

In my possession is a JSON tree structured as follows: { "projects": { "Proj1": { "milestones": { "default": "20150101", "default2": "20140406", "default3": "20140101", ...

Struggling to customize checkbox design using HTML and CSS

Why am I unable to style HTML checkboxes with CSS? No matter what I try, the appearance of the checkboxes remains unchanged: Here is the HTML code: <input type="checkbox" id="checkbox-1-1" class="regular-checkbox" /> <nput type="checkbox" id ...

Unable to showcase the chosen option utilizing select2

Utilizing angular-ui's select2 directive has been a bit of a challenge. While the functionality is there, I've encountered an issue where the selected value isn't being displayed properly due to my implementation workaround. <select ...

Submitting a file using Ajax XMLHttpRequest

Currently, I am facing an issue while attempting to send a file using the following XMLHttpRequest code. var url= "http://localhost:80/...."; $(document).ready(function(){ document.getElementById('upload').addEventListener('cha ...

Resolving the issue: "How to fix the error "Credentials are not supported if the CORS header 'Access-Control-Allow-Origin' is '*' in React?"

Recently, I encountered some CORS issues while using a third party API in my front-end application. After reaching out to the API maintainers, they lifted the CORS control by adding a * to Access-Control-Allow-Origin, which seemed like the perfect solution ...

Iterate through the input values using a for loop and output the value of each individual input

I am working on an express app where I need to loop through data in my EJS file to create 5 hidden input fields, each with a unique value. However, I am struggling to extract the values of these inputs using JavaScript. Despite trying multiple methods, I ...

The issue with three.js preserveDrawingBuffer not functioning properly persists on iOS devices

In my current project, I have implemented a particle system that leaves trails behind. While this feature is working smoothly on all browsers, I am facing an issue on iOS devices with both Safari and Chrome. The problem is that even though the particles ar ...

A 'select all' checkbox in a PHP 'foreach loop' with JavaScript

I am in search of a solution to implement JavaScript in the given code block. My goal is to have the checkbox with id = alles automatically check all the checkboxes that are generated within the while loop. <form id="andere" name="andere" action="<? ...

What is the process for creating a symbolic link from a project's node_modules directory?

Recently, I've been learning how to build a Password Manager using React, Node.js, and MYSQL by following a tutorial. However, I encountered an issue where the file /EncryptionHandler was located outside of the src/ directory of my project. Even thoug ...

Is it possible to utilize jQuery's .wrap or .wrapInner to encase a variety of elements within them?

I am working with a HTML structure that looks like this: <section> <item> <ele class="blue" /> <ele class="green" /> <ele class="red" /> </item> <item> <ele class="blue" /> <ele ...

Discovering a locator based on the initial portion of its value

Here's a piece of code that is used to click on a specific locator: await page.locator('#react-select-4-option-0').click(); Is there a way to click on a locator based on only the initial part of the code, like this: await page.locator(&apos ...

What's the deal with the max element indices/vertices in WebGL2?

In the process of developing a web component using WebGL2 (and three.js) with OES_element_index_uint enabled, I encountered an unexpected anomaly. When drawing a geometry using indexed vertices, strange lines appeared across the middle of the figure: http ...

Is there a way to turn off predictive text for reCAPTCHA on my mobile device?

Using reCAPTCHA on a sign-up form can get frustrating on mobile devices, with constant dictionary word suggestions while typing. I am aware of how to disable this feature for normal input fields by adding attributes through JavaScript, but shouldn't ...

Deactivate button based on two criteria

I am currently facing an issue with jQuery where I need to disable a button based on two conditions. The first condition requires the user to select an option, and the second condition requires the user to upload a file. While I have managed to write the c ...

Executing an SQL delete query with a button click using a JavaScript function in PHP

I have created a setup with three essential files - index.html, database.php, and function.js. In database.php, there is a form generated containing a delete button that triggers the deletion SQL query when clicked. The primary objective is to present a ta ...

What strategies can I use to optimize the code for the function provided?

This function allows for the dynamic display of select tags for location selection based on employee designation. The current code functions correctly, but I believe it can be optimized further. // Function to activate different location options based on e ...