vpenades / SharpGLTF

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

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

Closed naikrovek closed 5 years ago

naikrovek commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

Excellent, thank you! I'll close this.