Is there a way to evenly space out sprites on a spherical surface in THREE.js?

I'm working on a fascinating project where I want to create a visually appealing database of words. The idea is to arrange the words in a sphere, with the most important ones closer to the top and less important ones further away. To achieve this, I meticulously placed text sprites at varying distances from the top of the sphere based on their importance.

https://i.sstatic.net/f8NLF.png For those who prefer visuals, here's a video version:

Although my initial plan seemed solid, the spherical geometry caused the words to spread out more as they moved away from the top. My goal is to have a more balanced distribution across the surface of the sphere. It doesn't have to be perfect, just visually more consistent than it currently is.

Any suggestions on how I can achieve this desired effect?

Below are the methods I've been using:

positionDb(db) {
    console.log("mostRelated", db.mostRelated);
    console.log("depthList", this.depthList);
    let mostRelated = db.mostRelated;
    let depthList = this.depthList;
    for (let i = 0; i < mostRelated.length; i++) {
      this.addTextNode(mostRelated[i].data, this.depthList[i].vertice, this.depthList[i].depth);
    }

}
addTextNode(text, vert, distance) {
    let fontSize = 0.5 * (600 / distance);
    let sprite = new THREE.TextSprite({
      fillStyle: '#000000',
      fontFamily: '"Arial", san-serif',
      fontSize: fontSize,
      fontWeight: 'bold',
      text: text
    });
    this.scene.add(sprite);
    sprite.position.set(vert.x, vert.y, vert.z);
    setTimeout(() => {
        sprite.fontFamily = '"Roboto", san-serif';
    }, 1000)
}


this.scene = scene;
this.geometry = new THREE.SphereGeometry(420, 50, 550);
var material = new THREE.MeshBasicMaterial({
    color: 0x0011ff
});
var sphere = new THREE.Mesh(this.geometry, wireframe);
var wireframe = new THREE.WireframeGeometry(this.geometry);
let frontVert = {
    x: 0,
    y: 100,
    z: 0
}
let depthList = [];
this.geometry.vertices.forEach(vertice => {
  let depth = getDistance(frontVert, vertice);
  if (depthList.length === 0) {
    depthList.push({
      depth,
      vertice
    });
  } else {
    let flag = false;
    for (let i = 0; i < depthList.length; i++) {
      let item = depthList[i];
      if (depth < item.depth) {
        flag = true;
        depthList.splice(i, 0, {
          depth,
          vertice
        });
        break;
      }
    }
    if (!flag) depthList.push({
      depth,
      vertice
    });
  }
});

Answer №1

Consider using a fibonacci sphere

function fibonacciSphere(numPoints, point) {
  const rnd = 1;
  const offset = 2 / numPoints;
  const increment = Math.PI * (3 - Math.sqrt(5));

  const y = ((point * offset) - 1) + (offset / 2);
  const r = Math.sqrt(1 - Math.pow(y, 2));

  const phi = (point + rnd) % numPoints * increment;

  const x = Math.cos(phi) * r;
  const z = Math.sin(phi) * r;

  return new THREE.Vector3(x, y, z);
}

Here is an example showcasing the implementation:

 // Javascript code here
 
 /* CSS code here */
 
 <!-- HTML code here -->
 

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

Utilize a for loop to reference variable names with numbers

Is there a way to extract values from req.body.answerX without manually coding each one using a for loop? I currently have values stored as "answer1, answer2" and so on. This is what I tried: for( var i = 1; i <= 10; i++){ console.log(req. ...

In PHP, you can use the `echo` statement to output an HTML input

Incorporating HTML into PHP using heredoc methodology can sometimes lead to challenges when trying to retrieve user input variables. Attempting to access the input variable with $_GET["input"] may result in an error message indicating an undefined index: ...

Using Javascript, dynamically animate the text in the hero unit with fading in and out effects

I currently have a "Hero" unit with the following code: <div class="hero-unit"> <div class="container"> <h1>Dapibus Euismod Mollis</h1> <p>Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis ...

JavaScript function returning code

I am trying to create a JavaScript object with multiple functions, but I keep encountering an exception undefined is not a function Here is my JS code: var Cars = null; $(function () { Cars = function() { return { ...

Transform the X,Y coordinates on the screen into perspective projection at a defined z coordinate

Currently, I am immersed in the world of ThreeJS, but this question pertains to mathematics as a whole. My ultimate objective is to accurately position an object within my scene using 2D screen space coordinates while simultaneously specifying a desired z ...

What process is involved in implementing TCO (Tail Call Optimization) for a recursive anonymous function in ES5?

Is there a way to optimize an anonymous recursive function for tail call optimization, similar to how a named recursive function can be optimized? If such a method exists, please provide explanations on how to achieve it. Below is an example of a recursi ...

Update current properties of objects

I'm feeling like I'm going crazy and could really use some assistance. My predicament involves a function that looks like this: private generateTimeObject(firstObject: someInterface, secondObject?: someInterface) { let firstTime; let secondTi ...

Displaying the Status of a Script that is Running Asynchronously

My script takes around 5 minutes to complete and sends an email with a file attachment once finished. The entire process happens on a single PHP page without using AJAX. I want the front end to handle form submission seamlessly, processing the request in ...

Search for data in MongoDB based on the results obtained from the initial query

I am facing an issue with a mongoose query: let rE = await cR.find({myid: "xxxxxx"}); This query returns multiple results, and then I need to query another model based on the rE.class_id value. So I try to do this: let cla = await Cl.find({_id: ...

Halt and anticipate a boolean reply from another function

Is there a way to create two functions in JavaScript, run one of them, then within that function, execute the other and pause until it receives a response (yes or no) before moving on to an if statement? The user's response should be manual. Here is ...

react.js function that returns 'undefined'

Having trouble with my class function that returns undefined when I try to use the return value. Main.js: let db = new Database() let username = db.get() return( //returns undefined <p>{username}</p> ) database.js: class Database { [... ...

Issue with memory leak in Three.js r89 when using STL models

I'm struggling with a webpage I'm constructing. It seems like a simple task, but I keep encountering issues that I suspect may be caused by a memory leak. Despite spending most of the day researching for a solution, I can't seem to find one ...

What is the best way to execute two JavaScript functions within an inline onclick event?

I am currently working on the following code snippet: <script> function delay (URL) { setTimeout( function() { window.location = URL }, 2000); }</script> And then I have the following within the <body> element: <img src="small_rabbi ...

Unique Rails Magnific Pop-Up Featuring Edit Buttons for Individual Table Rows

Within my table of records, each row features an edit button that triggers a pop-up form utilizing the magnific pop-up gem. The goal is for clicking on the edit button to display a pop-up form containing the specific information of the record clicked. Howe ...

Redux's 'connect' function fails to recognize changes in the state array

I've recently implemented redux with a reducer that handles an array of time slots for a specific date. Whenever the date is changed, the reducer successfully updates the state (confirmed through console logs in my mapStateToProps function). However, ...

Smooth Laplacian causing faces to disconnect

I've been working on implementing Laplacian smoothing using JavaScript and Three.js, but I'm encountering some unexpected issues. When I hover and smooth over the meshes, instead of achieving a smooth effect, the faces are becoming disconnected a ...

Function to reset array not working properly and failing to remove items from alternate array

For this word game, I generate 9 letters randomly and wait for users to input their word. Then, I check if each letter in the user's input is included in the original set of generated letters. For example, if the word generated by startwordgame() is ...

Using Angular Ionic for a click event that is triggered by a specific class

I am utilizing Highcharts and would like to click on the legend upon loading. With the use of Angular Ionic, how can I trigger a click on the .highcharts-legend-item class within the ngOnInit() {} method? I am aiming to click on this class as soon as the ...

Issue with error handling in Node and MongoDB when using Express, Mongoose, and the 'mongoose-unique-validator' plugin

I am facing an issue with the 'mongoose-unique-validator' plugin when trying to handle Mongo ValidationError in my custom error handler. Despite other errors being handled correctly, this specific one is not triggering the desired response from m ...

Converting and saving geometry in three.js using the toJSON method and BufferGeometryLoader for serialization and deserialization. Transmitting geometries as text

Let me start by saying that I am new to three.js and would like to share my learning journey. My objective was to convert geometry into a format that can be transferred to another web worker (as not all objects can be transferred between workers, only s ...