Skip to content

add render item to user data during sorting#6

Merged
owenmech-nl merged 2 commits into
dev-8wfrom
renderitem-userdata
Jul 2, 2025
Merged

add render item to user data during sorting#6
owenmech-nl merged 2 commits into
dev-8wfrom
renderitem-userdata

Conversation

@owenmech-nl
Copy link
Copy Markdown

@owenmech-nl owenmech-nl commented Jul 1, 2025

Context

To support custom UI sorting, we need to be able to access the sort params of other UI objects in the comparator.

Testing

working with this passed into renderer.setTransparentSort():

// based on https://github.com/mrdoob/three.js/blob/dev/src/renderers/webgl/WebGLRenderLists.js#L27
function reversePainterSortWithUi(a: RenderListItem, b: RenderListItem) {
  // note(owenmech): to group UI elements together, sort them as if they were their root.
  const rootA = a.object.userData?.rootUi?.frame?.userData?.renderItem
  const rootB = b.object.userData?.rootUi?.frame?.userData?.renderItem
  a.z = rootA?.z ?? a.z
  b.z = rootB?.z ?? b.z

  if (a.groupOrder !== b.groupOrder) {
    return a.groupOrder - b.groupOrder
  } else if (a.renderOrder !== b.renderOrder) {
    return a.renderOrder - b.renderOrder
  } else if (rootA && rootA.id === rootB?.id) {
    // note(owenmech): when UI elements are in the same family, apply the custom UI sorting
    const aUiOrder = a.object.userData.uiOrder ?? 0
    const bUiOrder = b.object.userData.uiOrder ?? 0
    if (aUiOrder !== bUiOrder) {
      return aUiOrder - bUiOrder
    } else if (a.object.userData.isText !== b.object.userData.isText) {
      return a.object.userData.isText ? 1 : -1
    } else {
      return a.id - b.id
    }
  } else if (a.z !== b.z) {
    return b.z - a.z
  } else if (rootA?.id !== rootB?.id) {
    // note(owenmech): if two UI elements have different root UIs which happen to have the same Z
    // (or one is UI and one isn't), we must break the tie the same way the roots themselves would
    // to ensure that UI families are grouped together, as IDs are more-or-less arbitrary.
    return (rootA?.id ?? a.id) - (rootB?.id ?? b.id)
  } else {
    return a.id - b.id
  }
}

@owenmech-nl owenmech-nl force-pushed the renderitem-userdata branch from bd27470 to 8415f26 Compare July 1, 2025 18:24
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jul 1, 2025

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 337.69
78.76
337.69
78.78
+4 B
+23 B
WebGPU 557.18
154.29
556.95
154.24
-227 B
-47 B
WebGPU Nodes 556.1
154.07
556.3
154.01
+202 B
-56 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 468.91
113.42
469.15
113.52
+241 B
+106 B
WebGPU 632.83
171.3
632.97
171.29
+145 B
-15 B
WebGPU Nodes 587.96
160.69
588.02
160.6
+63 B
-87 B

@owenmech-nl owenmech-nl marked this pull request as ready for review July 1, 2025 18:28
@owenmech-nl
Copy link
Copy Markdown
Author

I'll pick this onto r172-8w, then build/pack that and throw it on the CDN

@owenmech-nl
Copy link
Copy Markdown
Author

VIsual approval from @dattanchu

@owenmech-nl owenmech-nl merged commit 0782879 into dev-8w Jul 2, 2025
8 of 9 checks passed
@owenmech-nl owenmech-nl deleted the renderitem-userdata branch July 2, 2025 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant