Skip to content

Commit 5d9cbb7

Browse files
authored
Merge pull request #12922 from Beilinson/fix-hidden-label-background
Fix background updating when label hidden #12138
2 parents b497b84 + ecdabc4 commit 5d9cbb7

File tree

4 files changed

+76
-36
lines changed

4 files changed

+76
-36
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes :wrench:
88

99
- Fixes an event bug following recent changes, where adding a new listener during an event callback caused an infinite loop. [#12955](https://github.com/CesiumGS/cesium/pull/12955)
10+
- Fix issues with label background when updating properties while `label.show` is `false`. [#12138](https://github.com/CesiumGS/cesium/issues/12138)
1011

1112
## 1.134 - 2025-10-01
1213

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
433433
- [宋时旺](https://github.com/BlockCnFuture)
434434
- [Marco Zhan](https://github.com/marcoYxz)
435435
- [Mikhail Porotkin](https://github.com/porotkin)
436+
- [Adam Beili](https://github.com/Beilinson)

packages/engine/Source/Scene/LabelCollection.js

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -150,47 +150,25 @@ function rebindAllGlyphs(labelCollection, label) {
150150
// presize glyphs to match the new text length
151151
glyphs.length = textLength;
152152

153-
const showBackground =
154-
label.show && label._showBackground && text.split("\n").join("").length > 0;
155153
let backgroundBillboard = label._backgroundBillboard;
156154
const backgroundBillboardCollection =
157155
labelCollection._backgroundBillboardCollection;
158-
if (!showBackground) {
159-
if (defined(backgroundBillboard)) {
160-
backgroundBillboardCollection.remove(backgroundBillboard);
161-
label._backgroundBillboard = backgroundBillboard = undefined;
162-
}
163-
} else {
164-
if (!defined(backgroundBillboard)) {
165-
backgroundBillboard = getWhitePixelBillboard(
166-
backgroundBillboardCollection,
167-
labelCollection,
168-
);
169-
label._backgroundBillboard = backgroundBillboard;
170-
}
171156

172-
backgroundBillboard.color = label._backgroundColor;
173-
backgroundBillboard.show = label._show;
174-
backgroundBillboard.position = label._position;
175-
backgroundBillboard.eyeOffset = label._eyeOffset;
176-
backgroundBillboard.pixelOffset = label._pixelOffset;
177-
backgroundBillboard.horizontalOrigin = HorizontalOrigin.LEFT;
178-
backgroundBillboard.verticalOrigin = label._verticalOrigin;
179-
backgroundBillboard.heightReference = label._heightReference;
180-
backgroundBillboard.scale = label.totalScale;
181-
backgroundBillboard.pickPrimitive = label;
182-
backgroundBillboard.id = label._id;
183-
backgroundBillboard.translucencyByDistance = label._translucencyByDistance;
184-
backgroundBillboard.pixelOffsetScaleByDistance =
185-
label._pixelOffsetScaleByDistance;
186-
backgroundBillboard.scaleByDistance = label._scaleByDistance;
187-
backgroundBillboard.distanceDisplayCondition =
188-
label._distanceDisplayCondition;
189-
backgroundBillboard.disableDepthTestDistance =
190-
label._disableDepthTestDistance;
191-
backgroundBillboard.clusterShow = label.clusterShow;
157+
// Create backgroundBillboard if needed
158+
if (label._showBackground && !defined(backgroundBillboard)) {
159+
backgroundBillboard = getWhitePixelBillboard(
160+
backgroundBillboardCollection,
161+
labelCollection,
162+
);
163+
label._backgroundBillboard = backgroundBillboard;
192164
}
193165

166+
updateBackgroundBillboard(
167+
backgroundBillboardCollection,
168+
label,
169+
backgroundBillboard,
170+
);
171+
194172
const glyphBillboardCollection = labelCollection._glyphBillboardCollection;
195173
const glyphTextureCache = glyphBillboardCollection.billboardTextureCache;
196174
const textDimensionsCache = labelCollection._textDimensionsCache;
@@ -335,6 +313,47 @@ function rebindAllGlyphs(labelCollection, label) {
335313
label._repositionAllGlyphs = true;
336314
}
337315

316+
function updateBackgroundBillboard(
317+
backgroundBillboardCollection,
318+
label,
319+
backgroundBillboard,
320+
) {
321+
if (!defined(backgroundBillboard)) {
322+
return;
323+
}
324+
const showBackground =
325+
label.show &&
326+
label._showBackground &&
327+
label._renderedText.split("\n").join("").length > 0;
328+
// Label is shown and background is hidden - remove the background billboard
329+
if (label.show && !showBackground) {
330+
backgroundBillboardCollection.remove(backgroundBillboard);
331+
label._backgroundBillboard = backgroundBillboard = undefined;
332+
return;
333+
}
334+
335+
backgroundBillboard.color = label._backgroundColor;
336+
backgroundBillboard.show = label._show;
337+
backgroundBillboard.position = label._position;
338+
backgroundBillboard.eyeOffset = label._eyeOffset;
339+
backgroundBillboard.pixelOffset = label._pixelOffset;
340+
backgroundBillboard.horizontalOrigin = HorizontalOrigin.LEFT;
341+
backgroundBillboard.verticalOrigin = label._verticalOrigin;
342+
backgroundBillboard.heightReference = label._heightReference;
343+
backgroundBillboard.scale = label.totalScale;
344+
backgroundBillboard.pickPrimitive = label;
345+
backgroundBillboard.id = label._id;
346+
backgroundBillboard.translucencyByDistance = label._translucencyByDistance;
347+
backgroundBillboard.pixelOffsetScaleByDistance =
348+
label._pixelOffsetScaleByDistance;
349+
backgroundBillboard.scaleByDistance = label._scaleByDistance;
350+
backgroundBillboard.distanceDisplayCondition =
351+
label._distanceDisplayCondition;
352+
backgroundBillboard.disableDepthTestDistance =
353+
label._disableDepthTestDistance;
354+
backgroundBillboard.clusterShow = label.clusterShow;
355+
}
356+
338357
function calculateWidthOffset(lineWidth, horizontalOrigin, backgroundPadding) {
339358
if (horizontalOrigin === HorizontalOrigin.CENTER) {
340359
return -lineWidth / 2;
@@ -507,7 +526,6 @@ function repositionAllGlyphs(label) {
507526
glyphPixelOffset.y = 0;
508527
}
509528
glyphPixelOffset.y = glyphPixelOffset.y * scale;
510-
511529
backgroundBillboard.width = totalLineWidth;
512530
backgroundBillboard.height = totalLineHeight;
513531
backgroundBillboard._setTranslate(glyphPixelOffset);

packages/engine/Specs/Scene/LabelCollectionSpec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,26 @@ describe("Scene/LabelCollection", function () {
11481148
});
11491149

11501150
describe("Label", function () {
1151+
it("should update background billboard when updating label properties while label is hidden", async function () {
1152+
const label = labels.add({
1153+
text: "abc",
1154+
showBackground: true,
1155+
});
1156+
1157+
await allLabelsReady();
1158+
expect(labels._backgroundBillboardCollection.length).toEqual(1);
1159+
1160+
const backgroundBillboard = label._backgroundBillboard;
1161+
const { width } = backgroundBillboard;
1162+
1163+
label.show = false;
1164+
label.text = "abcde";
1165+
expect(labels._backgroundBillboardCollection.length).toEqual(1);
1166+
1167+
label.show = true;
1168+
scene.renderForSpecs();
1169+
expect(backgroundBillboard.width).toBeGreaterThan(width);
1170+
});
11511171
it("can compute screen space position", function () {
11521172
labels.clampToPixel = false;
11531173
const label = labels.add({

0 commit comments

Comments
 (0)