# SharpGLTF.Ext.3DTiles

This project contains the implementation of 3D Tiles support for SharpGLTF.

The following extensions are supported:

- CESIUM_primitive_outline

Specs: https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Vendor/CESIUM_primitive_outline/README.md

Samples: https://github.com/CesiumGS/3d-tiles-samples/tree/main/glTF/CESIUM_primitive_outline/BoxPrimitiveOutline

- EXT_Mesh_Features

Specs: https://github.com/CesiumGS/glTF/tree/proposal-EXT_mesh_features/extensions/2.0/Vendor/EXT_mesh_features

Samples: https://github.com/CesiumGS/3d-tiles-samples/blob/main/glTF/EXT_mesh_features

- EXT_Instance_Features

Specs: https://github.com/CesiumGS/glTF/tree/3d-tiles-next/extensions/2.0/Vendor/EXT_instance_features

Samples: https://github.com/CesiumGS/3d-tiles-samples/tree/main/glTF/GpuInstancesMetadata

- Ext_Structural_Metadata

Specs: https://github.com/CesiumGS/glTF/tree/proposal-EXT_structural_metadata/extensions/2.0/Vendor/EXT_structural_metadata

Samples: https://github.com/CesiumGS/3d-tiles-samples/tree/main/glTF/EXT_structural_metadata

Not supported: External schema and min/max/scale/offset properties for StructuralMetadataClassProperty and PropertyAttributeProperty

## Unit testing 

## Reading 3D Tiles glTF files

The unit test project contains a set of glTF files that are used to test the implementation of the extensions. The glTF files 
are obtained from https://github.com/CesiumGS/3d-tiles-validator/tree/main/specs/data/gltfExtensions.

## Writing 3D Tiles glTF files

See the unit test project for examples of how to write glTF files with the extensions.

The unit tests writes glTF files like the samples from https://github.com/CesiumGS/3d-tiles-samples/tree/main/glTF

## Validation

For reading glTF files with 3D Tiles extensions the files should be valid according to a validator. The validator is available at https://github.com/CesiumGS/3d-tiles-validator

When writing glTF files with 3D Tiles extensions the resulting files should be valid according to the validator. 

## Sample code 

### Reading a 3D Tiles glTF file with metadata

```csharp
    Tiles3DExtensions.RegisterExtensions();
    var model = ModelRoot.Load("sample.gltf");
    var structuralMetadataExtension = model.GetExtension<EXTStructuralMetadataRoot>();
    var meshFeaturesExtension = model.LogicalMeshes[0].Primitives[0].GetExtension<MeshExtMeshFeatures>();
```

## Writing a 3D Tiles glTF file with attribute metadata

In the following sample a glTF with 1 triangle is created. The triangle contains metadata with
a name column. The name column is set to "this is featureId0". The triangle is assigned featureId 0.

```csharp
    Tiles3DExtensions.RegisterExtensions();
    int featureId = 0;
    var material = MaterialBuilder.CreateDefault().WithDoubleSide(true);

    var mesh = new MeshBuilder<VertexPositionNormal, VertexWithFeatureId, VertexEmpty>("mesh");
    var prim = mesh.UsePrimitive(material);

    var vt0 = VertexBuilder.GetVertexWithFeatureId(new Vector3(0, 0, 0), new Vector3(0, 0, 1), featureId);
    var vt1 = VertexBuilder.GetVertexWithFeatureId(new Vector3(1, 0, 0), new Vector3(0, 0, 1), featureId);
    var vt2 = VertexBuilder.GetVertexWithFeatureId(new Vector3(0, 1, 0), new Vector3(0, 0, 1), featureId);

    prim.AddTriangle(vt0, vt1, vt2);
    var scene = new SceneBuilder();
    scene.AddRigidMesh(mesh, Matrix4x4.Identity);
    var model = scene.ToGltf2();

    var rootMetadata = model.UseStructuralMetadata();
    var schema = rootMetadata.UseEmbeddedSchema("schema_001");

    var schemaClass = schema.UseClassMetadata("triangles");

    var nameProperty = schemaClass
        .UseProperty("name")
        .WithStringType();

    var propertyTable = schemaClass
        .AddPropertyTable(1);

    propertyTable
        .UseProperty(nameProperty)
        .SetValues("this is featureId0");

    foreach (var primitive in model.LogicalMeshes[0].Primitives)
    {
        var featureIdAttribute = new FeatureIDBuilder(1, 0, propertyTable);
        primitive.AddMeshFeatureIds(featureIdAttribute);
    }

    model.SaveGLTF(@"sample.gltf");
```

3D Tiles specific parts in the resulting glTF:

```
 "extensions": {
    "EXT_structural_metadata": {
      "propertyTables": [
        {
          "class": "triangles",
          "count": 1,
          "properties": {
            "name": {
              "stringOffsets": 3,
              "values": 2
            }
          }
        }
      ],
      "schema": {
        "classes": {
          "triangles": {
            "properties": {
              "name": {
                "type": "STRING"
              }
            }
          }
        },
        "id": "schema_001"
      }
    }
  },
  "extensionsUsed": [
    "EXT_structural_metadata",
    "EXT_mesh_features"
  ],
  "meshes": [
    {
      "name": "mesh",
      "primitives": [
        {
          "extensions": {
            "EXT_mesh_features": {
              "featureIds": [
                {
                  "attribute": 0,
                  "featureCount": 1,
                  "propertyTable": 0
                }
              ]
            }
          },
          "attributes": {
            "POSITION": 0,
            "NORMAL": 1,
            "_FEATURE_ID_0": 2
          },
          "indices": 3,
          "material": 0
        }
      ]
    }
  ],

  ```

  Sample loaded in Cesium:


  ![alt text](cesium_sample_metadata_attributes.png)

  ## Writing a 3D Tiles glTF file with texture metadata

  In the following sample a glTF with 2 triangles is created. For visualizing the triangles a texture of a building is used.  
  A second texture is used containing metadata for storing the metadata values:

  - R channel: Inside temperature

  - G channel: Outside temperature

  - B channel: Humidity

  Using a shader in the client application the metadata values can be visualized.

  ```csharp
    Tiles3DExtensions.RegisterExtensions();

    var img0 = " ";
    var imageBytes0 = Convert.FromBase64String(img0);
    var imageBuilder0 = ImageBuilder.From(imageBytes0);

    var img1 = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABLUlEQVR42mVSSxbDIAh0GzUxKZrmCF3n/oerIx9pupgHIswAGtblE7bIKN0vqSOyXSOjPLAtktv9sCFxmcXj7EgsFj8zN00yYxrBZZJBRYk2LdC4WCDUfAdab7bpDm1lCyBW+7lpDnyNS34gcTQRltTPbAeEdFjcSQ0X9EOhGPYjhgLA7xh3kjxEEpMj1qQj7iAzAYoPELzYtuwK02M06WywAFDfX1MdJEoOtSZ7Allz1mYmWZDNL0pNF6ezu9jsQJUcNK7qzbWvMdSYQ8Jo7KKK8/uo4dxreHe0/HgF2/IqBen/za+Di69Sf8cZz5jmk+hcuhdd2tWLz8IE5MbFnRWT+yyU5vZJRtAOqlvq6MDeOrstu0UidsoO0Ak9xGwE+67+34salNEBSCxX7Bexg0rbq6TFvwAAAABJRU5ErkJggg==";
    var imageBytes1 = Convert.FromBase64String(img1);
    var imageBuilder1 = ImageBuilder.From(imageBytes1);

    var material = MaterialBuilder
        .CreateDefault()
        .WithMetallicRoughnessShader()
        .WithBaseColor(imageBuilder0, new Vector4(1, 1, 1, 1))
        .WithDoubleSide(true)
        .WithAlpha(Materials.AlphaMode.OPAQUE)
        .WithMetallicRoughness(0, 1)
        .WithMetallicRoughness(imageBuilder1);

    var mesh = VBTexture1.CreateCompatibleMesh("mesh");
    var prim = mesh.UsePrimitive(material);
    prim.AddTriangle(
        new VBTexture1(new VertexPosition(0, 0, 0), new Vector2(0, 1)),
        new VBTexture1(new VertexPosition(1, 0, 0), new Vector2(1, 1)),
        new VBTexture1(new VertexPosition(0, 1, 0), new Vector2(0, 0)));

    prim.AddTriangle(
        new VBTexture1(new VertexPosition(1, 0, 0), new Vector2(1, 1)),
        new VBTexture1(new VertexPosition(1, 1, 0), new Vector2(1, 0)),
        new VBTexture1(new VertexPosition(0, 1, 0), new Vector2(0, 0)));

    var scene = new SceneBuilder();
    scene.AddRigidMesh(mesh, Matrix4x4.Identity);
    var model = scene.ToGltf2();

    // --------------------------------------------------------------

    var rootMetadata = model.UseStructuralMetadata();
    var schema = rootMetadata.UseEmbeddedSchema("SimplePropertyTextureSchema");

    // define schema 

    var exampleMetadataClass = schema
        .UseClassMetadata("buildingComponents")
        .WithName("Building properties");

    exampleMetadataClass
        .UseProperty("insideTemperature")
        .WithName("Inside temperature")
        .WithUInt8Type();

    exampleMetadataClass
        .UseProperty("outsideTemperature")
        .WithName("Outside temperature")
        .WithUInt8Type();

    exampleMetadataClass
        .UseProperty("insulation")
        .WithName("Insulation Thickness")
        .WithUInt8Type()
        .WithNormalized(true);

    // define texture property

    var buildingPropertyTexture = exampleMetadataClass.AddPropertyTexture();

    buildingPropertyTexture.CreateProperty("insideTemperature", model.LogicalTextures[1], new int[] { 0 });
    buildingPropertyTexture.CreateProperty("outsideTemperature", model.LogicalTextures[1], new int[] { 1 });
    buildingPropertyTexture.CreateProperty("insulation", model.LogicalTextures[1], new int[] { 2 });

    // assign to primitive

    var primitive = model.LogicalMeshes[0].Primitives[0];
    primitive.AddPropertyTexture(buildingPropertyTexture);

    model.SaveGLTF(@"sample.gltf");

```

3D Tiles specific parts in the resulting glTF:

```
  "extensions": {
    "EXT_structural_metadata": {
      "propertyTextures": [
        {
          "class": "buildingComponents",
          "properties": {
            "insideTemperature": {
              "index": 1,
              "channels": [
                0
              ]
            },
            "outsideTemperature": {
              "index": 1,
              "channels": [
                1
              ]
            },
            "insulation": {
              "index": 1,
              "channels": [
                2
              ]
            }
          }
        }
      ],
      "schema": {
        "classes": {
          "buildingComponents": {
            "name": "Building properties",
            "properties": {
              "insideTemperature": {
                "componentType": "UINT8",
                "name": "Inside temperature",
                "type": "SCALAR"
              },
              "outsideTemperature": {
                "componentType": "UINT8",
                "name": "Outside temperature",
                "type": "SCALAR"
              },
              "insulation": {
                "componentType": "UINT8",
                "name": "Insulation Thickness",
                "normalized": true,
                "type": "SCALAR"
              }
            }
          }
        },
        "id": "SimplePropertyTextureSchema"
      }
    }
  },
"meshes": [
    {
      "name": "mesh",
      "primitives": [
        {
          "extensions": {
            "EXT_structural_metadata": {
              "propertyTextures": [
                0
              ]
            }
          },
          "attributes": {
            "POSITION": 0,
            "TEXCOORD_0": 1
          },
          "indices": 2,
          "material": 0
        }
      ]
    }
  ],
```

Shader code for visualizing the metadata:

```
// Create a custom (fragment) shader that accesses the metadata value with the
// given property name, normalizes it to a value in [0,1] based on the given
// source range, and uses that value as the brightness for the fragment.
function createShader(propertyName, sourceMin, sourceMax) {
  const shader = new Cesium.CustomShader({
    fragmentShaderText: `
      void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material)
      {
        float value = float(fsInput.metadata.${propertyName});
        float range = float(${sourceMax}) - float(${sourceMin});
        float brightness = (value - float(${sourceMin})) / range;
        material.diffuse = vec3(brightness);
      }
    `,
  });
  return shader;
}

tileset.customShader = createShader("insideTemperature", 0.0, 255.0);
```

Sample loaded in Cesium:

![alt text](cesium_sample_metadata_texture.png)
