Expanding the functionality of Three.js classes

I need help figuring out how to extend the Object3D class in Three.js.

I've been trying to follow a Stackoverflow question on this topic, but despite multiple attempts, I haven't been successful.

Is there a way to extend a ThreeJS object?

If anyone has specific code examples or tips on how to make this work, it would be greatly appreciated. Here's what I have tried so far:

var MyObject3D = function() {
   THREE.Object3D.call(this);
   MyObject3D.prototype = new THREE.CubeGeometry();
   MyObject3D.prototype.constructor = MyObject3D;
}

When creating an instance:

var thing = new MyObject3D();
var testGeo = new THREE.CubeGeometry(10, 10, 10);
var testMat = new THREE.MeshPhongMaterial();
var testMesh = new THREE.Mesh(testGeo, testMat);
thing.add(testMesh);

However, when trying to call the "add" method on the MyObject3D instance using "thing," an error saying that "thing" does not have a method "add" is returned.

Can someone please explain what might be causing this issue?

Answer №1

It seems like you're assigning the prototype to CubeGeometry, which does not have an add method. From your instantiation approach, it appears that you actually want your object to inherit from the Mesh prototype.

You may need something along these lines:

var MyObject3D = function() {
    // Call the Mesh constructor with the provided arguments
    THREE.Mesh.apply(this, arguments);
};
// Inherit methods from Mesh for MyObject3D
MyObject3D.prototype = Object.create(THREE.Mesh.prototype);
// Ensure the correct constructor is called
MyObject3D.prototype.constructor = MyObject3D;

To create an instance:

var testGeo = new THREE.CubeGeometry(20, 20, 20);
var testMat = new Three.MeshNormalMaterial();
var thing = new MyObject3D(testGeo, testMat);

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

Creating a cascading select box with two levels in PHP and MySQLExplanation on how to generate a two-tier connected

While I have successfully retrieved values from a MySQL database using a select box in PHP, I am struggling with implementing a two-level chained select box. Does anyone have any sample code or suggestions on how to achieve this? Thank you. ...

Integrating Ace Editor into a PHP Powered Web Application

Currently, I am working on a small web application that allows users to input HTML, CSS, and JavaScript content through the use of Ace Editor. While it is relatively easy to display stored content in the editor, I am facing difficulties when trying to figu ...

Is there a way to remotely access and read text files stored on a server using either JavaScript or Ajax from an IIS6.0 Virtual directory?

Is it possible to use JavaScript or Ajax to read the text from a file on a remote machine (from IIS6.0 Virtual directory) and then copy it into a specific folder on the client machine? ...

Show or conceal a class

Hello there! I've been attempting to create a toggle effect using an anchor link with an "onclick" event to show and hide content. Despite my efforts with jQuery and JavaScript functions, I just can't seem to figure out the right approach. Here& ...

Is there a way to verify if a time interval has already been established using Javascript?

I've encountered an issue with a bouncing div that activates every 5 seconds through an interval. As I scroll to the bottom of the page, the div fades out and the interval is supposed to be cleared. However, it seems like there's a problem with ...

Nodejs and express authentication feature enables users to securely access their accounts by verifying their identity before they

I am currently working on a straightforward registration page that needs to save user input (name, email, password) into a database. My tools for this task are express and node. What I am experimenting with is consolidating all the database operations (suc ...

Maximizing the benefits of the express.js API through functions such as fetching data with get, sending

As someone who is just starting out in web development, I have recently delved into node and express 4.9.0. After going through some tutorials and experimenting with the API, it's clear to me that the http protocol and request URLs are essential comp ...

Change the colors and messages shown when the button is clicked or hovered over

I'm seeking assistance in getting this code to function properly, specifically to display different colors and messages when specific buttons are clicked or hovered over. My goals are: To change the background color of the page when the button &apo ...

What could be causing this array to be blank?

In the process of working on a product feedback app using React for my portfolio, I encountered an unexpected issue. The problem arises in my SuggestionDetails component, where I am fetching the current id with useParams and filtering out the current produ ...

Trigger a click event upon page load using jQuery or JavaScript

I tried searching for this functionality on a different website, but it didn't work on my site. Can you help me figure out how to trigger a click event on page load? On one of my pages, a popup box appears when I click on a "project" link. Here is th ...

Shopify Inventory Dropdown Tailored to Stock Levels

I'm having an issue with my Shopify store. How can I make sure that the quantity dropdown matches the actual items in stock? For instance, if there are only 3 items available, the maximum value in the quantity dropdown should be set to 3. And if there ...

Top technique for mirroring a website

Recently, I created a directory for a client's website. The site is fully operational, but the client would like a replicated version for testing and learning purposes. To create a full replica of his website, it will require hours of work to change ...

Unable to display the retrieved list data using v-for loop from axios get request

I recently started using Vue.js and decided to create a simple HTML website with Vue.js to display data from myphpadmin using axios. Everything was working smoothly until I encountered an issue where I couldn't display the JSON message on my screen. n ...

Disabling Express BodyParser for handling file uploads in Node.js

When developing my web application with Node.js + Express, I've found the connect BodyParser to be incredibly useful. However, I'm looking for a way to have more control over multipart form-data POSTS so that I can stream the input directly to an ...

Discovering the parent route details of the current route from within a component

I am facing an issue where I need to identify the parent route /products while working within the scope of the FeaturedProducts.vue component. The current route path for this component is set as /product-list/featured const routes = [ { path: "/ ...

Can we modify the styling of elements in Angular based on an object property?

I have a class named Task with the following properties: export class Task { name: string; state: number; } Within my component.ts file, I have an array of objects consisting of instances of the Task class (tasks). When displaying them in the tem ...

How can I display a timer icon in front of text on a Material-UI CardHeader subtitle?

I have a question regarding displaying time in my posts. Currently, I am showing the time as 'a few seconds ago', '2mins ago', 'an hour ago', etc. However, I would like to include a clock icon before this string. Although I a ...

Use Yii2 to pass an ID when a button is clicked in order to render a partial using

On the index page, I display all the rows and some fields from my model. When a button is clicked, I want a modal to appear with all the data from the corresponding row. When the button is clicked, I need an ajax call to render a partial view. This will i ...

Launching an external JavaScript from within a separate JavaScript file

I'm in the process of developing a virtual 'directory' for various locations in my city to assist fellow students. Here's the concept: On a map, I've pinned all the locations Clicking on these pins triggers a JavaScript funct ...

Error message: Attempting to access the 'path' property of an undefined variable results in TypeError while utilizing cloudinary services

I am currently attempting to upload a file from React to an Express server, but I keep encountering this error TypeError: Cannot read property 'path' of undefined The method I am using involves React for transferring/uploading the file to the ...