Skip to content

Commit 24c673b

Browse files
committed
fix flickering
1 parent 0a28da9 commit 24c673b

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

packages/uikit/src/components/root.ts

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export function setupRoot<EM extends ThreeEventMap = ThreeEventMap>(
202202
setupObjectTransform(state.root, object, state.globalMatrix, abortSignal)
203203

204204
const onCameraDistanceFrame = () => {
205+
state.root.reversePainterSortStableCache = undefined
205206
planeHelper.normal.set(0, 0, 1)
206207
planeHelper.constant = 0
207208
planeHelper.applyMatrix4(object.matrixWorld)

packages/uikit/src/order.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MergedProperties } from './properties/merged.js'
44
import { computedInheritableProperty } from './properties/index.js'
55
import { readReactive } from './utils.js'
66

7-
export type WithCameraDistance = { cameraDistance: number }
7+
export type WithCameraDistance = { cameraDistance: number; reversePainterSortStableCache?: number }
88

99
export const cameraDistanceKey = Symbol('camera-distance-key')
1010
export const orderInfoKey = Symbol('order-info-key')
@@ -16,11 +16,21 @@ export function reversePainterSortStable(a: RenderItem, b: RenderItem) {
1616
if (a.renderOrder !== b.renderOrder) {
1717
return a.renderOrder - b.renderOrder
1818
}
19-
const aDistanceRef = (a.object as any)[cameraDistanceKey] as WithCameraDistance
20-
const bDistanceRef = (b.object as any)[cameraDistanceKey] as WithCameraDistance
19+
let az = a.z
20+
let bz = b.z
21+
const aDistanceRef = (a.object as any)[cameraDistanceKey] as WithCameraDistance | undefined
22+
const bDistanceRef = (b.object as any)[cameraDistanceKey] as WithCameraDistance | undefined
23+
if (aDistanceRef != null) {
24+
aDistanceRef.reversePainterSortStableCache ??= az
25+
az = aDistanceRef.reversePainterSortStableCache
26+
}
27+
if (bDistanceRef != null) {
28+
bDistanceRef.reversePainterSortStableCache ??= bz
29+
bz = bDistanceRef.reversePainterSortStableCache
30+
}
2131
if (aDistanceRef == null || bDistanceRef == null) {
2232
//default z comparison
23-
return a.z !== b.z ? b.z - a.z : a.id - b.id
33+
return az !== bz ? bz - az : a.id - b.id
2434
}
2535
if (aDistanceRef === bDistanceRef) {
2636
return compareOrderInfo((a.object as any)[orderInfoKey].value, (b.object as any)[orderInfoKey].value)

0 commit comments

Comments
 (0)