Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an API for users who don't know the glTF API #26

Closed
naikrovek opened this issue Sep 24, 2019 · 4 comments
Closed

Provide an API for users who don't know the glTF API #26

naikrovek opened this issue Sep 24, 2019 · 4 comments

Comments

@naikrovek
Copy link

A simpler (and limited) API surface for users who do not know glTF should be provided for usability.

A user should be able to provide a list of vertices, zero or more materials, textures, and triangles, and be able to get a glTF file without further interaction with your API.

This API surface should be limited; there are only so many things that such an API would make sense for, to me. Animation would not be supported, and clearly static meshes would.

I don't know if this is within the scope of this API, and I wanted to suggest this in case it is.

@vpenades
Copy link
Owner

vpenades commented Sep 24, 2019

I think what you're looking for is the MeshBuilder<> class, which is included in the Toolkit package, in SharpGLTF.Geometry namespace.

This is a minimal example of how to create a triangle scene:

using SharpGLTF.Scenes;
using SharpGLTF.Geometry;
using SharpGLTF.Geometry.VertexTypes;
using SharpGLTF.Materials;

...

// Create a mesh with a triangle.
var mesh = new MeshBuilder<VertexPosition>();
var prim = mesh.UsePrimitive(MaterialBuilder.CreateDefault());

prim.AddTriangle
   (
   new VertexPosition(0, -1, 0), 
   new VertexPosition(1, 0, 0),
   new VertexPosition(0, 1, 0)
   );

// Create a scene and add the mesh:

var scene = new SceneBuilder();

scene.AddRigidMesh(mesh, Matrix4x4.Identity);

// save it to GLB:

scene.ToGltf2().Save("scene.glb");

let me know if this is what you're looking for, or you need something different.

@naikrovek
Copy link
Author

Whoa, that's pretty much it! I didn't know this was possible, thank you.

Is there a way to do this if I have UV coordinates for the vertices and/or vertex colors?

@vpenades
Copy link
Owner

yes, MeshBuilder<> uses predefined vertex fragment structures, so you can do this:

new MeshBuilder<VertexPositionNormal, VertexColor1Texture1>();

The MeshBuilder API is more or less explained here , although the documentation is a bit old.

@naikrovek
Copy link
Author

Excellent, thank you! I'll close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants