Skip to content

Commit

Permalink
Merge pull request #927 from hypar-io/merge-extensions-for-gltfs
Browse files Browse the repository at this point in the history
Merge extensions for gltfs (#927)
  • Loading branch information
wynged authored Dec 7, 2022
2 parents a640e33 + b7005e9 commit 4e5c517
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 14 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
- Remove the BBox3 validator.
- `RoutingConfiguration.MainLayer` and `RoutingConfiguration.LayerPenalty` are set obsolete.
- `EdgeInfo.HasVerticalChange` is set obsolete.
- `AdaptiveGraphRouting.RenderElements` is no longer paint hint lines in two different colors. Instead regular edges are paint into three groups. Weights are included to additional properties of produced elements.
- `AdaptiveGraphRouting.RenderElements` is no longer paint hint lines in two different colors. Instead regular edges are paint into three groups. Weights are included to additional properties of produced elements.


### Fixed

- Fix `Obstacle.FromLine` if line is vertical and start point is positioned higher than end.
- Materials exported to glTF now have their `RoughnessFactor` set correctly.
- Materials exported to glTF no longer use the `KHR_materials_pbrSpecularGlossiness` extension, as this extension is being sunset in favor of `KHR_materials_specular` and `KHR_materials_ior`.
- Gltfs that are merged that require additional extensions will also merge their extensions.
- Don't try to save test models that have no name, they can interfere with each other because they want to save to the same file.

## 1.3.0

Expand Down
35 changes: 23 additions & 12 deletions Elements/src/Serialization/glTF/GltfExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ private static int GetNextId()

/// <summary>
/// In normal function use, this should be set to null.
/// If not null and set to a valid directory path, gltfs loaded for
/// If not null and set to a valid directory path, gltfs loaded for
/// content elements will be cached to this directory, and can be
/// explicitly loaded by calling LoadGltfCacheFromDisk(). This is used
/// by `hypar run` and test capabilities to speed up repeated runs.
/// by `hypar run` and test capabilities to speed up repeated runs.
/// </summary>
public static string GltfCachePath
{
Expand Down Expand Up @@ -904,9 +904,9 @@ internal static Gltf InitializeGlTF(Model model,

// Attempt to pre-allocate these lists. This won't be perfect.
// Before processing the geometry of an element we can't know
// how much to allocate. In the worst case, this will reduce
// how much to allocate. In the worst case, this will reduce
// the list resizing.
// TODO: In future work where we update element geometry during
// TODO: In future work where we update element geometry during
// UpdateRepresentations, we will know at this moment how big the
// geometry for an element is, and we should tighten this up.
var elementCount = model.AllElementsAssignableFromType<GeometricElement>().Count();
Expand Down Expand Up @@ -946,16 +946,19 @@ internal static Gltf InitializeGlTF(Model model,
};
gltf.Scenes = new[] { scene };

var lights = model.AllElementsOfType<Light>().ToList();
gltf.ExtensionsUsed = lights.Any() ? new[] {
"KHR_materials_specular",
"KHR_materials_ior",
"KHR_materials_unlit",
"KHR_lights_punctual"
} : new[] {
var extensionsUsed = new HashSet<string> {
"KHR_materials_specular",
"KHR_materials_ior",
"KHR_materials_unlit"};
"KHR_materials_unlit"
};

var lights = model.AllElementsOfType<Light>().ToList();
if (lights.Any())
{
extensionsUsed.Add("KHR_lights_punctual");
}

gltf.ExtensionsUsed = extensionsUsed.ToArray();

var bufferViews = new List<BufferView>();

Expand Down Expand Up @@ -1025,6 +1028,7 @@ internal static Gltf InitializeGlTF(Model model,
textures,
images,
samplers,
extensionsUsed,
meshes,
nodes,
meshElementMap,
Expand Down Expand Up @@ -1100,6 +1104,11 @@ internal static Gltf InitializeGlTF(Model model,
gltf.Meshes = meshes.ToArray(meshes.Count);
}

if (extensionsUsed.Count > 0)
{
gltf.ExtensionsUsed = extensionsUsed.ToArray();
}

// This is a hack! For web assembly, the ToArray() call creates
// a copy of all items in the list, and is extremely slow. We
// get around this by accessing the underlying list directly.
Expand All @@ -1125,6 +1134,7 @@ private static void GetRenderDataForElement(Element e,
List<Texture> textures,
List<Image> images,
List<Sampler> samplers,
HashSet<string> extensions,
List<glTFLoader.Schema.Mesh> meshes,
List<Node> nodes,
Dictionary<Guid, List<int>> meshElementMap,
Expand Down Expand Up @@ -1155,6 +1165,7 @@ private static void GetRenderDataForElement(Element e,
textures,
images,
samplers,
extensions,
true,
e.Id,
out var parentNode);
Expand Down
6 changes: 6 additions & 0 deletions Elements/src/Serialization/glTF/GltfMergingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static List<int> AddAllMeshesFromFromGlb(Stream glbStream,
List<Texture> textures,
List<Image> images,
List<Sampler> samplers,
HashSet<string> extensions,
bool shouldAddMaterials,
System.Guid contentElementId,
out ProtoNode parentNode
Expand Down Expand Up @@ -60,6 +61,11 @@ out ProtoNode parentNode
accessors.Add(originAccessor);
}

foreach (var extension in loaded.ExtensionsUsed)
{
extensions.Add(extension);
}

var imageIncrement = images.Count;
if (loaded.Images != null)
{
Expand Down
35 changes: 35 additions & 0 deletions Elements/test/ContentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections.Generic;
using System;
using System.IO;
using glTFLoader;
using System.Linq;

namespace Elements.Tests
{
Expand Down Expand Up @@ -56,6 +58,39 @@ public void CatalogSerialization()
Assert.Equal("The Value", loadedCatalog.Content[0].AdditionalProperties["ImportantParameter"]);
}

[Fact]
public void MergeExtensions()
{
Name = nameof(MergeExtensions);
// This piece of content uses the KHR_materials_pbrSpecularGlossiness extension which is no longer used in our models.
const string contentLocation = "../../../models/MergeGlTF/LittleShapes.glb";
var littleShapeContent = new TestContentElem(contentLocation,
new BBox3(new Vector3(-0.5, -0.5, 0), new Vector3(0.5, 0.5, 3)),
new Vector3(),
new Transform(new Vector3(), Vector3.XAxis),
20,
BuiltInMaterials.Default,
null,
true,
Guid.NewGuid(),
"LittleShapes");
var anInstance = littleShapeContent.CreateInstance(new Transform(new Vector3(15, 0, 0)), "LittleShapes1");

var modelPath = $"./models/{nameof(MergeExtensions)}.glb";
Model.AddElement(new Mass(Polygon.Rectangle(1, 1)));
Model.ToGlTF(modelPath);
var gltfModelEmpty = Interface.LoadModel(modelPath);
var initialExtensions = gltfModelEmpty.ExtensionsUsed;

var gltfContent = Interface.LoadModel(contentLocation);
Model.AddElement(anInstance);
Model.ToGlTF(modelPath);
var gltfModelMerged = Interface.LoadModel(modelPath);
Assert.NotEmpty(gltfContent.ExtensionsUsed);
Assert.NotEmpty(gltfContent.ExtensionsUsed.Except(initialExtensions));
Assert.All(gltfContent.ExtensionsUsed, (ext) => Assert.Contains(ext, gltfModelMerged.ExtensionsUsed));
}

[Fact, Trait("Category", "Example")]
public void InstanceContentElement()
{
Expand Down
2 changes: 1 addition & 1 deletion Elements/test/ModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ModelTest()

public virtual void Dispose()
{
if (this._model.Elements.Any())
if (this._model.Elements.Any() && !String.IsNullOrEmpty(this._name))
{
if (this.GenerateGlb)
{
Expand Down
Binary file added Elements/test/models/MergeGlTF/LittleShapes.glb
Binary file not shown.

0 comments on commit 4e5c517

Please sign in to comment.