How can I delete the visuals and objects from my mind in Three.js?

I need to place a specific number of circles on the scene.

var radius = 1; 
var segments = 32; 
var circleGeometry = new THREE.CircleGeometry( radius, segments); 
function generateCircles(){
  //scene.remove(circle);
  var count=0;
  while (1000> count) {
   circle = new THREE.Mesh (circleGeometry, material);
   scene.add (circle);
   count ++;
  }
}

Is this the most efficient way to achieve my goal?

In my code, I invoke this function. However, every time it is called, the performance becomes slower, possibly due to increased objects in the scene. What steps can I take to address this issue?

Whenever the function is called, I want all previously generated circles to be completely removed from memory before adding new ones. This will help improve application speed, as running the function multiple times results in an accumulation of circles that slows down execution.

By "slower", I mean that I am aiming for faster application performance. As more circles are added with each function call, I want them to be cleared sooner to make room for new ones. The presence of numerous circles in the scene hinders smooth execution.

http://jsfiddle.net/v8oxsxtc/

Answer №1

To get rid of the existing circles in the scene, you can utilize the scene.remove method for each circle that was previously added. Below is a straightforward example based on your provided code:

let lastCircles = null;
function createNewCircles(){
  let count=0;
  if(lastCircles) { // remove old circles if they exist
    lastCircles.forEach(function(circle) {
      scene.remove(circle);
    });
  }
  lastCircles = []; // clear cache
  while (1000 > count) {
    let newCircle = new THREE.Mesh(circleGeometry, material);
    lastCircles.push(newCircle); // store each circle in cache
    scene.add(newCircle);
    newCircle.position.set(count,count,count)
    count ++;
  }
}

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

What is the reason for the result of 0x80000000 & 0x80000000 being lower than 0?

What is the reason for this inconsistency in nodejs? 0x80000000 & 0x80000000 < 0 while 0x40000000 & 0x40000000 > 0 Also, if I were to use a larger hexadecimal number like 0x800000000, could it potentially introduce hidden bugs? POSTAG.t ...

Having trouble converting JSON into a JavaScript object

I am encountering an issue with my HTML box: <span>Select department</span><span> <select id="department" onchange="EnableSlaveSelectBox(this)" data-slaveelaments='{"a": 1, "b": "2& ...

Autofill Dropdown in AngularJS Using Data from Previous Page

Despite searching extensively on StackOverFlow, I was unable to find the answer I needed. So, I am posting my question below. I have a form that includes a dropdown menu. When a user clicks a button, they are taken to a new HTML page with additional infor ...

The Field Projection operation is not taking into account the batchSize field name

I have a collection called user_batch which contains the following two documents: [{ _id: ObjectId("594baf96256597ec035df23c"), name: "Batch 1", batchSize: 30, users:[] }, { _id: ObjectId("594baf96256597ec035df234"), name: "Batch 2", batch ...

Utilizing Jquery for ASP.NET, an AJAX call dynamically populates a list

My user interface is designed to populate a select dropdown menu using data retrieved from a database through an AJAX call. The C# web method responsible for this operation is structured as follows: private static List<List<string>> componentT ...

Tips for mocking a particular http resource (URL) solely in Angular

I'm part of a team with front-end and back-end developers. At times, the front-end team needs to make REST requests to an http address or REST-resource that hasn't been implemented yet. Within ngMockE2E, I've come across the $httpBackend se ...

Firestore generates an error stating that 'onSnapshot() must have 1 to 4 arguments'

I am having trouble retrieving all unread messages for the current user from Firebase. The issue arises when using onSnapshot() as it initially fetches the required data but fails to do so when a new document is added, resulting in an error. FirebaseErro ...

Is it possible to pass a random variable into an included template in Jade?

In my jade template called 'main page', I have a reusable template named 'product template'. The 'product template' is designed to display dynamic data and needs to be flexible enough to be used in multiple pages without being ...

Chrome extensions using Cross-Origin XMLHttpRequest

Chrome extensions API states that cross-origin calls using the XMLHttpRequest object should be allowed with proper permissions: An extension can communicate with remote servers beyond its origin by requesting cross-origin permissions. Following the Goog ...

Exploring the Intersection of Windows 8 Store Applications and jQuery: Leveraging MSApp.execUnsafeLocalFunction

Developing a Windows 8 JavaScript Store App (using Cordova) has led to some complications when using jQuery. It seems that in order to utilize certain functions, I have had to modify the jQuery library by adding: MSApp.execUnsafeLocalFunction While this ...

"Utilize a specific parameter in the npm run script for enhanced

Is it possible to pass a named parameter to an npm run script in order to achieve the following functionality? "scripts":{ "say-hello":"echo $greeting && ls" } npm run hello --greeting=hello The desired outcome is to replace the $greeting ...

Creating 3D audio effects in HTML5

Let's dive into the challenge... Currently, I'm working on building a virtual environment with WebGL (THREE.js) where various objects produce distinct sounds. My goal is to create a surround sound experience based on the position of these object ...

Conceal certain digits of a credit card number in a masked format for security purposes

Is there a reliable method to mask Credit Card numbers in password format within a text field? **** **** **** 1234 If you are aware of a definitive solution, please share it with us. ...

Challenges with ColdFusion's floating point calculations

I am encountering an issue with my program where it is not displaying two decimal points properly. For example, when I enter 140.00, it shows as 140.0. Strangely, if I enter 140.15, it displays correctly as 140.15. It seems to consistently drop the zero. B ...

A feature to reveal additional content when it extends beyond a single line

<div class="questionnaire-description col-xs-12" id="questionnaire_description_wrapper"> <text-truncate> <span ng-class="questionnaire_description.show_more ? 'full-description' : 'ph-ellips ...

The AddClass function fails to function properly after an ajax form is submitted

I am facing a challenge in setting up validation for my ajax form. My goal is to have a red border appear around the input field if it is submitted empty. Unfortunately, when I try to use addClass(), it does not seem to have any effect. The alert message ...

Error: The function sendFile does not exist in the method res.status(...)

As a newcomer to this field, I must apologize if my issue has an obvious solution. However, I am encountering the following error: TypeError: res.status(...).sendFile is not a function at app.get (/root/discordtest/server.js:10:19) at callbacks (/ ...

Using Jquery to target an element within the same DOM element and apply changes

My website platform doesn't assign any IDs or classes to the menus it generates, and uses nested lists for submenus. To make the submenus expand upon clicking, I created a jQuery script: $(function () { $(".wrapper ul li").click(function () { ...

Discover the simple steps to include row numbers or serial numbers in an angular2 datagrid

Currently, I am utilizing angular2 -datatable. Unfortunately, I am facing an issue where the correct row numbers are not being displayed in their corresponding rows. Whenever a user moves to the next page using the paginator, the datatable starts countin ...

Despite the presence of a producer and topic, sending Kafka messages is proving to be a challenge

Currently, I am using TypeScript and the KafkaJS library on my local machine with a single Kafka broker. After successfully connecting a producer, confirming the creation of my topic, and creating messages like so: const changeMessage = { key: id, ...