Using Three.js to display multiple objects simultaneously causes my browser to crash

When it comes to rendering objects in a scene, I encountered an issue with loading multiple objects. Initially, loading all 3 objects as STL files worked fine. However, when I attempted to divide each object into multiple surfaces and create BufferGeometry for each surface, I ran into problems. Each object contains anywhere from 1000 to 5000 surfaces, and my goal was to simplify surface selection for highlighting purposes.

The code snippet below outlines my approach:

function renderSurfaces(data, checkbox) {
var group = new THREE.Group();
var vertices = data.vertices;
var surfaces = data.surfaces;
var triangles = data.triangles;
//Generate all surface and add each one to the group    
var surfacesKeys = Object.keys(surfaces);
for (var i = 0; i < surfacesKeys.length; i++) {
    var indices = [];
    //Get all triangle ids composing the current surface
    var surfaceTriIds = surfaces[surfacesKeys[i]].surfaceTriIds;
    //Loop over surfaceTriIds
    var geom = new THREE.Geometry();
    for (var j = 0; j < surfaceTriIds.length; j++) {
        //Get vertices Id for one triangle
        var verticeTriangleIds = triangles[surfaceTriIds[j]];
        //Set triangle indices
        indices.push(verticeTriangleIds[0]);
        indices.push(verticeTriangleIds[1]);
        indices.push(verticeTriangleIds[2]);
        //           
    }
    var geometry = new THREE.BufferGeometry();
    geometry.setIndex(indices);
    geometry.addAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
    geometry.computeVertexNormals();

    var obj = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial({
        color: 0xAAAAAA,
        specular: 0x111111,
        shininess: 200
    }));
    group.add(obj);
}
scene.add(group);    
camera.lookAt(group.position);

render();

However, I am facing a challenge with the browser crashing when attempting to load all 3 objects in this manner. It seems to handle rendering two objects fine. I am now wondering if there is an alternative way to render my objects surface by surface.

Answer №1

It seems like your code could use some improvement... From what I can gather, you're creating a new THREE.Geometry(), but not utilizing it anywhere in your code. Later, you switch to using a THREE.BufferGeometry().

However, creating a geometry per face may be excessive. If your goal is to highlight a face, you could try cloning the original geometry and mesh, then changing the material on the cloned mesh to highlight the triangle. You can then use geometry.setDrawRange to only draw the specific triangle you want to highlight:

Are you attempting to implement face picking with the mouse?

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

Issue with timestamp in the Instagram API call to retrieve media using AJAX (GET /media/search)

My API call is returning a 400 bad request error message: {"meta":{"error_type":"APIInvalidParametersError","code":400,"error_message":"invalid parameters-check the max\/min-timestamps, if you supplied them"}} Even though my request appears to be co ...

I am having trouble grasping certain syntax in JavaScript when it comes to using `${method_name}`

I'm having trouble understanding some of the syntax in this code, particularly ${method_name}. I'm not sure what we are achieving by passing the method name within curly braces. global._jsname.prototype.createEELayer = function (ftRule) { if ...

Is there a way to retrieve the title, description, and image URL of a URL through Ajax, similar to how Facebook shares a link?

Currently, I am developing a project that involves allowing users to submit a URL. The system will then extract the title, images, and description from the provided URL and offer the option to toggle between different images. Upon submission, these extrac ...

Is it possible to refresh AdSense banner when the router changes?

Is there a way to reload the AdSense banner ads when the router changes? I've been encountering issues trying to re-add the script and HTML properly. Any guidance on how this should be done would be greatly appreciated... This is just a test for one ...

Exploring the functionality of componentWillReceiveProps within functional components

Embarking on my journey with functional components after extensively working with class components. While experimenting, I encountered a challenge: how can I incorporate the functionality of componentWillReceiveProps within the context of the useEffect h ...

The state variable remains undefined even after integrating useEffect in a React.js component

Hello, I have a component within my React application that looks like this: import React, { useEffect, useState } from "react"; import AsyncSelect from "react-select/async"; import { ColourOption, colourOptions } from "./docs/data"; const App = () => ...

Retention of user-entered data when navigating away from a page in Angular

I need to find a way to keep the data entered in a form so that it remains visible in the fields even if the user navigates away from the page and then comes back. I attempted to use a solution from Stack Overflow, but unfortunately, it did not work as exp ...

Sending values from multiple input fields created in PHP using AJAX can be done by following these steps

https://i.sstatic.net/GKcRc.png Within this snippet, there is a portion of a form that consists of 4 different fields/divs like the one shown. I am struggling to figure out how to send the values of these input fields to PHP using AJAX. I apologize if ...

Error in JSON due to the presence of an unexpected token within the

I am facing a challenge with my PHP code, where I take a list of filenames or empty strings and store them in an array. This array is then converted to JSON and saved in a database successfully. However, the problem arises when this array is also stored wi ...

The value of an AngularJS model object cannot be altered with a dynamic key

app.controller('indexController', ['$scope', '$location', 'authService', function ($scope, $location, authService) { var vm = this; vm.$onInit = function () { vm.active = { "home": true, ...

What are some ways to adjust red and green blocks using CSS?

One question that arises is how to create a version of a webpage where only the yellow block can slide up, while the red and green blocks remain fixed. Currently, the green block is treated with the following CSS: position:sticky; right:0px; top:100px; ...

Navigate to a fresh web page without encountering any script loading issues

I am currently working on a web application that loads new pages without requiring the browser to reload. While some pages load perfectly fine, others are causing errors due to missing scripts. The code snippet below is used to load a new page: function lo ...

The initial value for the `useState` is not defined at the

Here's a simplified version of my requirement that demonstrates the issue. The ColorBox component receives a prop called "isVisible" from the ShowColorComponent component, which is used to initially set the state of the ColorBox.visible variable. impo ...

Obtain JSON information using an underscore template

As someone fairly new to using Backbone and Underscore, and web development in general, I am seeking guidance on how to retrieve individual model data on-click from a template format in order to populate a pop-up modal. Any advice or direction would be gre ...

React: Maximum call stack size exceeded error was caught as an uncaught RangeError

I've been experimenting with React and I've managed to get the functionality I want, but it's running very slow due to an infinite loop lurking somewhere. I suspect the issue lies within the component lifecycle methods, but I'm unsure h ...

React JS implementation of Dropbox logic with multiple steps

Greetings to you and your loved ones. I am in need of your assistance with a minor issue that has been eluding me for the past three days. I have been utilizing a basic React library for the following scenario: Scenario I have set up 4 dropdown menus: I ...

Remove an element from a mapping of type <String,String> in Javascript

Question! How can I remove a specific element from this JavaScript object by its ID? [ "{\"id\":\"b00a3a47-783a-4af5-90d9-59c4deb7a9e3\",\"notes\":\"sdfsdf\",\"recordType\":0}", "{\"id\": ...

What is the best way to retrieve a value from $http or $resource using this filter?

Within my HTML, I have a structure that looks like {{ i.userId | userName }}. Essentially, I am attempting to convert this into a name by utilizing a filter. However, I am encountering numerous challenges with the asynchronous function required to retrieve ...

Listening for changes in class property values in TypeScript with Angular involves using the `ngOnChanges`

Back in the days of AngularJS, we could easily listen for variable changes using $watch, $digest... but with the newer versions like Angular 5 and 6, this feature is no longer available. In the current version of Angular, handling variable changes has bec ...

Listener function triggered following its deletion on IE11

While developing a component that listens for clicks on the entire document, I encountered an issue with IE11. To demonstrate this problem, I created a few simple components. class App extends React.Component { constructor(props) { super(props); ...