vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
467 stars 75 forks source link

Any way to add some string description to MeshBuilder? #56

Closed berry4u closed 3 years ago

berry4u commented 4 years ago

I need to translate IFC geometry to GLTF enriching meshes with some description. Is there a way to to this?

Thank you very much.

Berardo

vpenades commented 4 years ago

Without having to resort to custom extensions, There's two places you can use to write "plain text":

From SharpGLTF, you can use the extras property like this:

// writing
var mesh = modelRoot.LogicalMeshes[3];
var extras = mesh.TryUseExtrasAsDictionary(true);
extras["IFC-Description"] = "My mesh description goes here";
// reading
var mesh = modelRoot.LogicalMeshes[3];
var extras = mesh.TryUseExtrasAsDictionary(false);
var ifcdesc = extras == null ? null : extras["IFC-Description"] as String;

Keep in mind that the extras property is accesible only from the Schema2 namespace. So if you're building your scene with the toolkit's SceneBuilder, SceneBuilder namespace does not have Extras property support, so you'll have to do some mapping and then write the extras directly to the logical meshes.

EugeneNIghtingale commented 3 years ago

Hello! How can I implement writing not an array or a dictionary, but my own class as an object? Example: "extras":{ "Params":[ {"name":"Somestring1","datatype":"Somestring2","value":"Somestring3"}]}

vpenades commented 3 years ago

@EugeneNIghtingale This is a serialization problem.

Keep in mind that when you set the Extras property, it is not serialized to json immediately, but when you save your model. So until then, I have to keep the data stored in memory in one way or another. And lists and dictionaries is the closest match to a json object.

Maybe it is possible to serialize your class to json, and then have an intermediate pass that would copy the json content to the lists and dictionaries in Extras, so the serialization/deserialization process woud be like this:

Custom Class => Json Text => Object/List/Dictionary => Extras Property Extras Property => Object/List/Dictionary => Json Text => Custom Class

prasad-debug commented 3 years ago

I have used extras to write the Dictionary data to root model and it worked fine. I need to write extras for every part written in scene. can we use extras in scene?

vpenades commented 3 years ago

@prasad-debug As far as I know, yes, the Extras property is available in almost all classes of a the schema, including the Scene.

prasad-debug commented 3 years ago

Thank you for the information. I have used Scenebuilder to fill the tessellated data and then converted it into schema2. I m able to add the properties to whole tessellated data, I'm looking for something in which the properties are added in scene builder along with position normal and textcoords. is there any way through which we can achieve it?

vpenades commented 3 years ago

SceneBuilder does not have any extras feature yet, I have to figure out an API to do that.

So for now, do it the way you're doing it right now, and at some point I'll introduce an API to support extras along all the *Builder APIs.

prasad-debug commented 3 years ago

Thank you for helping me out with my query. As suggested ill continue using the current approach of adding properties and will wait for the API to support extras in scenebuilder to be introduce soon.

vpenades commented 3 years ago

The new alpha21 has a much improved API for working with Extras, hope this will solve your problem.