SharpGLTF Documentation

repository·master·Indexed 20 days ago

https://github.com/vpenades/sharpgltf

A .NET Standard library for the Khronos Group glTF 2.0 file format, providing tools for reading, writing, rendering, and manipulating glTF models. It is distributed via specialized packages: SharpGLTF.Core for low-level file support, SharpGLTF.Runtime for rendering helpers, and SharpGLTF.Toolkit for model manipulation. The library includes support for various glTF2 extensions, 3D Tiles extensions via SharpGLTF.Ext.3DTiles, and provides a MonoGame loading pipeline.

Tokens
12.2K
Snippets
24
Records
49
Agent score
65%

What's inside SharpGLTF

  1. Overview of SharpGLTF packages

    master

    SharpGLTF is a .NET Standard library for supporting the Khronos Group glTF 2.0 file format. It is distributed via several specialized NuGet packages depending on your requirements:

    • SharpGLTF.Core: Provides read/write file support and low-level access to glTF models.
    • SharpGLTF.Runtime: Contains helper classes to simplify glTF model rendering.
    • SharpGLTF.Toolkit: Provides convenience utilities for creating, manipulating, and evaluating glTF models.

    Optional extension libraries include SharpGLTF.Ext.Agi and SharpGLTF.Ext.3DTiles.

  2. Overview of SharpGLTF.Toolkit

    master

    The SharpGLTF.Toolkit is a collection of classes and utilities designed to simplify the programmatic creation and editing of glTF files.

    While the core Schema2 namespace provides the underlying glTF structure, it is primarily designed as an "append-only" API (optimized for writing rather than modification). The Toolkit API is specifically built to bridge this gap, providing a more flexible and user-friendly interface for asset creation and manipulation that the base Schema2 API does not support.

  3. Supported 3D Tiles extensions in SharpGLTF.Ext.3DTiles

    master

    The SharpGLTF.Ext.3DTiles project provides support for several 3D Tiles extensions within the SharpGLTF ecosystem.

    Supported extensions:

    • CESIUM_primitive_outline
    • EXT_Mesh_Features
    • EXT_Instance_Features
    • Ext_Structural_Metadata

    Limitations for Ext_Structural_Metadata:

    • External schema is not supported.
    • min/max/scale/offset properties for StructuralMetadataClassProperty and PropertyAttributeProperty are not supported.
  4. Understand the SharpGLTF Core namespaces

    master

    SharpGLTF Core is organized into several namespaces that handle different aspects of glTF2 processing:

    • .Schema2: The primary low-level API for accessing glTF2 documents. The main entry point for representing a glTF2 model is the ModelRoot object.
    • .Runtime: Used for evaluating a model. This is useful for tasks like dumping a raw list of triangles in their final positions or preparing a model for rendering in a graphics engine.
    • .Transforms: Manages the scene graph and node relationships (typically via 4x4 matrices). It also handles coordinate space transformations, including skinning and morphing (moving meshes from local to world space).
    • .Animations: Provides classes for decoding and interpolating animation curves.
    • .Memory: Provides helper classes and structures to facilitate seamless access to structured arrays stored as encoded byte buffers in glTF2.
    • .IO: Contains logic related to JSON serialization.
  5. How SceneTemplate and SceneInstance work together

    master

    To render glTF models in a custom graphics engine, SharpGLTF.Runtime uses a two-tier abstraction: SceneTemplate and SceneInstance.

    1. SceneTemplate: An immutable object representing the resource asset in memory. You create it from a glTF scene (e.g., model.DefaultScene). It serves as the master blueprint for all instances.
    2. SceneInstance: Lightweight objects that reference a SceneTemplate. Instances allow you to:
      • Animate the model independently using SetAnimationFrame(animationName, time).
      • Manually edit individual nodes (e.g., using SetWorldMatrix(nodeName, matrix)) without affecting other instances.
      • Maintain separate states for multiple copies of the same model in a scene.

    This pattern allows you to load the heavy glTF data once and then efficiently manage many different animated or positioned versions of that model.

    // 1. Create the immutable template
    var modelTemplate = SharpGLTF.Runtime.SceneTemplate(model.DefaultScene, true);
    
    // 2. Create lightweight, independent instances
    var inst1 = modelTemplate.CreateInstance();
    inst1.SetAnimationFrame("Walking", 2.17f);
    
    var inst2 = modelTemplate.CreateInstance();
    inst2.SetWorldMatrix("Head", Matrix.LookAt(...) );
  6. How to traverse a glTF document using Logical and Visual approaches

    master

    The SharpGLTF.Schema2 namespace provides two distinct ways to traverse a glTF document depending on your goal:

    1. Logical Traversal: Use the ModelRoot.Logical* collections. This provides direct access to the individual building blocks exactly as they are stored in the glTF schema (often as plain lists with integer index cross-referencing). This is useful for low-level inspection of the document structure.

    2. Visual Traversal: Start with ModelRoot.DefaultScene and navigate through nodes and properties using .Visual* properties. This treats the document as a visual tree graph, which is more intuitive for navigating the scene hierarchy and spatial relationships.

    Note on Editing: Editing existing glTF models via Schema2 has very limited support. Removing elements is generally not possible because glTF data is often shared between elements, and removal would require expensive internal data reshuffling. For complex editing tasks, use SharpGLTF.Toolkit instead.

  7. Explore SharpGLTF.Toolkit namespaces

    master

    The Toolkit is organized into several functional namespaces to help manage different aspects of glTF asset construction:

    • Scenes: For managing the scene hierarchy and structure.
    • Geometry: For defining mesh data and geometry.
      • VertexTypes: For specifying different vertex layouts and formats.
    • Materials: For defining surface appearances and material properties.
  8. Configure fallback materials in MaterialBuilder

    master

    The MaterialBuilder supports defining a fallback material to be used if the primary material is not supported by a rendering engine.

    Constraint: Due to glTF limitations, this fallback feature is only available when:

    1. The main material uses the SpecularGlossiness shader.
    2. The fallback material uses the MetallicRoughness shader.
  9. Understand the MonoGame glTF loading pipeline limitations

    master
    When using the SharpGLTF MonoGame pipeline, loading glTF models involves extra vertex processing. This is because the implementation relies on MonoGame's default BasicEffect and SkinnedEffect. Because these effects have specific requirements, glTF vertex and index buffers cannot always be uploaded directly to the GPU; they must be processed to match the formats supported by these specific MonoGame shaders.
  10. How the Toolkit Scene API differs from the glTF Schema2 API

    master

    The Toolkit API uses a high-level, visual approach compared to the low-level glTF Schema2 API.

    In the glTF Schema2 API, you manually manage the hierarchy: you create a Scene, add Node children to it, and then manually assign Mesh and Skin references to those nodes.

    In the Toolkit API, you focus on what you want to render. Methods like AddMesh automatically handle the creation of the necessary nodes and instances. A SceneBuilder acts as a collection of rendering instances, and NodeBuilder objects are used to define hierarchical structures or armatures.

    // glTF Schema2 API approach (Manual hierarchy management)
    scene = model.UseScene(0);
    var n1 = scene.CreateNode();
    n1.Mesh = ...
    var n2 = scene.CreateNode();
    n2.Mesh = ...
    
    // Toolkit API approach (Visual/Intent-based rendering)
    scene = new SceneBuilder();
    scene.AddRigidMesh(...);
    scene.AddSkinnedMesh(...);
  11. How vertex fragments work in SharpGLTF Toolkit

    master

    In glTF, vertex buffers are collections of accessors with specific attributes, dimensions, and encodings. To simplify mesh creation, SharpGLTF Toolkit uses the concept of vertex fragments.

    Instead of manually managing complex buffer layouts, you compose a full vertex by combining predefined building blocks (fragments) from three categories: Position, Material, and Skinning. This abstraction ensures that the resulting vertex structure follows valid glTF combinations used by most runtimes.

    Common valid combinations include:

    • Position + Empty + Empty
    • Position Normal + Color0 + Joints0
    • Position Normal Tangent + Texture0 + Joints0 + Joints1
    • Position Normal Tangent + Color0 + Texture0 + Texture1
  12. How glTF data structures and memory mapping work

    master

    In glTF 2.0, data is stored in a hierarchical structure of byte buffers. To access structured data, you must navigate this hierarchy:

    1. Buffers: The lowest storage level, containing raw Byte[] arrays.
    2. BufferViews: Slices over a Buffer, behaving similarly to C# ArraySegment<byte>.
    3. Accessors: Metadata that describes how to decode the bytes in a BufferView into structured data (e.g., interpreting bytes as floats or integers).

    To work with this data in C#, you can use the wrappers in the SharpGLTF.Memory namespace to wrap raw byte arrays and expose them as typed arrays.