From 417ac26f0eb49939ed83cfe3f550a8474b167313 Mon Sep 17 00:00:00 2001 From: Bert Temme Date: Wed, 19 Jun 2024 14:19:27 +0200 Subject: [PATCH] got something working again --- src/GPUTileHandler.cs | 46 +++++++++++++++++++++++++++++++------------ src/TileHandler.cs | 2 +- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/GPUTileHandler.cs b/src/GPUTileHandler.cs index fd5f4f9..e6fcd40 100644 --- a/src/GPUTileHandler.cs +++ b/src/GPUTileHandler.cs @@ -23,15 +23,29 @@ public static byte[] GetGPUTile(List instances, bool UseScaleNonUnifor var settings = SceneBuilderSchema2Settings.WithGpuInstancing; settings.GpuMeshInstancingMinCount = 0; - var finalModel = sceneBuilder.ToGltf2(settings); + var model = sceneBuilder.ToGltf2(settings); - // todo: add metadata - foreach (var node in finalModel.LogicalNodes) + var schema = AddMetadataSchema(model); + + var distinctModels = instances.Select(s => s.Model).Distinct(); + + 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) { node.LocalTransform *= Matrix4x4.CreateTranslation(translation); } - var bytes = finalModel.WriteGLB().Array; + var bytes = model.WriteGLB().Array; return bytes; } @@ -78,8 +92,8 @@ public static byte[] GetGpuGlbClassicMethod(object model, List positio var settings = SceneBuilderSchema2Settings.WithGpuInstancing; settings.GpuMeshInstancingMinCount = 0; var gltf = sceneBuilder.ToGltf2(settings); - - var featureIdBuilder = GetFeatureIdBuilder(gltf, positions); + var schema = AddMetadataSchema(gltf); + var featureIdBuilder = GetFeatureIdBuilder(schema, positions); var node = gltf.LogicalNodes[0]; // todo: what if there are multiple nodes? node.AddInstanceFeatureIds(featureIdBuilder); @@ -90,9 +104,9 @@ public static byte[] GetGpuGlbClassicMethod(object model, List positio return bytes; } - private static FeatureIDBuilder GetFeatureIdBuilder(ModelRoot gltf, List positions) + private static FeatureIDBuilder GetFeatureIdBuilder(StructuralMetadataClass schemaClass, List positions) { - var propertyTable = GetPropertyTable(positions, gltf); + var propertyTable = GetPropertyTable(schemaClass, positions); var featureId0 = propertyTable != null ? new FeatureIDBuilder(positions.Count, 0, propertyTable) : @@ -100,11 +114,20 @@ private static FeatureIDBuilder GetFeatureIdBuilder(ModelRoot gltf, List instances, Vector3 translation, bool UseScaleNonUniform) { var sceneBuilder = new SceneBuilder(); var distinctModels = instances.Select(s => s.Model).Distinct(); + foreach (var model in distinctModels) { @@ -116,10 +139,10 @@ private static SceneBuilder AddModels(IEnumerable instances, Vector3 t private static void AddModelInstancesToScene(SceneBuilder sceneBuilder, IEnumerable instances, bool UseScaleNonUniform, Vector3 translation, string model) { - var pointId = 0; var modelInstances = instances.Where(s => s.Model.Equals(model)).ToList(); var modelRoot = ModelRoot.Load(model); var meshBuilder = modelRoot.LogicalMeshes.First().ToMeshBuilder(); // todo: what if there are multiple meshes? + var pointId = 0; foreach (var instance in modelInstances) { @@ -166,7 +189,7 @@ private static AffineTransform GetInstanceTransform(Instance instance, bool UseS } - private static PropertyTable GetPropertyTable(List positions, ModelRoot gltf) + private static PropertyTable GetPropertyTable(StructuralMetadataClass schemaClass, List positions) { var tags = new List(); @@ -178,9 +201,6 @@ private static PropertyTable GetPropertyTable(List positions, ModelRoo if (tags.Count > 0 && tags[0] != null) { PropertyTable propertyTable; - var rootMetadata = gltf.UseStructuralMetadata(); - var schema = rootMetadata.UseEmbeddedSchema("schema"); - var schemaClass = schema.UseClassMetadata("propertyTable"); propertyTable = schemaClass.AddPropertyTable(positions.Count); diff --git a/src/TileHandler.cs b/src/TileHandler.cs index 65118ed..7cb8100 100644 --- a/src/TileHandler.cs +++ b/src/TileHandler.cs @@ -13,7 +13,7 @@ public static byte[] GetTile(List instances, bool UseExternalModel = f { if (useGpuInstancing) { - // return GPUTileHandler.GetGPUTile(instances, UseScaleNonUniform); + return GPUTileHandler.GetGPUTile(instances, UseScaleNonUniform); }; if (useGpuInstancing && instances.Select(s => s.Model).Distinct().Count() > 1) {