Creating Backbone models that inherit the prototype from THREE.Object3D

I am looking to create a unique model that combines the functionality of Backbone.Model with another prototype from THREE.Object3D in three.js version r69. Currently, my approach involves creating an object and setting its prototype to be that of THREE.Object3D like this:

var MyObj = new function(){...}
MyObj.prototype = Object.create(THREE.Object3D.prototype);
MyObj.prototype.constructor = THREE.Object3D;

However, I am facing challenges in making MyObj an instance of Backbone.Model while still retaining the THREE.Object3d prototype so that I can seamlessly add it to a scene and use it as a regular THREE.Object3d without compromising the Backbone.Model interface.

When I attempt to extend Backbone.Model with THREE.Object3D's prototype using the following code:

var MyObj = Backbone.Model.extend(THREE.Object3D.prototype);

I encounter errors related to attributes being undefined, particularly rotation. I am unsure why this is happening. Why is rotation showing up as undefined?

EDIT: Check out my fiddle here. Don't forget to open the console to view the errors!

Answer №1

To achieve this, I would suggest extending the prototype of Backbone.Model and then invoking the constructor function of THREE.Object3D. This can be done by:

var Object3DModel = Backbone.Model.extend({

    initialize: function() {
        _(this.prototype).extend(THREE.Object3D.prototype);
        THREE.Object3D.apply(this, arguments);
    }

});

In this example, I am utilizing the underscore extend method to extend the prototype.

You can view a demonstration on jsbin.

Edit

After further experimentation, it appears that a more effective approach would be:

// Begin with a constructor, calling the constructors of both classes
var Object3DModel = function() {
  THREE.Object3D.apply(this, arguments);
  Backbone.Model.apply(this, arguments);
};

// Copy Object3D Prototype
Object3DModel.prototype = new THREE.Object3D();

// Add Backbone.Model prototype
var ref = new Backbone.Model();
for ( var name in ref ) {
  Object3DModel.prototype[name] = ref[name];
}

Due to the use of getters and setters in the THREE library, this is the correct way to inherit them.

I have also created a JsBin demonstration which includes testing of the basic functionality.

Answer №2

Perhaps this solution will be beneficial to you.

let myObject = Backbone.Model.extend(THREE.Object3D.prototype);

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

Utilize jQuery ajax to target a particular recurring input

I am using HTML code along with another jQuery loop. Within this loop, there is an input element that is also being looped. I am trying to manipulate it using jQuery Ajax... but I am facing an issue where only the first input element works properly. The re ...

How to retrieve a JavaScript variable within the codebehind of an ASP.NET application

Storing geolocation (lat,long) to a database table on page load is needed. Here is the script used for this purpose: <body onload="getLocation()"> <p>Click the button to get your coordinates.</p> <%--<button oncl ...

Modify the -webkit-text-stroke- hue using JavaScript

I've been working on a website that features text with "text stroke", and I am attempting to dynamically change the color of this text stroke using JavaScript. My goal is to have four different color options available. However, as it is based on webk ...

Incapable of stopping a form submission when a certain criteria is satisfied

I am currently working on a form validation system using jQuery $.ajax. The aim is to only allow submission of the form if a specific condition, data == 1, is met. var preventSubmit = function() { return false; var form = $(this), name = form.find ...

"Use Jquery to automatically scroll back to the top of the page after opening a new window

Currently, when a button is clicked, I am opening a new window that takes me to a specific website. However, in addition to this, I also need the new window to scroll down slightly once it opens. This is how I am currently achieving this: $(document).read ...

"Utilizing NodeJS in conjunction with socket.io and PostgreSQL's LISTEN feature

My goal is to create a frontend that can detect events whenever a specific table in my Postgres database is modified. The Postgres events are triggering correctly and I am able to transmit them through the Socket.io connection. However, I am experiencing ...

The JSON file is not being parsed by Javascript

Attempting to create an HTML-based player that requires a playlist to function. Encountering issues with reading a local JSON file on Chromium running on Raspberry Pi 3, although the entire setup works flawlessly on my laptop using EDGE (the getJSON functi ...

Give a CSS Element a specific class

Can all h3 elements be styled with the class responsive-heading using only CSS? Take a look at the CSS snippet provided for more clarity: h3 { .responsive-heading } /* The responsive-heading class is defined in another CSS file and includes: .respons ...

Translate Latitude and Longitude values into X and Y coordinates on an Albers projection map

Trying to plot a point on a map with Albers projection using the given latitude and longitude coordinates, but encountering issues. The map is built with the Albers projection using standard parallels at 52 and 64 degrees with WGS 84. I attempted to imple ...

Step-by-step guide on utilizing User scripts with the $window.open function in AngularJS

I am looking to initiate the following action - (for example: Upon clicking a button, it should direct to this , similar to how payment gateways function) and then, I need to input the user script below: getRealData('{"mailing":{"postalCode":"1 ...

Using curly braces for imports within a Nuxt component

I am facing an issue with importing Three.js in a Nuxt component. <script> import * as THREE from "three"; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' .. Although the first line of code works fine, t ...

How can I retrieve data from an API in Vue using promises?

Within my repository.js file, I have the following code snippet: async getAllContactRequests() { return new Promise(() => { axios .get("http://127.0.0.1:8000/api/contactRequests", { headers: { "Authorization&qu ...

display a table using angularjs only if the table contains at least one row

I am currently developing a web application using MVC5 and AngularJS. The main component of my app is a table. <div class="table-responsive scroll" ng-hide="hidetable"> <table id="table" class="table table-bordered table-condensed"> ...

The application of the applyMatrix method does not effectively adjust the scale in the Y axis when

Currently, I am working on a web page dedicated to showcasing 3D transformations using Three.js. During this project, I encountered an issue when attempting a negative scale in the Y axis. Despite my efforts, the object does not invert as intended, unlike ...

Building a structured object from the HTML document outline

I have a vision to develop a jQuery plugin that traverses the top layer of the DOM, collecting elements into an object until it reaches a heading. Once a heading is encountered, the plugin creates a new object with the text of that heading. The subsequent ...

"Using a function parameter as a key in a JSON object

While I may not be a JSON expert, I am struggling to understand why this code is not functioning as expected and what steps I need to take to fix it. Despite my efforts to search for solutions, I suspect that my lack of familiarity with the correct termino ...

What is preventing bots and crawlers from detecting Next.js meta tags?

I'm currently using Next.js with Typescript and MongoDB to retrieve data. I'm encountering difficulties in rendering the page because the crawler is unable to detect the meta tags. For instance, Bing Search Engine claims that Title and Meta desc ...

Going through an array of objects that hold arrays of data

const endpoint = 'https://raw.githubusercontent.com/FormidableLabs/radon-typeahead/master/demo/car-models.json'; const cars = []; //fetching the data on the cars, converting the raw json into json and adding it to the array ca ...

The radio input is not being properly checked using ng-checked in Angular

The ng-checked attribute is causing issues in the application, specifically with radio buttons. It seems to be working fine for checkboxes but not for radio buttons. <input type="radio" class="radio" name="job_class_radio" ng-checked="key===jobClassDat ...

Javascript Workers - how can I improve the handling of worker messages and address any delays?

I have a situation where a Worker is sharing a SharedArrayBuffer with the main thread. In order for everything to function properly, it's crucial that the worker has access to the SAB before the main thread tries to access it. (EDIT: The code creating ...