Skip to content

Commit

Permalink
got something working again
Browse files Browse the repository at this point in the history
  • Loading branch information
bertt committed Jun 19, 2024
1 parent 8031526 commit 417ac26
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
46 changes: 33 additions & 13 deletions src/GPUTileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,29 @@ public static byte[] GetGPUTile(List<Instance> 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;
}

Expand Down Expand Up @@ -78,8 +92,8 @@ public static byte[] GetGpuGlbClassicMethod(object model, List<Instance> 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);
Expand All @@ -90,21 +104,30 @@ public static byte[] GetGpuGlbClassicMethod(object model, List<Instance> positio
return bytes;
}

private static FeatureIDBuilder GetFeatureIdBuilder(ModelRoot gltf, List<Instance> positions)
private static FeatureIDBuilder GetFeatureIdBuilder(StructuralMetadataClass schemaClass, List<Instance> positions)
{
var propertyTable = GetPropertyTable(positions, gltf);
var propertyTable = GetPropertyTable(schemaClass, positions);

var featureId0 = propertyTable != null ?
new FeatureIDBuilder(positions.Count, 0, propertyTable) :
new FeatureIDBuilder(positions.Count, 0);
return featureId0;
}

private static StructuralMetadataClass AddMetadataSchema(ModelRoot gltf)
{
var rootMetadata = gltf.UseStructuralMetadata();
var schema = rootMetadata.UseEmbeddedSchema("schema");
var schemaClass = schema.UseClassMetadata("propertyTable");
return schemaClass;
}

private static SceneBuilder AddModels(IEnumerable<Instance> instances, Vector3 translation, bool UseScaleNonUniform)
{
var sceneBuilder = new SceneBuilder();

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


foreach (var model in distinctModels)
{
Expand All @@ -116,10 +139,10 @@ private static SceneBuilder AddModels(IEnumerable<Instance> instances, Vector3 t

private static void AddModelInstancesToScene(SceneBuilder sceneBuilder, IEnumerable<Instance> 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)
{
Expand Down Expand Up @@ -166,7 +189,7 @@ private static AffineTransform GetInstanceTransform(Instance instance, bool UseS
}


private static PropertyTable GetPropertyTable(List<Instance> positions, ModelRoot gltf)
private static PropertyTable GetPropertyTable(StructuralMetadataClass schemaClass, List<Instance> positions)
{
var tags = new List<JArray>();

Expand All @@ -178,9 +201,6 @@ private static PropertyTable GetPropertyTable(List<Instance> 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);

Expand Down
2 changes: 1 addition & 1 deletion src/TileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static byte[] GetTile(List<Instance> 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)
{
Expand Down

0 comments on commit 417ac26

Please sign in to comment.