From b585ba19e5ff48cdaca9cec3cc47a318686bbe64 Mon Sep 17 00:00:00 2001
From: Dan Bagnell <daniel.p.bagnell@gmail.com>
Date: Fri, 14 Apr 2017 15:34:30 -0400
Subject: [PATCH 1/3] Fix initial billboard clustering.

---
 Source/DataSources/EntityCluster.js | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/Source/DataSources/EntityCluster.js b/Source/DataSources/EntityCluster.js
index c20ce4d3e895..39b8e3882f71 100644
--- a/Source/DataSources/EntityCluster.js
+++ b/Source/DataSources/EntityCluster.js
@@ -723,13 +723,24 @@ define([
         // If clustering is enabled before the label collection is updated,
         // the glyphs haven't been created so the screen space bounding boxes
         // are incorrect.
+        var commandList;
         if (defined(this._labelCollection) && this._labelCollection.length > 0 && this._labelCollection.get(0)._glyphs.length === 0) {
-            var commandList = frameState.commandList;
+            commandList = frameState.commandList;
             frameState.commandList = [];
             this._labelCollection.update(frameState);
             frameState.commandList = commandList;
         }
 
+        // If clustering is enabled before the billboard collection is updated,
+        // the images haven't been added to the image atlas so the screen space bounding boxes
+        // are incorrect.
+        if (defined(this._billboardCollection) && this._billboardCollection.length > 0 && !defined(this._billboardCollection.get(0).width)) {
+            commandList = frameState.commandList;
+            frameState.commandList = [];
+            this._billboardCollection.update(frameState);
+            frameState.commandList = commandList;
+        }
+
         if (this._enabledDirty) {
             this._enabledDirty = false;
             updateEnable(this);

From 32ec2317b1a9804b42e37f72bc26bb20cf7dbb52 Mon Sep 17 00:00:00 2001
From: Dan Bagnell <daniel.p.bagnell@gmail.com>
Date: Fri, 14 Apr 2017 15:45:40 -0400
Subject: [PATCH 2/3] Add clustering on first update tests.

---
 Specs/DataSources/EntityClusterSpec.js | 69 ++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)

diff --git a/Specs/DataSources/EntityClusterSpec.js b/Specs/DataSources/EntityClusterSpec.js
index 466323b543a4..dd77ff1f4e14 100644
--- a/Specs/DataSources/EntityClusterSpec.js
+++ b/Specs/DataSources/EntityClusterSpec.js
@@ -175,6 +175,29 @@ defineSuite([
         expect(cluster._clusterLabelCollection).not.toBeDefined();
     });
 
+    it('clusters billboards on first update', function() {
+        cluster = new EntityCluster();
+        cluster._initialize(scene);
+
+        var entity = new Entity();
+        var billboard = cluster.getBillboard(entity);
+        billboard.id = entity;
+        billboard.image = createBillboardImage();
+        billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5);
+
+        entity = new Entity();
+        billboard = cluster.getBillboard(entity);
+        billboard.id = entity;
+        billboard.image = createBillboardImage();
+        billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5);
+
+        cluster.enabled = true;
+        cluster.update(scene.frameState);
+
+        expect(cluster._clusterLabelCollection).toBeDefined();
+        expect(cluster._clusterLabelCollection.length).toEqual(1);
+    });
+
     it('clusters labels', function() {
         cluster = new EntityCluster();
         cluster._initialize(scene);
@@ -208,6 +231,29 @@ defineSuite([
         expect(cluster._clusterLabelCollection).not.toBeDefined();
     });
 
+    it('clusters labels on first update', function() {
+        cluster = new EntityCluster();
+        cluster._initialize(scene);
+
+        var entity = new Entity();
+        var label = cluster.getLabel(entity);
+        label.id = entity;
+        label.text = 'a';
+        label.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5);
+
+        entity = new Entity();
+        label = cluster.getLabel(entity);
+        label.id = entity;
+        label.text = 'b';
+        label.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5);
+
+        cluster.enabled = true;
+        cluster.update(scene.frameState);
+
+        expect(cluster._clusterLabelCollection).toBeDefined();
+        expect(cluster._clusterLabelCollection.length).toEqual(1);
+    });
+
     it('clusters points', function() {
         cluster = new EntityCluster();
         cluster._initialize(scene);
@@ -241,6 +287,29 @@ defineSuite([
         expect(cluster._clusterLabelCollection).not.toBeDefined();
     });
 
+    it('clusters points on first update', function() {
+        cluster = new EntityCluster();
+        cluster._initialize(scene);
+
+        var entity = new Entity();
+        var point = cluster.getPoint(entity);
+        point.id = entity;
+        point.pixelSize = 1;
+        point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5);
+
+        entity = new Entity();
+        point = cluster.getPoint(entity);
+        point.id = entity;
+        point.pixelSize = 1;
+        point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5);
+
+        cluster.enabled = true;
+        cluster.update(scene.frameState);
+
+        expect(cluster._clusterLabelCollection).toBeDefined();
+        expect(cluster._clusterLabelCollection.length).toEqual(1);
+    });
+
     it('clusters points that have labels', function() {
         cluster = new EntityCluster();
         cluster._initialize(scene);

From 30c5a98146fe7d35c8bf2300ccc0ff57389bd146 Mon Sep 17 00:00:00 2001
From: Dan Bagnell <daniel.p.bagnell@gmail.com>
Date: Fri, 14 Apr 2017 15:48:39 -0400
Subject: [PATCH 3/3] Update CHANGES.md.

---
 CHANGES.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/CHANGES.md b/CHANGES.md
index 586150da100a..b93d70feaacf 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -7,6 +7,7 @@ Change Log
 * Fixed issue with displaying `MapboxImageryProvider` default token error message [#5191](https://github.com/AnalyticalGraphicsInc/cesium/pull/5191)
 * Added a `depthFailMaterial` property to line entities, which is the material used to render the line when it fails the depth test. [#5160](https://github.com/AnalyticalGraphicsInc/cesium/pull/5160)
 * Upgrade FXAA to version 3.11. [#5200](https://github.com/AnalyticalGraphicsInc/cesium/pull/5200)
+* Fix billboards not initially clustering. [#5208](https://github.com/AnalyticalGraphicsInc/cesium/pull/5208)
 
 ### 1.32 - 2017-04-03