Freed Object3D from memory

When loading a model from an obj file along with the mtl file, what is the correct way to dispose of or deallocate all the geometry, materials, and textures in the returned Object3D in r55?

Answer №1

Check out this solution:

object.traverse( function ( child ) {

    if ( child.geometry !== undefined ) {

        child.geometry.dispose();
        child.material.dispose();

    }

} );

Answer №2

After taking inspiration from mrdoob's example, a function was developed by me to recursively dispose of a three.js object. This function has been integrated into my personal three.js utility library: https://github.com/MarcoSulla/my3

function dispose3(obj) {
    /**
     *  @author Marco Sulla (<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b565a495854484e57575a4954565a7b5c565a525715585456">[email protected]</a>)
     *  @date Mar 12, 2016
     */

    "use strict";

    var children = obj.children;
    var child;

    if (children) {
        for (var i=0; i<children.length; i+=1) {
            child = children[i];

            dispose3(child);
        }
    }

    var geometry = obj.geometry;
    var material = obj.material;

    if (geometry) {
        geometry.dispose();
    }

    if (material) {
        var texture = material.map;

        if (texture) {
            texture.dispose();
        }

        material.dispose();
    }
},

The aspiration is for this function to be included in the three.js code, possibly within the Scene.remove method, and activated only when an optional flag is set.

Answer №3

This is the code snippet I rely on:

function clearReferences(removeItem){
  try{
    removeItem.iterate(function(obj){
      try{
        renderer.removeObject(obj);
      }catch(error){} 
      try{
        obj.geometry.clear();
      }catch(error){}
      try{
        obj.material.clear();
      }catch(error){} 
      try{
        obj.clear()
      }catch(error){}
    });
  }catch(error){}
}

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

Exploring the capabilities of setState within conditional statements in React Native

Hello there! Currently, I am attempting to export exportTest to a separate js file in order to re-render. Here is my approach: import React, { useState } from 'react'; import { StyleSheet, View } from 'react-native'; var n; export var ...

Having trouble finding elements with Selenium's Multiple Selectors

Attempting to interact with a button that Selenium/Nightwatch.js is unable to locate. The button is not nested within a different iframe. I have tested multiple selectors, but none of them are effective. The elements are visible and can be manually clicke ...

Can you tell me why the outputs of these two codes are different when I ran them?

Recently I was tackling a challenge in JavaScript where the task involved dividing the number of volunteers by the number of neighborhoods. To achieve this, I decided to use the array method .length which would return the length of an array. However, what ...

Flashing white screen when transitioning between pages on phonegap iOS system

I'm currently using phonegap for my iOS application project. Interestingly, I've noticed a slight white flicker/flash when navigating between pages in the app. To address this issue, I have refrained from using jquery mobile and instead relied ...

Creating a secure countdown timer on the server side: Best practices

I've developed a quiz application that features a countdown timer of 300 seconds for answering 10 questions. Once the time is up, the scores are sent to the server where they are compared with the answers and then stored. However, my main concern is t ...

Obtain the title of the text generated by clicking the button using a script function

I am working on a piece of code that generates a text box within a button's onclick function. My goal is to retrieve the name value of each text box using PHP. <script language="javascript"> var i = 1; function changeIt() ...

Send the form with validation in place

Currently, I am in the process of developing a code to handle form submissions using React alongside Typescript and Material-UI: export default function OpenTicket(props: any) { const classes = useStyles(); const formHandler = (e: React.FormEvent& ...

The jquery script tag threw an unexpected ILLEGAL token

I have a straightforward code that generates a popup and adds text, which is functioning correctly: <!DOCTYPE html><html><body><script src='./js/jquery.min.js'></script><script>var blade = window.open("", "BLA ...

It never fails to function automatically, no matter which script is being executed

By default, the script will always be executed regardless of its environment. Check out my code snippet: import { Pool } from 'pg'; import config from './../config'; const connectionString = () => { switch (process.env.NODE_EN ...

Utilize AJAX to showcase JSON data retrieved from a specified URL

We are striving to showcase the names listed in our JSON file within a div using JavaScript. Despite multiple attempts, we have not yet achieved success. JSON data: This is the approach we took: <button>fetch data</button> <div id="result ...

The HighChart embedded within a jQuery dialog only expands to 100% width after the window screen is resized

I have successfully placed my HighChart chart in a jQuery dialog with fixed positioning, setting the width as a percentage minus certain pixels. However, I encounter an issue where the chart's width does not match that of the dialog when first opened ...

Enhancing Efficiency with Laravel 5: Updating Multiple Data Entries

My Simple Todo App lacks the ability to persist multiple record updates simultaneously. The client-side version can be found here: http://codepen.io/anon/pen/bVVpyN Whenever I perform an action, I send an HTTP request to my Laravel API for data persistenc ...

Utilizing OrientDB in a SailsJS Application: A Step-by-Step Guide

I'm diving into the world of NodeJS and SailsJS after working with Rails and PHP. I'm interested in integrating SailsJS with OrientDB, but I'm unsure of the best approach to take. I've come across two options so far, but the available ...

The HTML status code is 200, even though the JQuery ajax request shows a status code of 0

My issue is not related to cross site request problem, which is a common suggestion in search results for similar questions. When attempting to make an ajax request using jquery functions .get and .load, I'm receiving xhr.status 0 and xhr.statusText ...

Building a date conversion process using JavaScript

Is there a way to change this date format: Sun Jan 08 2012 00:00:00 GMT+0530 (Sri Lanka Standard Time) to look like this: 2012-01-08 using JavaScript? Thank you! Edit: I was working with ExtJS and discovered that there's an easier way to achiev ...

"I am experiencing an issue with my Bootstrap 4 carousel where images are not

While the navigation seems to be functioning properly, the carousel is causing frustration to the point where I want to smash my laptop. The carousel is not displaying images; instead, it only shows three sliding dots when using bootstrap 4.1.2. <!d ...

Tips for transforming a scroll element into the viewport using Angular 2+

This is a sample Here is a component with a list of items: class HomeComponent { text = 'foo'; testObject = {fieldFirst:'foo'}; itemList = [ '1', '2', '3', & ...

typescript handling a supposedly optional property that is not truly optional

As I embark on my journey with TypeScript, please bear with me if this is not the conventional way of doing things. I have a few objectives in transitioning this JavaScript code to TypeScript. Item = {} Item.buy = function (id) {} Item.sell = function (i ...

Materials within an instancedMesh in the React Three Fiber library are handled independently

I have a collection of different geometries that require individual and dynamic text to be displayed. My current approach involves using a canvas material that is updated in real-time within a useFrame function along with the position of the mesh. Howeve ...

Does the useState hook have any connection to hoisting in React?

I am relatively new to React.js and JavaScript, currently working on a project where I need the ability to manually update my components as needed, due to limitations with a third-party library. After doing some research, I came across a pattern on the of ...