Stabilize visible render item sorting

This commit is contained in:
2026-04-05 19:19:47 +08:00
parent 31693a83b6
commit 1de8091b77
4 changed files with 73 additions and 16 deletions

View File

@@ -113,22 +113,7 @@ uint32_t GetAdditionalLightTypeRank(const Components::LightComponent& light) {
}
bool CompareVisibleItems(const VisibleRenderItem& lhs, const VisibleRenderItem& rhs) {
if (lhs.renderQueue != rhs.renderQueue) {
return lhs.renderQueue < rhs.renderQueue;
}
const bool isTransparentQueue = IsTransparentRenderQueue(lhs.renderQueue);
if (lhs.cameraDistanceSq != rhs.cameraDistanceSq) {
return isTransparentQueue
? lhs.cameraDistanceSq > rhs.cameraDistanceSq
: lhs.cameraDistanceSq < rhs.cameraDistanceSq;
}
if (lhs.gameObject != rhs.gameObject) {
return lhs.gameObject < rhs.gameObject;
}
return lhs.sectionIndex < rhs.sectionIndex;
return CompareVisibleRenderItemsStable(lhs, rhs);
}
} // namespace

View File

@@ -116,6 +116,41 @@ void AppendRenderItemsForGameObject(
outVisibleItems.push_back(visibleItem);
}
bool CompareVisibleRenderItemsStable(
const VisibleRenderItem& lhs,
const VisibleRenderItem& rhs) {
if (lhs.renderQueue != rhs.renderQueue) {
return lhs.renderQueue < rhs.renderQueue;
}
const bool isTransparentQueue = IsTransparentRenderQueue(lhs.renderQueue);
if (lhs.cameraDistanceSq != rhs.cameraDistanceSq) {
return isTransparentQueue
? lhs.cameraDistanceSq > rhs.cameraDistanceSq
: lhs.cameraDistanceSq < rhs.cameraDistanceSq;
}
const Core::uint64 lhsObjectId = lhs.gameObject != nullptr ? lhs.gameObject->GetID() : 0u;
const Core::uint64 rhsObjectId = rhs.gameObject != nullptr ? rhs.gameObject->GetID() : 0u;
if (lhsObjectId != rhsObjectId) {
return lhsObjectId < rhsObjectId;
}
if (lhs.hasSection != rhs.hasSection) {
return lhs.hasSection && !rhs.hasSection;
}
if (lhs.sectionIndex != rhs.sectionIndex) {
return lhs.sectionIndex < rhs.sectionIndex;
}
if (lhs.materialIndex != rhs.materialIndex) {
return lhs.materialIndex < rhs.materialIndex;
}
return false;
}
std::vector<VisibleRenderItem> CollectRenderItemsForEntityIds(
const Components::Scene& scene,
const std::vector<uint64_t>& entityIds,