Set the mesh position in Three.js to the coordinates 0,0,0

I'm currently working on positioning a basic cube at coordinates 0,0,0.

When I try to position the cube at 0,0,0, I end up with this outcome: https://i.sstatic.net/S2zom.png

However, this is not the desired result. Here is what I am aiming for: https://i.sstatic.net/lFLjq.png

The code I am using is quite simple:

const cube = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1));
cube.position.x = 0;
cube.position.z = 0;
cube.position.y = 0;
cube.material = new THREE.MeshPhongMaterial({ color: 'green' });
this.scene.add(cube);

Answer №1

To find the center of the mesh, consider that the BoxGeometry places the mesh with a range from (-width/2, -height/2, -depth/2) to (width/2, height/2, depth/2). Given a cube size of (1, 1, 1), the cube's minimum point is at (-0.5, -0.5, -0.5). To adjust the position of the cube to its center, you need to shift it by (0.5, 0.5, 0.5):

const cube = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1));
cube.position.set(0.5, 0.5, 0.5)

Instead of manually modifying the mesh position, an alternative is to translate the geometry itself:

const cubeGeometry = new THREE.BoxGeometry(1, 1, 1).translate(0.5, 0.5, 0.5);
const cube = new THREE.Mesh(cubeGeometry);

For a more universal approach, you can translate the mesh based on the minimum of its bounding box dimensions:

const geometry = new THREE.BoxGeometry(1, 1, 1);

// This method applies to any geometry
geometry.computeBoundingBox();
geometry.translate(
    -geometry.boundingBox.min.x,
    -geometry.boundingBox.min.y,
    -geometry.boundingBox.min.z);

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

How can I create a custom checkbox in React Bootstrap without specifying an ID

I'm struggling to understand why this seemingly straightforward Bootstrap custom checkbox isn't functioning as expected. My project involves React, Bootstrap, and React-Bootstrap. import React from "react"; import ReactDOM from "react-dom"; impo ...

What is the best way to dynamically add getJSON's data to a div whenever the loadmore button is clicked?

When a page loads, my getJSON function displays its output in a div called myDiv. Now, I am looking to add a button at the bottom of the page. When the user clicks this button, I want to trigger another call to getJSON. Each time the button is clicked, I ...

How can we verify that the client has successfully completed the file download process?

I am currently working with a single large file. My goal is to accomplish the following tasks: 1) Upload the file to the server 2) Automatically delete the file from the server once it has been successfully downloaded by the user. Is there a method to ...

Issue arises when attempting to render a component while utilizing window.location.pathname and window.location.hash in conjunction with a navigation bar

I am encountering a problem when attempting to render a react component using a navigation bar. I have experimented with both Switch case and if-statement methods. The first approach involves using window.location.hash, which successfully alters the URL u ...

Incorporating an external SVG link into a React application

While I may not be an SVG expert, I haven't encountered any issues with loading SVGs in my React app so far. I prefer using the svg tag over the image tag because sizing tends to present problems with the latter option when working with external links ...

React component failing to update when props change

After loading correctly, my react component fails to re-render when props change. Despite the fact that render() is being called and the list array contains the correct data according to console.log(list), the page does not refresh. Adding a setState in co ...

Tips on obtaining the element that was used as the event selector

I am facing a challenge with a specific issue. Within a div containing various elements, I have implemented a mouseover event. The issue arises when trying to target this particular div in the mouseover function, as there are multiple automatically genera ...

What are the steps to update the title and creator details in the package.json file of an Openshift Node.js application?

Recently delving into the world of node.js and PaaS platforms like Openshift, I find myself faced with a perplexing issue. How exactly can I modify the values generated by Openshift in the package.json file without encountering any errors? Each time I at ...

The technique of using Javascript x to escape characters is

I've come across a handful of other software programs that feature a similar construct: let str = '\x32\x20\x60\x78\x6e\x7a\x9c\x89'; As I experimented with the sequence of numbers and letters within ...

Having trouble accessing a CSV file with JQuery and FileContentResult?

How can I make an ajax call without using ActionLink in my controller? Here is a snippet of my controller code: public IActionResult ExportUsers(List<string> listOfEmails) { /*some data processing*/ return File(result, "text/csv", ...

Tips and tricks for retaining the collapsed state upon reloading Bootstrap 5

Just diving into the world of bootstrap and javascript. How do I save the collapsed state to make sure it stays even after refreshing the page? <p> <button class="btn btn-primary" type="button" data-bs-toggle="collapse&q ...

Issue with JQuery on Mobile Devices: Troubles with Dropdown Menu and Loading Function

Having some trouble with my JQuery code on mobile devices. The drop down menu isn't showing up on an iPhone, the load function isn't working to display the additional PHP page on a Samsung Edge 7, and the drop down doesn't seem to be functio ...

Give drawn elements a touch of fuzziness and experiment with the color of the fragment shader

I am currently experimenting with creating an animated gradient effect by blending three separate blobs together in a melting-like fashion, with each blob moving independently. The desired result can be seen in the image provided below. So far, I have bee ...

Ways to assign a CSS class specifically for images

.calendarList { background-image: url('/resource3/hpsc/common/images/calendar.png'); background-position: 135px 50%; background-repeat: no-repeat; cursor:pointer; } <input type="text" id="toDatepicker" class="cal ...

Ways to create a rounded pane for Highchart's Solidgauge

I've noticed that my arcs have rounded edges, but the pane is flat. Is there a way to make the pane rounded as well? I am currently using Highcharts v9.1.2. I tried using "stroke-linejoin: "round"", but it didn't work. https://i.sstati ...

Ensure that both the top row and leftmost column are fixed in a vertical header using JQuery DataTable

Check out the implementation of vertical header flow in Datatables by visiting: https://jsfiddle.net/2unr54zc/ While I have successfully fixed the columns on horizontal scroll, I'm facing difficulty in fixing the first two rows when vertically scroll ...

Error encountered when attempting to export a TypeScript class from an AngularJS module

In my application using Angular and TypeScript, I have encountered a scenario where I want to inherit a class from one module into another file: generics.ts: module app.generics{ export class BaseClass{ someMethod(): void{ alert(" ...

Click to save image using jQuery

I am trying to implement a feature where clicking on an image allows users to download it. I am using the download attribute for this purpose. <a href="http://mysite.ru/userfiles/certificate_2.png" class="download-certificate-link" data-title="certific ...

What is the best way to keep a button visible at all times and active without needing to be clicked?

<v-card :style="{ textAlign: 'left' }" class="basic-card pa-6" :class="{ 'small-padding': !$vuetify.breakpoint.xl }" elevation="0" flat :height="windowHeight - 104 + 'px&ap ...

Update the value in a nested object array by cross-referencing it with a second nested object array and inserting the object into the specified

I have a large array of objects with over 10,000 records. Each object contains an array in a specific key value, which needs to be iterated and compared with another array of objects. If there is a match, I want to replace that value with the corresponding ...