Design Your Own Custom Spherical Section Using Three.js

Currently, I am working on a project involving a sphere in THREE.js that I like to refer to as the master sphere. In this project, I have enabled the user to pinpoint multiple spots on the sphere which serve as markers for the corners of a polygon. These points are visualized as spheres in 3D space and lie on the surface of the master sphere. Once the user has placed three or more points, they can connect them to form a polygon.

My next goal is to generate a polygon slightly above the surface of the master sphere. This polygon should mimic the shape of the one constructed by the user but elevated just above the sphere's surface.

To achieve this, initially I attempted creating a polygon directly on the sphere's surface and then displacing each vertex along its normal using a vertex shader. For this purpose, I utilized THREE.Shape and THREE.ShapeGeometry to create a 2D shape on the z-axis. Subsequently, I translated the shape to the surface and experimented with various combinations of THREE.SubdivisionModifier and THREE.TessellateModifier to generate vertices for the shader. It was observed that without applying a modifier, the transformed shape wasn't even visible post-translation.

Presently, I am employing the following combination of modifiers, however, the outcome exhibits severe distortion:

// Initialize modifiers and specify number of divisions.
var subdivisionModifier = new THREE.SubdivisionModifier(3);
var tessellateModifier = new THREE.TessellateModifier(8);

// Modify geometry using both modifiers.
for(var x=0; x<6; x++) {
    tessellateModifier.modify(polyGeometry);
}

subdivisionModifier.modify(polyGeometry);

The result shows the yellow outline representing the intended shape, while the blue outline signifies the actual generated shape. The green wireframe depicts the master sphere.

I have created a JSFiddle demonstration of my project available here: Globe with an elevated polygon

From the current state of affairs, it is evident that the final polygon does not match the desired shape. Consequently, I find myself questioning whether I am heading towards a dead-end path and if my objective is even feasible.

Answer №1

To generate a mesh with multiple vertices, I adjusted the maximum edge length parameter in THREE.TessellateModifier and increased the number of iterations, eliminating the need for the subdivision modifier.

Check out the final code snippet:

var tessellateModifier = new THREE.TessellateModifier(.01);

//subdivisionModifier.modify(polyGeometry);

// Apply the modifier to our cloned geometry.
for(var x=0; x<25; x++) {
    tessellateModifier.modify(polyGeometry);
}

Furthermore, there's no requirement to rely on a vertex shader to distort the mesh. Simply utilize the setLength() function of each vertex to displace them and create a curved polygon just above the globe's surface.

//push each vertex to just above the surface of the globe
for(var i = 0; i < this.polyMesh.geometry.vertices.length; i++){
    this.polyMesh.geometry.vertices[i] = this.polyMesh.geometry.vertices[i].setLength(this.surfaceDisplacement);
}

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

Ensure that the mesh and object bounding boxes are perfectly aligned using auxiliary axes

It appears that the bounding box is not properly aligned with the 3D mesh. I am seeking advice on how to rotate the bounding box so that it aligns correctly with the 3D mesh. Here is a screenshot showing the misaligned object bounding box: https://i.ssta ...

Several examples of objects utilizing the identical function as the most recent instance

While working on a new feature for a Javascript library, I ran into an interesting issue. It seems that when a function of an object utilizes a closure and a promise together, all instances of the object end up using the most recently created version. This ...

Exploring Discord.js: Sorting a Collection of Retrieved Messages Using .sort()

This is a code example I am working with: .sort((a, b) => b.createdAt - a.createdAt) After fetching and filtering messages from a channel (which has a total of 8 messages), the filter operation returns a collection that may not be sorted in order. Ho ...

Tips for Displaying and Concealing Tables Using Radio Buttons

Does anyone know how to refactor the jQuery code to toggle between two selection options (Yes and No)? This is the jQuery code I have tried: $(document).ready(function() { $("#send_to_one").hide(); $("input:radio[name='decision']").chan ...

Enhancing the level of abstraction in selectors within Redux using TypeScript

Here is a custom implementation of using Redux with TypeScript and the connect method. import { connect, ConnectedProps } from 'react-redux' interface RootState { isOn: boolean } const mapState = (state: RootState) =&g ...

Using Three.js to Manipulate Objects through Their Names

Is there a way to access multiple meshes with the same name? var mesh1 = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0xffffff })); mesh1.name = "meshes"; scene.add( mesh1); var mesh2 = new THREE.Mesh( geometry, new THREE.MeshBasicMate ...

Unable to see html5 canvas content when accessing from localhost

I am having an issue with displaying code in an HTML file. When I try to view the code on localhost using MAMP, all I see is a black canvas area with a border around it. I have checked it in both Chrome and Firefox, but I keep getting the same results. Can ...

What is the reason behind Rxjs switchMap only emitting the final value from an of() observable source?

Here are two code snippets, one using map and the other using switchMap. The functionality of map is clear: of('foo', 'bar') .pipe(map((val) => sanitizer(val))) .subscribe((val) => console.log('value:', val)); func ...

Use two fingers to scroll up and down on the screen

I am currently developing a sketch web application (using angular) that utilizes one finger gestures for drawing. My goal is to enable vertical scrolling in the sketch content by using two fingers. However, when attempting to scroll with two fingers, Safa ...

Verifying internet connectivity and updating content using jQuery and JavaScript

Upon loading the page, the following functionality occurs without triggering a click event: The updated code attempts to determine if the internet connection is active. If the connection is off, the 'link' on the page will be disabled (non-click ...

Steps for accessing the "this" keyword within the parent function

Struggling with referencing `this` within a parent function while building a basic tab system using AngularJS. It seems like I may not have a solid grasp on the fundamentals, so any guidance would be appreciated: JavaScript: $scope.tabs = { _this: th ...

Having trouble getting the Angular 2 quickstart demo to function properly?

Just starting out with Angular 2, I decided to kick things off by downloading the Quickstart project from the official website. However, upon running it, I encountered the following error in the console: GET http://localhost:3000/node_modules/@angular/ ...

Tips for sorting through JSON Data to isolate a particular day

I developed a Food-App that displays a different menu every day. I retrieve the local JSON Data using Axios and attempt to filter the mapped menu with .filter. My issue lies in not being able to filter specific days. I attempted to restructure the JSON Da ...

Modify the button text when it is hovered over

I am attempting to modify the text displayed on a button when hovering over it in a React component from Ant Design. However, I have not been successful so far. Button <div className={ status == "verified" ? `${styles.btn1} ${styles.btn1C ...

Discover the proper technique to display an error message in cases where no data is detected during the filtering process

I'm currently working on a component that involves search filtering and dropdown filtering. The filtering functionality is working fine, but I'm struggling to determine where to display an error message if no data is found during the filtering pr ...

Formatting JSON Date Output in a Unique Style

I am sending an api request and I would like to present the date in a similar format to what can be seen at this link: Here is the json data I am receiving: dates: { start: { localDate: "2017-04-06", localTime: "19:31 ...

Identifying the conclusion of a folder being dropped in vanilla JavaScript

I am working on determining when a directory tree has been completely traversed after being dropped onto a page. My goal is to identify the point at which the next process can begin once the entire folder and its sub-directories have been processed by the ...

Express JS encountering Firebase Google Sign-In glitch

Whenever a user clicks a button on the HTML page, this function is triggered. The function resides in auth.js and it is called from the server.js page. auth.js const firebase = require("firebase"); static googleSignIn(req,res){ firebase.aut ...

Having trouble with CSS values not being applied to dynamically injected HTML div elements in Angular 4?

Link to Codepen My Angular calendar application runs smoothly without any errors. However, I am encountering an issue where the CSS styles are not being applied to the page. When I implemented this separately, everything worked fine. But as soon as I inc ...

Verifying Kentico Cloud webhook signatures using Express.js

Is there a way to verify the signature of webhooks using Express.js? I've looked through the documentation on notification signatures, but I'm unsure how to integrate it with Express.js. This question was originally posted on the official Ken ...