The organization and control of metadata associated with threejs Geometry

Looking for advice on how to connect Metadata to my three.js geometries. Since there is no direct Attribute available in the Object, what would be the most effective method for linking this data?

Answer №1

If you want to keep track of user information, the Object.userData object is a great way to do so.

update: Keep in mind that Geometries do not support userData...

However, userData can still serve its purpose and will be saved in serialization. To work around this limitation, consider implementing something like

myScene.userData.geometryMetas={}
and then fill myScene.userData.geometryMetas[geometry.uuid]=YourMetaData

Answer №2

There are three approaches you can take.

1. Monkey patching

To quickly add the Attribute property directly to the three.js object, use this method:

var myGeom = new THREE.Geometry();
myGeom.Attributes = Metadata;

While this method is fast and simple, it comes with drawbacks such as being hacky and somewhat fragile (typical of monkey patching), and the metadata won't be included in the Geometry's toJSON() method, which may or may not be desired.

2. Wrapping

Alternatively, wrapping the geometry in a new object with its own Attributes property offers another option. You can create a plain object like this:

var myGeom = {
    geometry: new THREE.Geometry(),
    attributes: Metadata
};

Or opt for using a constructor:

function MyGeom(geometry, attributes) {
    this.geometry = geometry;
    this.attributes = attributes;
}
var myGeom = new MyGeom(new THREE.Geometry, Metadata);

The downside here is that you'll need to handle wrapped objects instead of straightforward THREE.Geometry instances, which involves unwrapping them when interacting with the Three.js API and re-wrapping any data obtained from it.

3. Keep Metadata separate

If you prefer keeping attribute data separate from geometries, you can utilize each THREE.Geometry object's unique uuid property. By using this uuid as a key in a dedicated metadata object, you can work with regular Three.js objects like so:

var AllMetadata = {}; // Global or project namespace assignment
var myGeom = new THREE.Geometry();
AllMetadata[myGeom.uuid] = Metadata;

This approach allows you to avoid carrying attribute data along with geometries needlessly, while still having it accessible when required.

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

Obtaining the accurate offsetTop and offsetLeft values for an element following a CSS3 rotation

Is there a method to accurately determine the offsetTop and offsetLeft values of an element post-transform rotation? Are there any lesser-known element properties that could be helpful in this scenario? Attached is an image that can provide further clari ...

Steps to retrieve a specific key value from each object in an AJAX request

I am looking to extract the "url" key and its value from the provided code and save it in a variable. I have attempted different methods to isolate the url key as seen in my commented-out code. $.ajax({ type: 'GET', url: "url", // data: da ...

Modify JSON from using single quotes to double quotes using JavaScript

Received a JSON from the backend to the front end that uses single quotes throughout, causing issues with a Magento 2 widget. The JSON structure is as follows: { 'mood': 'happy', 'reason': 'why shouldn't I?'} T ...

Would parallax stars be overwhelming for a webpage?

I'm a newcomer to the world of web development and I have an idea to make stars move across my website. However, I'm concerned about whether this might be too much for a simple website to handle. Is there a way to optimize the code? const canvas ...

A Step-by-Step Guide on Importing Components in Vue.js

I have defined the following component as shown below: import Vue from 'vue'; import Datepicker from 'vuejs-datepicker'; Vue.component('DatePicker', { template: ` <datepicker name="date"></datepicker> ` ...

Harnessing the power of flexbox for data visualization

Trying to use flexbox to display data from a dataset in my code. Here is the code snippet: <div ng-app="myapp" ng-controller="FirstCtrl"> <div ng-repeat="name in names" class="graph-wrapper"> <div class="aside-1 content"> ...

Is it possible to extract the version_name using the version_code of an android package?

Is it possible to retrieve the version_name by using the version_code of an Android package? For instance: 'com.nianticlabs.pokemongo' version_code: 2017121800 => version_name: 0.87.5 I'm looking for a function that can accomplish th ...

The GLTFExporter exports GLTF files with a shade of gray

I recently exported a glTF file using this demo page. However, when I imported the file into Blender, the 3D model appeared in grey color and seemed to be missing color information. Does anyone know how to rectify this issue? Could it be that glTF files d ...

javascript issue with attribute manipulation

My current struggle involves setting the attribute of an element through programming, but I keep encountering an error in Firebug: obj.setAttribute is not a function. Since I am working with jQuery, allow me to provide some additional code for better conte ...

Traverse through the JSON data until a certain condition is satisfied, then proceed to tally the number

Looking to parse a json file containing user data and points. The goal is to loop through the data until the _id matches the userId, then determine the position of that user in the list based on the number of objects. The json file provided below already ...

Scrolling automatically to the first empty mandatory field with the help of AngularJS

After utilizing angular js to create a form with 7 input elements, I encountered an issue. Upon clicking submit, the form should scroll up to the first blank required field. However, it is not accurately identifying the left blank field. Any suggestions on ...

What could be the reason for the malfunction of this angular binding?

Looking at the HTML below: <input type="checkbox" name="person" [(ngModel)]="person.selected" /> This input is part of a ngFor loop. Testing has revealed that despite some values of selected being true and others false, all checkboxes appear check ...

Tips for utilizing JavaScript getElementByClassName to retrieve all the elements within a ul without having to specify the class name in each li

Looking to tidy up my HTML/CSS. Is there a way to keep this JavaScript functioning without needing to add the class name to every li element within the ul? Any suggestions on how to improve the visual appeal and readability of the HTML code? const Profi ...

Mastering the Art of Page Scrolling with d3

I would like to implement a scrolling effect for my d3 that allows the entire page to scroll while panning, similar to the effect on challonge (http://challonge.com/tournaments/bracket_generator?ref=OQS06q7I5u). However, I only want the scrolling to occur ...

Utilizing React, generate buttons on the fly that trigger the display of their respective

Looking for a way to dynamically display 3 buttons, each of which opens a different modal? I'm struggling to figure out how to properly link the buttons to their respective modals. Here's the code I've attempted so far: Button - Modal Code: ...

HTML code for positioning an image after resizing

Within an HTML page, I have an image that has been resized to a width of 400px using CSS. .cropbox { width: 400px; height : auto; } Additionally, there is a JavaScript function associated with the image that allows users to select a point on the image. T ...

The API call for /api/users/create was resolved without a response, which could potentially lead to requests getting stuck. This issue was detected in

I've developed an API endpoint to manage user account creation within my Next.js application, utilizing knex.js for handling queries. Despite this, I keep encountering the following error: API resolved without sending a response for /api/users/create ...

Tips for eliminating default classes from Mui Typography styling

I’ve been working on creating a Typography element and noticed some default MUI CSS classes added when debugging in the browser. I attempted to disable them by using empty arrays at the end of the sx prop, but it did not work as expected. In the image p ...

Inspecting the Ace Editor within the onbeforeunload event handler to confirm any modifications

I'm attempting to utilize the $(window).on('beforeunload', function(){}); and editor.session.getUndoManager().isClean(); functions within the ace editor to detect whether a user has made modifications to a document without clicking the submi ...

Conceal an HTML element by utilizing *ngIf in Angular after a user clicks away from the designated area

Is there a way to implement an eventlistener on a <div> or another element, in order to hide a displayed item controlled by an *ngIf in Angular, when the user clicks away from that element? Context: I have a customized CSS dropdown that appears usin ...