Strange outcome received from THREE.JS - Vector3.project function

Currently I am working on a project in THREE.JS and I am encountering an issue with projecting a Vector3. When attempting to project the Vector3 (0,0,0), which should ideally appear at the center of my camera screen, I receive NaN as the result. Surprisingly, other Vector3s that are all visible (though visibility shouldn't impact this, right?) are returning values like NaN, 0, Infinity, and various random numbers between -1 and 0 most of the time. Below is a snippet of the code I am using:

camera.position.x=32;
camera.position.y=32;
camera.position.z=32;
camera.lookAt(new THREE.Vector3(0,0,0));
for(var x=0;x<=16;x++)
{
    for(var z=0;z<=16;z++)
    {
        var p=new THREE.Vector3(x,0,z).project(camera);
        alert(p.x+" "+p.y+" "+p.z);
        //I'm stuck here!!!
    }
}

Any help or guidance would be greatly appreciated :)

Answer №1

Encountered an issue while using a PerspectiveCamera to compute the projected coordinates of Vector3s without utilizing a Scene or WebGLRenderer.

To resolve this, I found that by calling camera.getWorldPosition(), it refreshed the camera's world matrix and allowed point.project(camera) to function correctly as intended.

Answer №2

When setting the camera position, it is important to use setters in newer versions such as r67. To set the position, you can do the following:

camera.position.setX( 50 );
camera.position.setY( 50 );
camera.position.setZ( 50 );

You can also set the position using a single line like this:

camera.position.set( 50, 50, 50 );

If you haven't used setters, chances are that your camera's position is at the default (0, 0, 0).

To avoid projecting a Vector from itself to itself, you can add a condition to check if the camera position is equal to the vector before executing the projection:

vector = new THREE.Vector3( x, y, z );
if( ! camera.position.equals( vector ) ){
    var p = vector.project(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

An issue has been discovered with the Search function as JavaScript's Array.filter() and .map() methods are not functioning properly, resulting in

Currently, I'm working on integrating a search feature into my Flask application that will display the cities entered by users and are present in the JSON API results of a weather API. I am following a tutorial and have used a code similar to this: h ...

Tips for utilizing two renderCell functions in a datagrid from Material UI without encountering duplication or repetition

Utilizing Material UI, I have designed a table to showcase my data. In this setup, I am required to use renderCell for two specific properties: 'level by user' and 'level by referent'. Issue: The problem arises when Material UI displa ...

Guidelines for populating data with an array

I have the following array: const dataArr = [[15, 14, 5], [16, 10, 2], [17, 6, 13], [18, 4, 8], [19, 7, 4]]; Now, I am populating another array using the above data as shown below: for (let i=0; i<dataArr.length; i++){ newData.push(dataArr[i]); ...

Every time I attempt to insert a background image into a div using jQuery, I am consistently faced with a 404 error message

When I hit enter in my search bar, a new div is created each time. However, I am struggling to assign a background image to the created div as I keep receiving a 404 error in the console. Below is the code snippet I'm working with: function appendToD ...

position a div element at the bottom of its parent container

I am facing a challenge with this issue: I want to position a red div at the bottom of another div. The red div should always stay at the bottom of its parent div. .homepage-wrapper{ max-width: 1028px; margin-left: auto; ...

Updating an array with complex values in React using the useState hook in

I am working with a long array where I need to update the quantity under the misc array for each person. Each person in the array has a list of miscellaneous items, and each of those items can have their own array with quantities that need to be updated. T ...

Activate Button upon Textbox/Combobox/RadDatePicker Modification through JavaScript

On my ASP.NET form, I have various input elements including textboxes, dropdowns, date pickers, and update buttons. My goal is to enable the update button whenever a user makes a change in any of these fields. To achieve this, I applied a specific CSS cla ...

Unexpected JSONP Parsing Issue Despite Correct JSON Data

I've implemented a Cross Domain AJAX request using JSONP, and it's working fine with CORS. However, I'm facing an issue with JSONP. I've checked other threads but couldn't find a solution for my case. Below is the code snippet: ...

Tips on displaying a confirmation box when the page is refreshed or closed

I need to implement a confirmation box for users who try to close the window without saving the content of a textarea within a form. Here is the code I currently have: var myEvent = window.attachEvent || window.addEventListener; var chkevent = window.att ...

JavaScript: Receiving an error that function is undefined when working with data binding

Why is my code showing that it's not defined while I'm attempting a simple code with data binding? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="ht ...

Ensure that the form is validated even when setState is not executed immediately

I am currently working on a form in React and I am facing an issue with validation. When the Submit Form button is clicked without filling in the input fields, an error should be displayed. This part is functioning correctly. However, even when the fields ...

Is there a way to incorporate my JavaScript code into my page.jsx file efficiently?

Can I integrate this javascript code from a .js file into my .jsx file seamlessly? const { exportTraceState } = require("next/dist/trace"); const toggleBtn = document.querySelector('.toggle_btn') const toggleBtnIcon = document.querySe ...

"Trouble with Custom Shader in Three.js - Not Showing Up

Delving into the world of shaders and three.js has been an exciting journey for me. However, I encountered a little bump in the road when trying to display a shader with a texture - it simply shows up as all black. If you want to take a look at the codepe ...

What is the best way to send multiple requests for the same file and retrieve the necessary response in PHP using jQuery AJAX?

var postID = $post->ID; $.ajax({ type: "POST", url: "<?php echo get_template_directory_uri();?>/b.php", data:{postID:postID}, dataType: 'json', success: function(re ...

Strategies for Resolving Table Alignment Problems using HTML and JavaScript

I am struggling to figure out how this dashboard will function as it is not a live website, but rather a tool that pulls data from a chemical analysis program and displays it in a browser view. My main issue is aligning two tables at the top of the page ...

Using the TIMESTAMP data type in PostgreSQL and getting the most out of it

After saving a Luxon datetime value in a TIMESTAMP(3) type column in a postgres database, I encountered difficulty using it for various operations like converting time zones. Despite creating the object with the code snippet below: const { DateTime } = req ...

Top method for cycling through an Array in React

I am looking to create a memory game using React for a portfolio project. I have an array of 20 items with 10 different sets of colors. const data = [ // Manchester Blue { ID: 1, Color: "#1C2C5B" }, ...

What is the best way to create a TypeScript interface or type definition for my constant variable?

I'm facing challenges in defining an interface or type for my dataset, and encountering some errors. Here is the incorrect interfaces and code that I'm using: interface IVehicle { [key: number]: { model: string, year: number }; } interface IV ...

Is there a way to prevent specific objects from being dropped?

Imagine having object1, object2, and object3. They are designated for dropping only in Zone1 Now, consider object4, object5, and object6. They can only be dropped in Zone2 How do I set up this configuration? Additionally, I want object7 to be droppable ...

Is there a term similar to "Rise above the Rest" in the world of Web Development?

Hey, I've encountered a new issue right now. Currently, I have two elements that are fixed to the top and bottom of the page. However, the elements in between them are overlapping the top element. Even though I tried keeping both elements fixed, th ...