Skip to content

Commit

Permalink
Merge pull request #1038 from hypar-io/multiple-representations-two
Browse files Browse the repository at this point in the history
gltf: parent node for an element and the representations as children
  • Loading branch information
katehryhorenko authored Oct 10, 2023
2 parents 9bcdbe9 + 5e9dacb commit 9f8db4e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@

### Changed
- `GltfExtensions.UseReferencedContentExtension` is now true by default.

### Changed

- `GeometricElement.Intersects` method now supports multiple representations
- `GeometricElement.Intersects` method now supports multiple representations.
- `GltfExtensions.ToGlTF` creates parent node for element and child nodes for representation instances.

## 2.0.0

Expand Down
7 changes: 5 additions & 2 deletions Elements/src/Serialization/glTF/GltfExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,17 +1289,19 @@ private static void GetRenderDataForElement(Element e,

if (element.RepresentationInstances != null && element.RepresentationInstances.Any())
{
var elementNodeId = NodeUtilities.AddInstanceNode(nodes, element.Transform, element.Id);
foreach (var representation in element.RepresentationInstances)
{
// get the unique id that contains representation Id and opening Ids
int combinedId = representation.GetHashCode(element);

if (representationsMap.TryGetValue(combinedId, out var mesh))
{
var addedNodes = NodeUtilities.AddInstanceNode(nodes, mesh, element.Transform, e.Id);
var addedNodes = NodeUtilities.AddNodes(nodes, mesh, elementNodeId);
foreach (var index in addedNodes)
{
NodeUtilities.SetRepresentationInfo(nodes[index], representation);
NodeUtilities.SetElementInfo(nodes[index], element.Id);
}
}
else if (representation.Representation.TryToGraphicsBuffers(geometricElement, out var graphicsBuffers,
Expand All @@ -1320,10 +1322,11 @@ private static void GetRenderDataForElement(Element e,
{
var meshIdList = new List<int> { meshId };
representationsMap.Add(combinedId, meshIdList);
var addedNodes = NodeUtilities.AddInstanceNode(nodes, meshIdList, element.Transform, e.Id);
var addedNodes = NodeUtilities.AddNodes(nodes, meshIdList, elementNodeId);
foreach (var index in addedNodes)
{
NodeUtilities.SetRepresentationInfo(nodes[index], representation);
NodeUtilities.SetElementInfo(nodes[index], element.Id);
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions Elements/src/Serialization/glTF/NodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ internal static int[] AddNodes(List<Node> nodes, IEnumerable<Node> newNodes, int
return newIds;
}

internal static int[] AddNodes(List<Node> nodes, List<int> meshIds, int? parentId)
{
var newNodes = meshIds.Select(meshId =>
{
return new Node() { Mesh = meshId };
});

return AddNodes(nodes, newNodes, parentId);
}

internal static int AddNode(List<Node> nodes, Node newNode, int? parentId)
{
return NodeUtilities.AddNodes(nodes, new[] { newNode }, parentId).First();
Expand Down Expand Up @@ -138,6 +148,14 @@ private static int RecursivelyCopyNode(List<Node> nodes, ProtoNode nodeToCopy)
return nodeIndex;
}

internal static int AddInstanceNode(List<glTFLoader.Schema.Node> nodes, Transform transform, Guid elementId)
{
float[] matrix = TransformToMatrix(transform);
var newNode = new Node() { Matrix = matrix, Name = elementId.ToString() };
newNode.SetElementInfo(elementId);
return AddNode(nodes, newNode, 0);
}

internal static int[] AddInstanceNode(
List<glTFLoader.Schema.Node> nodes,
List<int> meshIds,
Expand Down

0 comments on commit 9f8db4e

Please sign in to comment.