Rotating the Three.js plane geometry causes it to vanish

I am attempting to create a simple scene with a triangular plane continuously rotating around the x-axis.

Below is the code to create the geometry object, based on a previous question on Stack Overflow:

// create triangular plane geometry
var geometry_1 = new THREE.Geometry();
var v1 = new THREE.Vector3(0,0,0);
var v2 = new THREE.Vector3(3,0,0);
var v3 = new THREE.Vector3(0,3,0);
geometry_1.vertices.push(v1); 
geometry_1.vertices.push(v2); 
geometry_1.vertices.push(v3);
geometry_1.faces.push(new THREE.Face3(0, 1, 2));

The animation function animates the scene and applies a slight rotation to the mesh:

function animate() {
    requestAnimationFrame( animate );
    mesh_1.rotation.x += 0.005;
    renderer.render( scene, camera );
}

Everything works well until the value of mesh.rotation.x reaches the [Math.PI, 2*Math.PI] range, causing it to disappear for half of the cycle. You can see this behavior in this JSFiddle.

  • This issue is not related to lighting, as there are ambient and directional lights supposed to illuminate the mesh throughout its rotation.
  • The problem is not with the material either, since I have set its side property to THREE.DoubleSide, and both faces are visible within the [0, Math.PI] range.
  • I have attempted to add another face to the geometry using
    geometry_1.faces.push(new THREE.Face3(0, 2, 1));
    , but the problem persists.
  • As a workaround, I added a second geometry with an opposite face using
    geometry_2.faces.push(new THREE.Face3(0, 2, 1));
    and reversed the rotation with mesh_2.rotation.x -= 0.005; to achieve the desired result. However, this is not an ideal solution.

Any insights on what might be causing this issue and how to resolve it would be greatly appreciated. Thank you!

Answer №1

Read the Documentation for more information

OrthographicCamera( left, right, top, bottom, near, far )

To set up your camera correctly, use the following code:

camera = new THREE.OrthographicCamera(-FRUSTUM_SIDE/2, FRUSTUM_SIDE/2, 
                                       FRUSTUM_SIDE/2, -FRUSTUM_SIDE/2);

This configuration will give you default values for the near and far camera frustrum planes (0.1 and 2000).

Explanation: By positioning your camera at z-position FRUSTRUM_SIDE/2, you are setting the far camera frustrum plane at the same distance. This means you can see everything between your camera's position and a distance of FRUSTRUM_SIDE/2 away. In world coordinates, your far plane is located at point (0, 0, 0). Therefore, if an object moves beyond a distance of FRUSTRUM_SIDE/2 from your camera, it will disappear.

Answer №2

Expanding on the response by @prisoner849, the issue demonstrated in the JSFiddle is not related to the geometry or material of the mesh, but instead to the configuration and dimensions of the frustum set by the OrthographicCamera.

As elaborated in a helpful video tutorial and the documentation, the frustum of an OrthographicCamera is a rectangular parallelepiped defined by the values

left, right, top, bottom, near, far
:

https://i.sstatic.net/PYC2Y.png

The camera should essentially be perceived as being attached to the near side surface and oriented towards negative values of the z axis.

Once the frustum's shape is set with:

FRUSTUM_SIDE = 20;
camera = new THREE.OrthographicCamera(
   -FRUSTUM_SIDE/2, FRUSTUM_SIDE/2, 
   FRUSTUM_SIDE/2, -FRUSTUM_SIDE/2,
   -FRUSTUM_SIDE/2, FRUSTUM_SIDE/2);

we will be able to visualize all the objects in the scene that are completely enclosed within it.

However, changing the camera's position after defining the frustum:

camera.position.z = FRUSTUM_SIDE/2;
(line 24 of the fiddle) repositions the entire frustum rather than just the image location. Consequently, any object previously at (0,0,0) which was at the center of the frustum will now be situated on the far end plane.

The animation function:

function animate() {
    requestAnimationFrame( animate );
    mesh_1.rotation.x += 0.005;
    renderer.render( scene, camera );
}

causes the triangle to rotate towards the image plane, but for angles between [Math.PI, 2*Math.PI], the plane is directed outside the frustum and hence becomes invisible.

Options to address this issue include removing the line

camera.position.z = FRUSTUM_SIDE/2;
, altering the frustum configuration, or repositioning the mesh at the center of the new frustum. For example, check the revised fiddle.

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

Is there a way to use Javascript or JQuery to conceal all unchecked items in a RadioButtonList?

Currently, I am utilizing the Asp .net repeater to bind the selected value in the RadioButton list. Here is an example snippet: <asp:RadioButtonList ID="RadioButtonList1" runat="server" SelectedValue='<%# Eval("Numbers& ...

Tips on handling a Vue computed property

I am struggling to dispatch an object that is created within a computed property in vue.js. Being fairly new to this framework, I can't seem to make it work. My goal is to dispatch the object named "updateObject" to the vuex-store. I have tried using ...

The error handler function is missing in Zepto's $.post method

When I was using Zepto instead of jQuery, I noticed that while the $.ajax method has an error handler, other methods like $.post and $.get do not. Do you know why this is the case? Function Signature $.post(url, [data], function(data, status, xhr){ ... }, ...

Youtube video embedding showing cut edges on smaller screens

Looking for assistance with a Next.js and tailwind site I am building. Having trouble getting the video component to display properly on mobile screens. Tried various fixes but the video still gets cut off on smaller screen sizes. If anyone has a soluti ...

Creating distinctive ng-form tags within a form using ng-repeat in Angular

I need help with creating a form that includes a table looping over a list of objects. Each object should have checkboxes for the user to check/uncheck attributes. The issue I am facing is setting the ng-model attribute on the checkboxes. This is what my ...

What methods can be used to monitor changes made to thumbnails on the YouTube platform?

I have embarked on a project to create a Chrome extension that alters the information displayed on the thumbnails of YouTube's recommended videos. In this case, I am looking to replace the video length with the name of the channel. Imagine you are on ...

Utilize the match() method in JavaScript to target and extract a whole paragraph

I am attempting to extract an entire paragraph from a text file using JavaScript and then display it in an HTML element. Despite reading numerous posts and articles, I have been unable to find the correct regex formula. Here is an excerpt from the text fil ...

Transfer data via ajax to the controller

I need assistance with storing a file in my database using Tabulator without having a form already created. I am currently creating a simple input element like this: var editor = document.createElement("input");. After clicking the file, it trigg ...

having trouble with displaying the results on the webpage

I am facing an issue while trying to display the data fetched from the database: {"results": ["USA", "Canada"]} {"message":"Could not find any countries."} //else An error was encountered in the console: Uncaught TypeError: Cannot read property 'l ...

When a webpage loaded through ajax, an error may occur indicating that Javascript functions are not

The functions have been defined and are functioning properly. However, when the same page is loaded via ajax, an error stating callA is not defined appears in the firebug console. I'm puzzled as to why this works in one scenario but not the other. Am ...

Updating Kendo by modifying the Angular model

While working on a project with Angular, I recently discovered the Kendo-Angular project available at . I successfully integrated Angular-Kendo into my project and it seems to be functioning well, except for updating models in the way I am accustomed to. ...

The issue with Framer motion's whileInView is that it does not animate the element when it is within the viewport in Next.js

In my current Next.js 14 project with the App Router, I decided to play around with animations using Framer Motion. One specific feature I wanted to implement was animating text elements into view as they enter the viewport. The idea is for the text to gra ...

Refresh the information stored in the spliced array of objects

I've managed to splice the data and now I need to update its object from disabled = true to disabled = false. I have searched for another solution but couldn't find one... Any advice is welcomed. Thank you. This is my dropdown: const newDrop = ...

Turn off the authentication middleware for a particular HTTP method on a specific endpoint

Currently, I am using Express/Node and have developed authentication middleware to validate JWT on each request. My requirement is to disable this middleware for a specific route (POST '/api/user/') while keeping it active for another route (GET ...

Toggle the visibility of table rows based on a specific class using a checkbox

I am currently working on a code that displays the results of a query in a table format. I would like to have the ability to click on certain elements to show or hide them. I know this can be achieved using toggle and CSS, but I keep encountering obstacles ...

Issue with unrecognized expression in JQuery when processing Ajax response

Recently, I implemented a JQuery Ajax Form on my website. $('#modal-body-sign-in').on('submit', '#sign-in', function(e) { e.preventDefault(); var data = $(this).serialize(); var url = $(this).attr(&apo ...

Minimize a collection of JavaScript objects

I am working with an Array of objects where I need to return the collection as an Object, with the key names being the indexes of their length. Additionally, I have to filter this object based on its values. Check out my code snippet below: const data = ...

Uploading files and data in Laravel using Vue.js and Vuetify: A step-by-step guide

My Vuetify form is working fine with text input, but when I try to include images, I encounter the error GET http://danza.test/thumbnails[object%20File] 404 (Not Found). How can I successfully pass both text and images in a form? This is part of the code ...

Guide on setting a JSON object using the getJSON method

In my coding project, I am trying to assign values of lat and lng from a JSON object named branch to a variable called jsonData. When I use console.log to check jsonData.responseJSON.positions.length or jsonData.responseJSON.positions[0].lat, etc. I c ...

Injecting resolve into Angular controller and encountering undefined value in logging operation

My setup involves the following: .state('posts', { url: '/posts/{id}', templateUrl: 'posts.html', controller: 'postsController as postsCtrl', resolve: { post: getSinglePostWrapper ...