Multiple scenarios featuring multiple meshes from a single array

For my project, I am working with an array that contains 100 JSON objects, each of which corresponds to a mesh. The goal is to add all 100 meshes to the scene, but this is causing a significant strain on performance. To address this issue, I am exploring the possibility of dividing the array into 10 parts, with each part containing 10 "Planet" objects that will be added to separate scenes. In total, there will be 10 scenes, each with 10 meshes.

As a novice in three.js, I am unsure if this approach is feasible. How should I proceed? Below is the code snippet I have been working on:

 // Scene initialization
 var scenes = [];
 for (var i = 0; i < 10; i++) {
    scenes.push(new THREE.Scene());
 }

// Data for the planets
var data = [
    { "Planet": "1" },
    { "Planet": "2" },
    { "Planet": "3" },
    // Continues up to planet 100
];

// Adding 100 meshes to the scene
data.forEach(function(item, index) {
    var loader = new THREE.TextureLoader();
    var material = new THREE.MeshPhongMaterial({ map: loader.load('image.jpg') });
    var geometry = new THREE.SphereGeometry(30, 50, 50);
    var mesh = new THREE.Mesh(geometry, material);

    // Randomize positions
    mesh.position.x = THREE.Math.randInt(-500, 500);
    mesh.position.z = THREE.Math.randInt(-500, 500);

    // Distributing meshes across scenes
    scenes[Math.floor(index / 10)].add(mesh);
});

Edit: To clarify, the objective is to create 10 scenes, each displaying 10 "planets" based on the data array. This way, users can switch between scenes and view a manageable number of planets at a time, rather than rendering all 100 in a single scene. Here's a simple example of how the switching logic can work:

var userInput = 1;
if (userInput === 1) {
    renderer.render(scenes[0], camera); // Render first scene
} else {
    renderer.render(scenes[userInput - 1], camera); // Render specified scene
}

Answer №1

If you're looking to address your specific query, one approach could be to firstly store the scenes in an array:

// Defining scenes
var scenesArray = [];
for(var j=0; j<10; j++) {
    scenesArray.push(new THREE.Scene());
}

Subsequently, you can allocate the meshes to a particular scene based on a counter:

// Add 100 meshes to each scene
for (var k = 0; k < data.length; k++) {
    ...
    var index = Math.floor(k/10);
    var currentScene = scenesArray[index];
    currentScene.add(mesh);
}

However, without further context or additional code snippets, it's difficult to gauge the ultimate objective in terms of performance enhancement. If all 10 scenes are still being rendered simultaneously, the speed won't differ significantly from using just one scene.

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

Centered iframe within a div

I am trying to center my iframe within a white box and need some assistance with this. I want the iframe to be in the middle of the white box rather than its current position at the moment. game1.html: <html> <head> <title>R ...

restructure array upon alteration of data

Here's the current structure of an array stored in a $scope variable: $scope.objects: [ {selected: true, description: 'string1'}, {selected: false, description: 'string2'}, {selected: true, description: 'string3'}, ...

How to use Angular ng-repeat to filter an array by a specific key

Here is the data I am working with: $scope.allNames = [ "A": [ { "name": "a1" }, { "name": "a2" }, { "name": "a3"} ], "B": [ { "name": "b1" }, { "name": "b2" }, { "name": "b3"} ], "C": [ { "name": "c1" }, { "name": "c2" }, { "name": "c3"} ], ...

Can I use my local network/browser to download an html file from a webpage as if I manually downloaded it using javascript or nodejs?

As someone who is relatively new to javascript/nodejs and its packages, I have a question about downloading files. Is it feasible for me to download a file using my own local browser or network? Typically, when researching how to scrape or download html ...

Top method for preventing an HTTP 403 error when receiving the Set-Cookie header in order to establish the CSRF Cookie

As I interact with a REST API that includes CSRF protection measures, I am facing a common hurdle. Successfully obtaining the token and sending it back to the server seems to work smoothly. However, encountering an HTTP 403 error arises when initiating t ...

Access the data from a JSON object within a v-select menu

<v-form :model='management'> <v-flex xs3 sm2 md3> <div class="form-group> <v-select :items="days" item-text='text' item-value='text' placeholder="MM" single-line attach v- ...

Adonis.js Creating a RESTFUL API Solution

I recently embarked on a new project using the adonisjs framework. I had the option to utilize expressjs, but I was drawn to adonisjs due to its structured nature, especially reminiscent of Laravel. My current challenge revolves around building a RESTful ...

Encountered a 404 error while trying to delete using VueJS, expressJS, and axios. Request failed with

Currently, I'm in the process of learning the fundamentals of creating a MEVN stack application and facing a challenge with the axios DELETE request method. The issue arises when attempting to make a DELETE request using axios, resulting in a Request ...

Guide to utilizing JSDoc within your local project

Objective My goal is to utilize jsdocs with npm in my project. Experience I am new to working with npm and its plugins. Recently, I came across jsdoc and attempted to incorporate it into my project without success. Attempted Solution Initially, I inst ...

Ways to trigger rendering when the global variable value is updated?

#Code Block 1: let ticketEnabled = false; export default class SupportTicketMain extends Component { constructor () { super(); } render () { let expIcon = <DownIcon/>; if (this.state.ticketDetailExpand) { expIcon = <UpIcon ...

What are the potential drawbacks of combining the useState hook with Context API in React.js?

Within my code, I establish a context and a provider in the following manner. Utilizing useState() within the provider enables me to manage state while also implementing functions passed as an object to easily destructure elements needed in child component ...

Error: The function exec in matchExpr[type] is not defined

I made some changes to Object.prototype and now I'm running into errors with jQuery's methods on selectors. The error message I'm getting is: Uncaught TypeError: matchExpr[type].exec is not a function Additionally, when trying to use $.po ...

The request successfully functions on Postman, but when using FETCH it is inexplicably returned as 'undefined'

Encountering issues when trying to send data to the backend. Currently, the frontend contains the following fetch function: function appending() { console.log('in Appending'); console.log('formInfo', formInfo); c ...

Encountering a server issue while incorporating ejs in expressjs

I am a beginner with nodejs and I am experimenting with using ejs to template my website. However, every time I try to set the view engine, I encounter the error 500 - Internal Server Error. Here is my app.js: var express = require('express'); v ...

Reactjs is displaying an AxiosError stating a Network Error

Working on a project with Reactjs and Nextjs, I encountered an issue where the data being posted with axios is inserting "NULL" values. I'm not sure where I am going wrong. Below is the code snippet in Nextjs: const userData = { name: state.name, ...

Using JavaScript, extract the most recent 10 minutes worth of UNIX timestamps from a lengthy array

I am working with an array that contains multiple UNIX timestamps and I need to retrieve the timestamps from the last 10 minutes. Specifically, I only require the count of these timestamps, possibly for a Bot protection system. Would it be best to use ...

Nested views within Angular providing a multitude of perspectives

Currently, I am tackling a project at my workplace with my colleagues, but we are collectively stumped on how to proceed. Consequently, the details may come across as nebulous. Allow me to present a preview of the expected layout: Template The plan is fo ...

What is the best way to link this interval service with a view component?

Exploring the AngularJS documentation for $interval, I came across an interesting example demonstrating how to utilize $interval in a controller to create a user-friendly timer in a view. The official example code can be found on the angularJS documentatio ...

Having trouble toggling journal entries in an HTML journal? The Jquery function might not be working properly

I have been tasked with creating a civil war journal for my 8th grade Social Studies class and I decided to present it as an HTML file featuring the title and date of each journal entry. The goal is to allow users to click on each entry to open it while au ...

The script remains static and does not alter the "href" or "div" content based on the current URL

I am having an issue with my NavMenu on my website. It is a table with images that link to product pages, and I want to change the links and names based on the language of the site. However, the script I wrote for this is not working properly. I have tried ...