Skip to content

Commit

Permalink
Handle gpu instancing tags is all null for a tile
Browse files Browse the repository at this point in the history
  • Loading branch information
bertt committed Oct 16, 2024
1 parent a69e1d9 commit 45961d7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/GPUTileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@ public static byte[] GetGPUTile(List<Instance> instances, bool UseScaleNonUnifor
settings.GpuMeshInstancingMinCount = 0;
var model = sceneBuilder.ToGltf2(settings);

var schema = AddMetadataSchema(model);
if (instances.Any(s => s.Tags != null))
{

var distinctModels = instances.Select(s => s.Model).Distinct();
var schema = AddMetadataSchema(model);

var i = 0;
var distinctModels = instances.Select(s => s.Model).Distinct();

foreach (var distinctModel in distinctModels)
{
var modelInstances = instances.Where(s => s.Model.Equals(distinctModel)).ToList();
var featureIdBuilder = GetFeatureIdBuilder(schema, modelInstances);
var node = model.LogicalNodes[i];
node.AddInstanceFeatureIds(featureIdBuilder);
i++;

var i = 0;

foreach (var distinctModel in distinctModels)
{
var modelInstances = instances.Where(s => s.Model.Equals(distinctModel)).ToList();
var featureIdBuilder = GetFeatureIdBuilder(schema, modelInstances);
var node = model.LogicalNodes[i];
node.AddInstanceFeatureIds(featureIdBuilder);
i++;
}
}

foreach (var node in model.LogicalNodes)
Expand Down
31 changes: 31 additions & 0 deletions tests/TileHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@ namespace i3dm.export.tests;

public class TileHandlerTests
{
[Test]
public void GetGpuTileWithoutTagsTest()
{
Tiles3DExtensions.RegisterExtensions();

// arrange
var instances = new List<Instance>();
var instance = new Instance();
instance.Position = new Wkx.Point(1, 2, 0);
instance.Scale = 1;
instance.Model = "Box.glb";
instances.Add(instance);

// act
var tile = GPUTileHandler.GetGPUTile(instances, UseScaleNonUniform: false);

var fileName = Path.Combine(TestContext.CurrentContext.WorkDirectory, "instancing_tile_without_tags.glb");
File.WriteAllBytes(fileName, tile);

var model = ModelRoot.Load(fileName);

// assert
// Model only contains the EXT_mesh_gpu_instancing extension, no other extensions
Assert.That(model.ExtensionsUsed.Count() == 1);
Assert.That(model.ExtensionsUsed.First() == "EXT_mesh_gpu_instancing");

Assert.That(tile.Length > 0);
}

[Test]
public void GetGpuTileTest()
{
Expand All @@ -34,6 +63,8 @@ public void GetGpuTileTest()
File.WriteAllBytes(fileName, tile);

var model = ModelRoot.Load(fileName);
Assert.That(model.ExtensionsUsed.Count() == 3);

var extInstanceFeaturesExtension = model.LogicalNodes[0].GetExtension<MeshExtInstanceFeatures>();

var extStructuralMetadataExtension = model.GetExtension<EXTStructuralMetadataRoot>();
Expand Down

0 comments on commit 45961d7

Please sign in to comment.