Commit 035d8ae
authored
Only calculate the world-space mesh center for an object when we know that object is transparent. (#23711)
PR #22041 made us use the center of the mesh AABB as the pivot point for
transparent sorting. This is fine in and of itself, but the way it was
implemented involved adding a matrix multiplication to
`collect_meshes_for_gpu_building` for all meshes, not just transparent
meshes. It also added several fields to `RenderMeshInstance` and related
instances for each mesh, even opaque ones. Since most meshes are opaque,
and `collect_meshes_for_gpu_building` is a performance-critical system,
this doesn't strike me as the right tradeoff.
This PR moves the calculation of the mesh center to
`queue_material_meshes`, to take place only after a mesh has been deemed
to be transparent. Not only does this make
`collect_meshes_for_gpu_building` faster, but it also allows us to
remove the various `center` fields, which stored redundant information.
Note that this comes with two tradeoffs:
1. The transparent sorting no longer takes a custom `Aabb` component on
the mesh into account. I doubt anybody was relying on this behavior.
2. We do have to calculate the AABB for the mesh when importing it to
the render world for the first time.
On `bevy_city --size 90 --no-cpu-culling`, this reduces the time spent
in `collect_meshes_for_gpu_building` after the loading screen from mean
84.85 ms, median 73.4 ms to mean 70.62 ms, median 72.5 ms.
Before this PR:
<img width="2756" height="1800" alt="Screenshot 2026-04-07 173043"
src="https://github.com/user-attachments/assets/3157b58c-b4f1-43db-8157-390e5c9c6ff0"
/>
After this PR:
<img width="2756" height="1800" alt="Screenshot 2026-04-07 172952"
src="https://github.com/user-attachments/assets/fda7100b-7695-4226-99e6-71b4c168f980"
/>1 parent 6e9522c commit 035d8ae
6 files changed
Lines changed: 134 additions & 66 deletions
File tree
- crates
- bevy_math/src
- bevy_pbr/src
- render
- bevy_render/src/mesh
- examples/shader_advanced
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
5 | 9 | | |
6 | 10 | | |
7 | 11 | | |
8 | 12 | | |
9 | 13 | | |
10 | 14 | | |
11 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
12 | 26 | | |
13 | 27 | | |
14 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
| |||
1114 | 1116 | | |
1115 | 1117 | | |
1116 | 1118 | | |
| 1119 | + | |
1117 | 1120 | | |
1118 | 1121 | | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
1119 | 1125 | | |
1120 | 1126 | | |
1121 | 1127 | | |
| |||
1227 | 1233 | | |
1228 | 1234 | | |
1229 | 1235 | | |
1230 | | - | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
1231 | 1248 | | |
1232 | 1249 | | |
1233 | 1250 | | |
| |||
1316 | 1333 | | |
1317 | 1334 | | |
1318 | 1335 | | |
1319 | | - | |
| 1336 | + | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
| 1345 | + | |
| 1346 | + | |
| 1347 | + | |
1320 | 1348 | | |
1321 | 1349 | | |
1322 | 1350 | | |
| |||
1784 | 1812 | | |
1785 | 1813 | | |
1786 | 1814 | | |
| 1815 | + | |
| 1816 | + | |
| 1817 | + | |
| 1818 | + | |
| 1819 | + | |
| 1820 | + | |
| 1821 | + | |
| 1822 | + | |
| 1823 | + | |
| 1824 | + | |
| 1825 | + | |
| 1826 | + | |
| 1827 | + | |
| 1828 | + | |
| 1829 | + | |
| 1830 | + | |
| 1831 | + | |
| 1832 | + | |
| 1833 | + | |
| 1834 | + | |
| 1835 | + | |
| 1836 | + | |
| 1837 | + | |
| 1838 | + | |
| 1839 | + | |
| 1840 | + | |
| 1841 | + | |
| 1842 | + | |
| 1843 | + | |
| 1844 | + | |
| 1845 | + | |
| 1846 | + | |
| 1847 | + | |
| 1848 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
835 | 835 | | |
836 | 836 | | |
837 | 837 | | |
838 | | - | |
839 | | - | |
840 | 838 | | |
841 | | - | |
842 | | - | |
843 | 839 | | |
844 | 840 | | |
845 | 841 | | |
| |||
858 | 854 | | |
859 | 855 | | |
860 | 856 | | |
861 | | - | |
862 | 857 | | |
863 | 858 | | |
864 | 859 | | |
| |||
896 | 891 | | |
897 | 892 | | |
898 | 893 | | |
899 | | - | |
900 | | - | |
901 | 894 | | |
902 | 895 | | |
903 | 896 | | |
904 | 897 | | |
905 | 898 | | |
906 | 899 | | |
907 | | - | |
908 | 900 | | |
909 | 901 | | |
910 | 902 | | |
| |||
1105 | 1097 | | |
1106 | 1098 | | |
1107 | 1099 | | |
1108 | | - | |
1109 | 1100 | | |
1110 | 1101 | | |
1111 | 1102 | | |
| |||
1115 | 1106 | | |
1116 | 1107 | | |
1117 | 1108 | | |
1118 | | - | |
1119 | 1109 | | |
1120 | 1110 | | |
1121 | 1111 | | |
| |||
1127 | 1117 | | |
1128 | 1118 | | |
1129 | 1119 | | |
1130 | | - | |
1131 | 1120 | | |
1132 | 1121 | | |
1133 | 1122 | | |
| |||
1137 | 1126 | | |
1138 | 1127 | | |
1139 | 1128 | | |
1140 | | - | |
1141 | 1129 | | |
1142 | 1130 | | |
1143 | 1131 | | |
| |||
1149 | 1137 | | |
1150 | 1138 | | |
1151 | 1139 | | |
1152 | | - | |
1153 | 1140 | | |
1154 | 1141 | | |
1155 | 1142 | | |
| |||
1166 | 1153 | | |
1167 | 1154 | | |
1168 | 1155 | | |
1169 | | - | |
1170 | 1156 | | |
1171 | 1157 | | |
1172 | 1158 | | |
1173 | 1159 | | |
1174 | 1160 | | |
1175 | | - | |
1176 | | - | |
1177 | 1161 | | |
1178 | 1162 | | |
1179 | 1163 | | |
| |||
1254 | 1238 | | |
1255 | 1239 | | |
1256 | 1240 | | |
1257 | | - | |
1258 | | - | |
1259 | | - | |
1260 | | - | |
1261 | | - | |
1262 | | - | |
1263 | | - | |
1264 | | - | |
| 1241 | + | |
| 1242 | + | |
1265 | 1243 | | |
1266 | 1244 | | |
1267 | | - | |
1268 | 1245 | | |
1269 | | - | |
1270 | | - | |
| 1246 | + | |
1271 | 1247 | | |
1272 | 1248 | | |
1273 | 1249 | | |
| |||
1290 | 1266 | | |
1291 | 1267 | | |
1292 | 1268 | | |
1293 | | - | |
1294 | 1269 | | |
1295 | 1270 | | |
1296 | 1271 | | |
| |||
1528 | 1503 | | |
1529 | 1504 | | |
1530 | 1505 | | |
1531 | | - | |
1532 | | - | |
1533 | | - | |
1534 | | - | |
1535 | | - | |
1536 | | - | |
1537 | 1506 | | |
1538 | 1507 | | |
1539 | 1508 | | |
1540 | | - | |
1541 | 1509 | | |
1542 | 1510 | | |
1543 | 1511 | | |
| |||
1549 | 1517 | | |
1550 | 1518 | | |
1551 | 1519 | | |
1552 | | - | |
1553 | | - | |
1554 | 1520 | | |
1555 | 1521 | | |
1556 | 1522 | | |
| |||
1590 | 1556 | | |
1591 | 1557 | | |
1592 | 1558 | | |
1593 | | - | |
1594 | | - | |
1595 | | - | |
1596 | 1559 | | |
1597 | 1560 | | |
1598 | 1561 | | |
| |||
1610 | 1573 | | |
1611 | 1574 | | |
1612 | 1575 | | |
1613 | | - | |
1614 | 1576 | | |
1615 | 1577 | | |
1616 | 1578 | | |
| |||
1690 | 1652 | | |
1691 | 1653 | | |
1692 | 1654 | | |
1693 | | - | |
1694 | | - | |
1695 | | - | |
1696 | | - | |
1697 | | - | |
1698 | 1655 | | |
1699 | 1656 | | |
1700 | 1657 | | |
| |||
1815 | 1772 | | |
1816 | 1773 | | |
1817 | 1774 | | |
1818 | | - | |
1819 | 1775 | | |
1820 | 1776 | | |
1821 | 1777 | | |
| |||
1836 | 1792 | | |
1837 | 1793 | | |
1838 | 1794 | | |
1839 | | - | |
1840 | 1795 | | |
1841 | 1796 | | |
1842 | 1797 | | |
| |||
1870 | 1825 | | |
1871 | 1826 | | |
1872 | 1827 | | |
1873 | | - | |
1874 | 1828 | | |
1875 | 1829 | | |
1876 | 1830 | | |
| 1831 | + | |
| 1832 | + | |
| 1833 | + | |
| 1834 | + | |
1877 | 1835 | | |
1878 | 1836 | | |
1879 | 1837 | | |
1880 | 1838 | | |
1881 | 1839 | | |
1882 | | - | |
1883 | | - | |
1884 | | - | |
1885 | | - | |
| 1840 | + | |
1886 | 1841 | | |
1887 | 1842 | | |
1888 | 1843 | | |
| |||
2178 | 2133 | | |
2179 | 2134 | | |
2180 | 2135 | | |
2181 | | - | |
2182 | 2136 | | |
2183 | 2137 | | |
2184 | 2138 | | |
| |||
2626 | 2580 | | |
2627 | 2581 | | |
2628 | 2582 | | |
2629 | | - | |
2630 | | - | |
2631 | | - | |
2632 | 2583 | | |
2633 | 2584 | | |
2634 | 2585 | | |
| |||
0 commit comments