简体   繁体   English

管理Threejs Geometry的元数据

[英]Managing Metadata of threejs Geometry

I have Metadata for my three.js geometries. 我的three.js几何图形具有元数据。 What is the best way to link this Metadata, as there is no Attribute where I would be able to store it directly in the Object itself? 链接此元数据的最佳方法是什么,因为没有属性可以直接将其存储在对象本身中?

You can store user data in the Object.userData object. 您可以将用户数据存储在Object.userData对象中。

edit: It's been pointed out that Geometries don't have a userData... 编辑:有人指出,几何没有userData ...

But userData can still be used and will be serialized, so perhaps something along the lines of 但是userData仍然可以使用并且将被序列化,所以也许类似

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

You have three options. 您有三个选择。

1. Monkey patching 1.猴子修补

Just add the Attribute property directly to the three.js object. 只需将Attribute属性直接添加到three.js对象即可。

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

This has the advantage of being quick and easy. 这具有快速简便的优点。 It has the disadvantages of being hacky and somewhat fragile (as monkey patching always is), and the metadata won't be included in the Geometry's toJSON() method, which may or may not be what you want. 它的缺点是易破解且有些脆弱(就像猴子修补一样),并且元数据不会包含在Geometry的toJSON()方法中,这可能是您想要的,也可能不是。

2. Wrapping 2.包装

Just wrap the geometry in a new object with its own Attributes property. 只需将几何图形包装在具有自己的Attributes属性的新对象中即可。 You can use a plain object: 您可以使用一个普通对象:

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

or create a constructor: 或创建一个构造函数:

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

The disadvantage is that everywhere that you would have simply carried around THREE.Geometry objects, you now have to use the wrapped objects, and unwrap them when it's time to call into the Three.js API, and re-wrap anything you get out of it. 缺点是,到处只能携带THREE.Geometry对象,现在您必须使用包装的对象,并在需要时调用Three.js API对其进行包装,然后重新包装从包装中获取的所有内容。它。

3. Keep Metadata separate 3.将元数据分开

Every THREE.Geometry object has a unique uuid property. 每个THREE.Geometry对象都有一个唯一的uuid属性。 You can use this uuid as the key in a separate metadata object and still just work with regular Three.js objects. 您可以将此uuid用作单独的元数据对象中的键,并且仍然只能与常规Three.js对象一起使用。

var AllMetadata = {}; // Should be global or assigned to a project namespace
var myGeom = new THREE.Geometry();
AllMetadata[myGeom.uuid] = Metadata;

This way you don't have to pass around your attribute data with the geometries, but it's available when you do need it. 这样,您就不必通过几何传递属性数据,但是在需要时可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM