Convert a rectangular BoxGeometry into a spherical shape

I'm currently working on a project where I have a box with various textures on each face. My goal is to create a quad sphere using the same textures from the box. After researching a method for transforming the cube into a sphere, I came across this informative post:

Since I am relatively new to threejs (and 3D programming in general), my initial attempt was to apply the suggested formula directly to the geometry vertices. However, this resulted in simply shrinking the size of the box.

var geometry = new THREE.BoxGeometry(2, 2, 2);
// Material and texture details omitted
var vertices = cube.geometry.vertices;
var sqrt = Math.sqrt;
cube.geometry.dynamic = true;    

for (var i = 0; i < vertices.length; i++) {
    var v = vertices[i];
    var dx = v.x * sqrt(1.0 - ((v.y * v.y) / 2.0) - ((v.z * v.z) / 2.0) + ((v.y * v.y * v.z * v.z) / 3.0));
    var dy = v.y * sqrt(1.0 - ((v.z * v.z) / 2.0) - ((v.x * v.x) / 2.0) + ((v.z * v.z * v.x * v.x) / 3.0));
    var dz = v.z * sqrt(1.0 - ((v.x * v.x) / 2.0) - ((v.y * v.y) / 2.0) + ((v.x * v.x * v.y * v.y) / 3.0));
    vertices[i].set(dx, dy, dz);
}
cube.geometry.verticesNeedUpdate = true;

If anyone has suggestions or tips on how I could successfully achieve this transformation, I would greatly appreciate it!

Answer №1

Check out this great example that demonstrates exactly what you're looking for:

Take a look at the following snippet of code to see how it's done:

var geometry = new THREE.SphereGeometry( 250, 32, 32 );

for ( var i = 0; i < geometry.vertices.length; i ++ ) {

    var vertex = geometry.vertices[ i ];
    vertex.normalize().multiplyScalar( 500 );

}

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

Using useState props in React Native navigation screens with React Navigation

I'm currently working on my first React Native app with react navigation after previously having a background in web react development. In the past, I typically used useState for managing state. For instance, rendering a list of components based on d ...

What is the best way to sort inertia data on the client-side?

Looking for a solution with my component that receives data, I have the following setup in my controller: route::get('/', function() { return inertia('foo', ['data' => Model::all()]) }); Within my Vue component: <templa ...

Tips to prevent modal impacts on underlying elements?

When I click on a button, a modal pops up. However, the background contents seem to spread out and sometimes scroll automatically when the modal appears. I have searched for a solution to this issue but have been unsuccessful. Can anyone please help me ide ...

Sending a post request from JavaScript to Django Rest Framework

I am working with a DFR api endpoint: url = http://example.com/api/data/ The URL of the page where I am running JavaScript code is: http://example.com/page/1/ I have logged in as User1 in my browser. POST request - from DRF browser API - successful. G ...

Issues with plugins in Rails 4 are causing functionality to not be operating

I recently installed a template and encountered an issue with some of the JavaScript functionality. However, upon checking the compiled list of JavaScript files, I can see that all the files have loaded successfully and the CSS includes all the necessary f ...

Question about using the map method in Javascript to generate an array with undefined elements

I've been attempting to utilize the .map method in order to create a new array named workspaces, containing only the ids from the existing array. However, despite the console.log() displaying the ids as expected, the return statement does not populate ...

Conflicting directives are canceling each other out

I am facing an issue with my two directives. One directive is responsible for checking the file size, while the other ensures that the uploaded file format is valid. When both directives are assigned to an input=file element separately, they work fine. How ...

Adjust the color of each list item depending on an array of strings

Within my perspective, I possess a collection of different statuses. <ul> <li>FIRST_STATUS</li> <li>SECOND_STATUS</li> <li>THIRD_STATUS</li> </ul> To continuously update the statuses in my contr ...

Having trouble displaying Firebase Firestore data when using getStaticProps in Next.js?

I'm currently facing an issue while fetching data from Firebase Firestore using Next.js's getStaticProps method. When I try to utilize getStaticProps, I encounter the following error: res.json() is not a function This is how my implementation ...

What is the process for sending a post request with a JSON payload to an external API using Node.js?

I am currently facing an issue while attempting to send a POST request with a JSON payload to an external API using node.js. Below is the code I am using: var express = require('express'); var bodyParser = require('body-parser'); var ...

Incorporate the key as a prop within a Child Component in a React application

I am trying to display a list of elements in React, where the key of each element is used as an index in front of the item. However, when I try to access props.key, it just returns undefined. Does anyone have any suggestions on how to access the key proper ...

What is the process for configuring browser environment variables in Next.js with the `"type": "module"` setting?

While working on my nextjs project, I have encountered an issue with setting environment variables that can be accessed at the browser level. Whenever I use console.log(process.env), it always returns {} in the javascript console, and any variable like pr ...

Updating by clicking with auto-prediction feature

I have implemented an autosuggestion feature to display results from a database table while typing in an HTML field, and I am looking to utilize JavaScript to post another value from the same row where the autosuggested values are stored. https://i.stack. ...

I designed three interactive buttons that allow users to change the language of the website with a simple click. While my JavaScript code functions perfectly on local host, it seems to encounter issues when deployed

For those interested, the live server can be accessed at . After creating 3 buttons that change the language of the website when clicked, the issue arose where the JavaScript code worked perfectly on localhost but failed to function properly on the online ...

Enhance the functionality of jQuery sortable by including additional details

I have a li list that I have implemented sortable functionality using jQuery. In order to ensure that the updated data is sent to the correct destination, I need to include some hidden values in the serialized data. How can I achieve this? HTML <ul i ...

Deselect a selected radio button by clicking on it

I am trying to create a radio button with an unchecked value, and I thought this code would work: $('form').on('click', 'input[type="radio"]:checked', function (event) { $(this).prop("checked", false); }); It seems simpl ...

Confusion surrounding the Javascript object prototype (utilizing require.js and three.js)

I'm feeling a bit confused about my understanding of Objects, prototypes, require.js and three.js. Let me illustrate with an example: In Summary: The instance "wee" is derived from "Wee," which acts as a wrapper or monkey patch for THREE... Shouldn& ...

Initial input firing empty string on React's onChange event

Issue - When I input text for the first time, no string output is displayed. As a result, the last character of the input is not showing up. What could be causing this? Specific focus - Concentrating on the name property const [options, setOptions] = useS ...

When attempting to import classes from files that import additional classes in Angular 8 with WebWorker, compilation errors arise

Currently in Angular v8 and encountering an issue with my model.ts file structure, which includes the following code: import {map} from 'rxjs/operators'; export class Person { constructor() { } } In addition, I have a WebWorker file called te ...

How Should One Properly Describe an Object with Multiple Dimensions?

PHP features multidimensional arrays, meaning an array that contains multiple arrays within it. When working with JavaScript, how does one refer to an object that holds multiple objects? Is it called a multidimensional object or is there a different termi ...