Creating a custom Object class in THREE.js that handles imported meshes

Hey there, I'm just getting started with THREE.js and I'm trying to create a custom class that extends THREE.Mesh for my scene. My goal is to have the class contain an imported mesh via JSON loader, but so far all my attempts have not been successful.

Below is my code snippet:

THREE.ImportedMesh = function(){
    this.type = 'ImportedMesh';

    this.load = function(url){
        var loader = new THREE.JSONLoader();
        loader.load(url, function(geometry,materials){

            THREE.Mesh.call(self,geometry,new THREE.MeshFaceMaterial(materials));
        });
    };
};
THREE.ImportedMesh.prototype = Object.create( THREE.Mesh.prototype );
THREE.ImportedMesh.prototype.constructor = THREE.ImportedMesh;

Unfortunately, I keep encountering these errors in the console:

Uncaught TypeError: Cannot read property 'remove' of undefined

Uncaught TypeError: this.updateMorphTargets is not a function

If anyone has any insights on how to resolve this, I would greatly appreciate it!

Thanks,

Rick

Answer №1

If you're looking for a similar solution, consider the following code snippet:

CustomMesh = function(){
  this.category = 'CustomMesh';

  THREE.Mesh.call(this);
  var self = this
  this.loadData = function(url){
    var dataLoader = new THREE.JSONLoader();
    dataLoader.load(url, function(geometry,materials){
       self.material = new THREE.MeshFaceMaterial(materials)
       self.geometry = geometry
    });
  };
};
CustomMesh.prototype = Object.create( THREE.Mesh.prototype );
CustomMesh.prototype.constructor = CustomMesh;

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 best way to modify an array within separate functions in a NodeJS environment?

I am facing an issue where I want to update an object inside the fetchAll() functions and then send it back after successful updation. However, the response I receive is '[]'. var ans = [] Country.fetchAll(newdate,(err, data) => { if ...

Avoiding JavaScript onclick event using JSON information

Here's a situation I'm dealing with: I have a button created using PHP that triggers a JavaScript onclick event: <button onClick='edit(this, "<?php echo $this->result[$i]["type"]; ?>","<?php echo $quality; ?>", "<?php e ...

Utilizing sub-object attributes as ng-model in AngularJS

I'm working with a dropdown that is connected to a user object structured like this: { userId:1, userLogin:"test", locale: { localeCode:"en-US" } } This structure originates from a C# model class where User and Locale are associated classes (U ...

Ways to verify and incorporate https:// in a URL for a MEAN Stack application

When extracting the URL from API data, my code looks like this: <div class="row copy-text"> <a href="{{copy.Url}}" target="_blank" style="text-decoration: underline !important;">{{copy.Title}}</a> </div> I am interested in ve ...

Tips for transitioning from an old link to a new link within an MVC framework

Is there a way to redirect an old link to a new link in MVC? In Google search results, my old URL was cached as www.abcd.com/product?id=64 however, my new URL is now www.abcd.com/product/sample How can I set up a redirect so that when a user clicks on th ...

Tips for implementing live camera feed in reactJs

UPDATE I attempted to make changes in the code by using srcObject, however, it did not produce the desired result as expected. componentDidMount() { navigator.mediaDevices.getUserMedia({ video: true, audio: true }) .then( stream => { ...

Is it possible to utilize waxjs for retrieving data on NFTs within the Wax Cloud Wallet?

I am currently in the process of incorporating the Wax Cloud Wallet into my React/NextJS application. To achieve this, I am utilizing waxjs and referencing the documentation available here. At present, users can log into their accounts and the applicatio ...

Prevent AJAX request while in progress?

I've made some adjustments to a jQuery Autocomplete plugin, which now retrieves a JSON object from a MySQL database instead of an array. However, I've noticed that each time I click on the input field, it triggers a new request, even if it&apos ...

Using HTML5 validation on a form along with a button that triggers a JavaScript function to send an email via PHP and display a modal upon

When I click the button on my website's form, a JS function is triggered to take the inputs from the form and send an email using PHP. After the email is sent, a modal popup appears thanking the user for contacting. Everything works perfectly until I ...

The encodeURIComponent function does not provide an encoded URI as an output

Looking to develop a bookmarklet that adds the current page's URL to a specific pre-set URL. javascript:(function(){location.href='example.com/u='+encodeURIComponent(location.href)}()); Even though when I double encode the returned URL usin ...

Ensure that variables are accessible to asynchronous calls without the use of closures

As a newcomer to the world of javascript, I've been trying to navigate the realm of nested functions. Let's explore the following two examples: // example 1 var x = 45; function apple(){ var y = 60; setTimeout(function(){ console ...

Retrieve data from an external website containing an HTML table without a table ID using Java Script and transform it into JSON

I have developed a script that can convert HTML table data into a JSON Object. To accomplish this task, I utilized the jquery plugin created by lightswitch05. With this code, I am able to extract data from an HTML table on the same web page using: var t ...

Functions for abbreviating and combining strings in Javascript

Looking for help to simplify and shorten a Javascript function: $scope.doRefresh = function (){ if($scope.bulletpointPopular){ ArticleService.popular().then(function(data){ $scope.articles = data; }) .finally(function() { ...

What measures can be taken to block Javascript from retrieving PHP cookie information?

(Extracted from an interview) Identify the correct answers from the list below: Implement the httponly parameter when creating the cookie The user needs to disable Javascript support This setting is related to cookies in the browser Restrict access to t ...

Configuring babel-loader in webpack for optimal HMR functionality

Recently, I encountered an issue while trying to add Hot Module Replacement (HMR) to my project. Despite the console showing that HMR was enabled and detecting changes in files, the view was not re-rendering. The console would display: [HMR] Updated modul ...

Encountering issues with resolving dependencies in webdriverIO

I'm attempting to execute my WebdriverIo Specs using (npm run test-local) and encountering an error even though I have all the necessary dependencies listed in my package.json as shown below: [0-2] Error: Failed to create a session. Error forwardin ...

Code in JavaScript: Generating Random Number within a Loop

Can anyone help me come up with a unique JavaScript loop that can guess a correct number within the range of 1-500? I want each iteration of the loop to generate a new number that has not been guessed before, but it should guess in a random order. For ex ...

Deactivating one div's class upon clicking on another div

Below is the HTML code snippet: <div class="container"> <ul class="navbar"> <li class="nb-link"><a>Home</a></li> <li class="dropdown"> <a>CBSE</a> <ul class="dropdown-menu"&g ...

Explain the operation of recursive function calls in JavaScript

I’ve been working on converting the algorithm found in this Python code snippet into JavaScript. function divide(arr, depth, m) { if (complements.length <= depth) { complements.push(2 ** (depth + 2) + 1); } var complement = comple ...

Angular and Node integration with Firestore authentication

I need some guidance with integrating an Angular application and a Node.js API, both using Firestore (Firebase). We have encountered an issue when validating tokens for authenticated users - the token never seems to expire. Even after logging out in the An ...