Improving efficiency in THREE.js through approximately 7500 lines of optimization

I'm facing an optimization issue in my code.

Here's a snippet of what I have:

/////////// PARTICLE (var system[] about 5500 Particles with x, y, x inside) //////
            var particle_system_geometry = new THREE.Geometry();
                for (var i=0; i < system.length; i++) {
                    var x = system[i][0];
                    var y = system[i][1];
                    var z = system[i][2];
                    var sec =  system[i][3] * 1;
                    if (sec.toFixed(2) <= 0) {
                        sec = 0;
                    }
                    particle_system_geometry.vertices.push(new THREE.Vector3(x, y, z));
                    colors[ i ] = new THREE.Color( 0xffffff );
                    colors[ i ].setHSL( ( sec * 0.5 ), 1, 0.5 );    
                }
            particle_system_geometry.colors = colors;           

            var particle_system_material = new THREE.ParticleSystemMaterial( { size: 5, map: sprite, vertexColors: true, transparent: true } );
            particle_system_material.color.setHSL( 1.0, 0.2, 0.7 );

            var particleSystem = new THREE.ParticleSystem(
              particle_system_geometry,
                particle_system_material
            );

            particleSystem.sortParticles = true;
            scene.add(particleSystem);

This section runs smoothly at 30 fps without any issues.

The problem arises when I introduce the following code to connect particles:

/////////// LINE (var jump[] About 7500 Line with x1, y1, z1 and x2, y2, z2 inside) //////
            var testline;
            lines = new THREE.Object3D();
                for (var i=0; i < jump.length; i++) {
                    testline = new THREE.Geometry();
                    var x1 = jump[i][0];
                    var y1 = jump[i][1];
                    var z1 = jump[i][2];
                    var x2 = jump[i][3];
                    var y2 = jump[i][4];
                    var z2 = jump[i][5];
                    testline.vertices = [new THREE.Vector3(x1, y1, z1),new THREE.Vector3(x2, y2, z2)];  
                    colorsjump[ i ] = new THREE.Color( 0xffffff );
                    colorsjump[ i ].setHSL( ( jump[i][6] * 1 ), 1, 0.5 );
                    
                    var line = new THREE.Line(testline, new THREE.LineBasicMaterial({
                    transparent: true, opacity: 0.3, color: colorsjump[ i ] }));
                    lines.add(line);
                }
            scene.add(lines);

Unfortunately, this piece of code significantly reduces my FPS to 7-8...

Does anyone know how I can optimize the "line" code to improve performance and FPS?

Thank you!

Answer №1

This is working perfectly

Credit goes to WestLangley for the assistance

/////////// CODE SNIPPET (Approximately 7500 lines involving x1, y1, z1, and x2, y2, z2) //////
                var testline;
                testline = new THREE.Geometry();
                    for (var i=0; i < jump.length; i++) {
                        // Defining the start and end points of the line //
                        var x1 = jump[i][0];
                        var y1 = jump[i][1];
                        var z1 = jump[i][2];
                        var x2 = jump[i][3];
                        var y2 = jump[i][4];
                        var z2 = jump[i][5];
                        
                        // Adding coordinates to vertices //
                        testline.vertices.push(new THREE.Vector3(x1, y1, z1),new THREE.Vector3(x2, y2, z2));    

                        // Implementing colors //
                        colorsjump[ i ] = new THREE.Color( 0xffffff );
                        colorsjump[ i ].setHSL( ( jump[i][6] * 1 ), 1, 0.5 );   
                        colorsjump2[ i ] = new THREE.Color( 0xffffff );
                        colorsjump2[ i ].setHSL( ( jump[i][6] * 1 ), 1, 0.5 );
                        
                        // Storing final colors in an array //
                        lastColor.push(colorsjump[ i ], colorsjump2[ i ]);
                    }

                    // Setting the colors for geometry //
                    testline.colors = lastColor;

                    // Creating a material for the testline //
                    var lineMaterial = new THREE.LineBasicMaterial( {
                        color: 0xffffff,
                        vertexColors: THREE.VertexColors,
                        opacity: 0.2,
                        transparent: true
                    } );

                    // Generating the line //
                    var line = new THREE.Line(testline, lineMaterial,  THREE.LinePieces  );
                    
                    // Adding the line to the scene //
                    scene.add(line);

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

The Isomorphic-style-loader is throwing an error: "Cannot read property 'apply' of null"

Despite encountering the same error and exploring various solutions, I have yet to resolve my issue, potentially due to my limited understanding of the React structure. It seems that the error is stemming from context.insertCss.apply(context, styles) not ...

Exploring the Scope of LocalStorage in Javascript

I've recently begun working with local storage and encountered some scope issues. Whenever I try to console.log(test), the second if statement returns undefined. What am I missing here? This is my current code; $("document").ready(function(){ ...

React component fails to re-render after state change

For the past two days, I've been struggling with this error and can't seem to fix it! I'm currently working on creating a weather app in React which utilizes the API. The app features a Bootstrap Navbar with a search option that allows user ...

React Select Event Bug: Not firing on subsequent attempts

My select filter menu has 5 different options that work fine when the page loads or refreshes. However, when I try to continuously select an option, it does not filter the content as expected. Here is a snippet of the code: <div className="filter_ ...

What causes the timer to pause, and what steps can be taken to avoid it? (Using Javascript with Iframe)

On my website, I have a page where clients must view an advertisement for 20 seconds. The website is displayed in an iframe with a countdown timer above it. I've set it up so that the timer stops when the window loses focus to ensure the client is ac ...

Is it advisable to standardize or de-standardize tree-structured data while creating the ngrx state?

In the process of developing a todo application, I have encountered a challenge regarding the structure of my model. Initially, before implementing ngrx, my Todo model included properties for sub Todos like this: export interface Todo { id: string; ti ...

Using an In-Memory Isolated Neo4J Database to Test NodeJS Applications

Can anyone suggest a method to quickly create small in-memory Neo4J database instances for testing NodeJS code with Jest? ...

Incrementally including DIV elements...one by one...2, 4, 6, 8, 10, 12

I am in the process of creating animated div boxes step by step that are activated when a button is pressed. The current code I have is working smoothly, with the box moving right and then left back to its original position. You can see a demo of this here ...

The prop type 'lg' supplied to 'ForwardRef(Grid)' is not valid and has failed

This particular code snippet is responsible for managing the layout of components on the webpage. However, I have encountered some warning messages in the console: Warning: Failed prop type: The lg prop provided to ForwardRef(Grid) is invalid, it should ...

I'm seeing a message in the console that says "Form submission canceled because the form is not connected." Any idea why this is happening?

For the life of me, I can't figure out why this code refuses to run the handleSubmit function. Essentially, the form is supposed to take an input and execute the handleSubmit function upon submission. This function then makes a POST request to an API ...

The argument labeled as 'State' cannot be assigned to a parameter labeled as 'never'

I've recently delved into using TypeScript with React. I attempted to incorporate it with the React useReducer hook, but I hit a roadblock due to an unusual error. Below is my code snippet: export interface ContractObj { company: string; ...

Creating a reusable function in AngularJS using the factory method

Currently, I am using the AngularJS factory method to retrieve data and passing that value to controllers. In order to avoid creating separate functions, I would like to create a common function that can be utilized in all controllers. How can I effi ...

Do not try to render objects as a React child. If you intended to display a group of children, make sure to use an array instead

I am encountering difficulty in accessing the required data from an array, and consistently receiving this error message: Objects are not permissible as a React child (found: object with keys {id, name, country, logo, flag, season, standings}). If you ...

I am looking to transition a Diamond shader from WebGL to Three.JS

Currently, I am in the process of developing an online store specializing in exquisite diamond jewelry. The primary focus is to showcase these diamonds in a visually appealing manner; however, I am encountering challenges with incorporating reflections and ...

Modifying an object's attribute in React.js by toggling a checkbox

As I delve into learning React, I am constructing a straightforward todo list. Here's the object contained within my initialState: getInitialState:function(){ return { items: [ { text:"Buy Fish", ...

Unveil the socket connection using web sockets

My current setup involves a server generating a socket on port 8181. I am interested in accessing this socket from a web page viewed in Google Chrome version 14. It seems that direct access may not be feasible, as Chrome only supports Web Sockets and not s ...

Struggling with conditionally rendering components or elements in React while managing state

I am currently working on a react login/signup form where the user lands on the signup section by default. My goal is to display the login section when the user clicks on the login button and show the products section when the user clicks on "Get Started" ...

Tips for shrinking a circle size

Is there a way to decrease the size of both circles? I have tried adjusting the values in the source code, but haven't been successful. The circles are generated using canvas. Here is the HTML: <div style='position: absolute: top: 0px; left: ...

Is there a way to lock an element's position on a webpage once it reaches a specific point?

Excuse me if this query seems trivial. I am currently in the process of designing a webpage with a header that has two distinct sections (upper and lower). My aim is for the lower portion of the header to remain fixed on the page as I scroll down. Is the ...

The jQuery function throws an Error that is not caught by the surrounding try / catch block

Recently, I've been enhancing error handling in my JavaScript objects that heavily utilize jQuery. However, I've run into an issue where throwing a new Error from within a jQuery method is not caught by the surrounding catch statement. Instead, i ...