@@ -1666,6 +1666,9 @@ void* UnityPrepareRendererResources::prepareInMainThread(
16661666
16671667 int32_t meshIndex = 0 ;
16681668
1669+ // i3dm index tracking variable
1670+ int i3dmGlobalIndex = 0 ;
1671+
16691672 // For backwards compatibility.
16701673 CesiumForUnity::CesiumMetadata metadataComponent =
16711674 pModelGameObject
@@ -1691,6 +1694,7 @@ void* UnityPrepareRendererResources::prepareInMainThread(
16911694 &pModelGameObject,
16921695 &tileTransform,
16931696 &meshIndex,
1697+ &i3dmGlobalIndex,
16941698 &tilesetComponent,
16951699 pCoordinateSystem,
16961700 createPhysicsMeshes,
@@ -1754,14 +1758,26 @@ void* UnityPrepareRendererResources::prepareInMainThread(
17541758
17551759 ::DotNet::UnityEngine::Object::DestroyImmediate (primitiveGameObject);
17561760
1757- ExtractInstanceDataFromGltfModel (gltf, tileTransform, instanceData);
1761+ // Extract only instances of the current node (i3dm) (to prevent duplication)
1762+ const auto * pGpuInstancing =
1763+ node.getExtension <CesiumGltf::ExtensionExtMeshGpuInstancing>();
1764+
1765+ if (pGpuInstancing && !pGpuInstancing->attributes .empty ()) {
1766+ ExtractInstanceDataFromExtMeshGpuInstancing (
1767+ gltf,
1768+ node,
1769+ *pGpuInstancing,
1770+ tileTransform * transform,
1771+ instanceData);
1772+ }
17581773
17591774 for (size_t instanceDataIndex = 0 ;
1760- instanceDataIndex < instanceData.size () / mesh. primitives . size () ;
1775+ instanceDataIndex < instanceData.size ();
17611776 ++instanceDataIndex) {
17621777
17631778 UnityEngine::GameObject intanceGameObject (System::String (
1764- " Mesh " + std::to_string (instanceDataIndex) +
1779+ " I3dm " + std::to_string (i3dmGlobalIndex) +
1780+ " Mesh " + std::to_string (instanceDataIndex) +
17651781 " Primitive " +
17661782 std::to_string (primitiveIndex)));
17671783 if (showTilesInHierarchy) {
@@ -1777,6 +1793,9 @@ void* UnityPrepareRendererResources::prepareInMainThread(
17771793 intanceGameObject.layer (tilesetLayer);
17781794 intanceObjects.push_back (intanceGameObject);
17791795 }
1796+
1797+ // Increment the index with the next i3dm
1798+ i3dmGlobalIndex++;
17801799 }
17811800
17821801 glm::dmat4 modelToEcef = tileTransform * transform;
0 commit comments