Skip to content

Commit ae67c5b

Browse files
committed
XRLayer: prioritize renderOrder for layer sorting
1 parent a1d1ecb commit ae67c5b

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

packages/xr/src/store.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -682,23 +682,24 @@ export function createXRStore<T extends XRElementImplementations>(options?: XRSt
682682
if (currentLayers == null) {
683683
return
684684
}
685-
//sort by distance to camera
685+
//layer sorting
686686
const xrCamera = xrManager.getCamera()
687687
xrCamera.getWorldPosition(cameraWorldPosition)
688688
;(layerEntries as Array<XRLayerEntry>).sort((entryA, entryB) => {
689+
const renderOrderDifference = entryA.renderOrder - entryB.renderOrder
690+
691+
//if renderOrder is the same, sort by distance to camera
692+
if (renderOrderDifference !== 0) {
693+
return renderOrderDifference
694+
}
695+
689696
entryA.object3D.getWorldPosition(tempLayerWorldPosition)
690697
const distA_sq = tempLayerWorldPosition.distanceToSquared(cameraWorldPosition)
691698

692699
entryB.object3D.getWorldPosition(tempLayerWorldPosition)
693700
const distB_sq = tempLayerWorldPosition.distanceToSquared(cameraWorldPosition)
694701

695-
const depthDifference = distB_sq - distA_sq
696-
697-
//if the distance is the same, fall back to renderOrder
698-
if (Math.abs(depthDifference) < 0.00001) {
699-
return entryA.renderOrder - entryB.renderOrder
700-
}
701-
return depthDifference
702+
return distB_sq - distA_sq
702703
})
703704
let changed = false
704705
const layers = layerEntries.map<XRLayer>(({ layer }, i) => {

0 commit comments

Comments
 (0)