Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support layer opacity for vector points #166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cesium
62 changes: 55 additions & 7 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,48 @@ goog.require('olcs.core.OlLayerPrimitive');
};


/**
* Synchronizes the vector layer rendering properties (currently only
* 'opacity') to the given Cesium primitives.
* @param {!ol.layer.Vector} olLayer
* @param {!Cesium.PrimitiveCollection} csPrimitives
* @api
*/
olcs.core.updateCesiumPrimitives = function(olLayer, csPrimitives) {
var opacity = olLayer.getOpacity();
if (!goog.isDef(opacity)) {
opacity = 1;
}
csPrimitives.olLayerOpacity = opacity;
var i, bb, j, prim, geoms, geom, color;
for (i = csPrimitives.length - 1; i >= 0; --i) {
prim = csPrimitives.get(i);
if (prim instanceof Cesium.PrimitiveCollection) {
olcs.core.updateCesiumPrimitives(olLayer, prim);
} else {
if (!prim.ready) continue;
var attrs = prim.getGeometryInstanceAttributes("id");
if (!attrs) continue;
color = attrs.color;
if (color) {
//FIXME This currently overrides style opacity with layer opacity
color[3] = Cesium.Color.floatToByte(opacity);
attrs.color = color;
}
}
}
if (csPrimitives instanceof olcs.core.OlLayerPrimitive) {
var bbs = csPrimitives.context.billboards;
bbs.olLayerOpacity = opacity;
for (i = bbs.length - 1; i >= 0; --i) {
bb = bbs.get(i);
//FIXME Use Cesium.Color.fromAlpha after the next Cesium update
bb.color = new Cesium.Color(1.0, 1.0, 1.0, bb.olStyleOpacity * opacity);
}
}
};


/**
* Convert a 2D or 3D OpenLayers coordinate to Cesium.
* @param {ol.Coordinate} coordinate Ol3 coordinate.
Expand Down Expand Up @@ -480,7 +522,8 @@ goog.require('olcs.core.OlLayerPrimitive');
geometry: geometry,
attributes: {
color: Cesium.ColorGeometryInstanceAttribute.fromColor(color)
}
},
id: "id"
});
};

Expand All @@ -502,7 +545,7 @@ goog.require('olcs.core.OlLayerPrimitive');
}
var appearance = new Cesium.PerInstanceColorAppearance(options);

var instances = createInstance(geometry, color);
var instances = [createInstance(geometry, color)];

var primitive = new Cesium.Primitive({
// always update Cesium externs before adding a property
Expand Down Expand Up @@ -690,9 +733,10 @@ goog.require('olcs.core.OlLayerPrimitive');

var outlinePrimitive = new Cesium.Primitive({
// always update Cesium externs before adding a property
geometryInstances: new Cesium.GeometryInstance({
geometry: outlineGeometry
}),
geometryInstances: [new Cesium.GeometryInstance({
geometry: outlineGeometry,
id: "id"
})],
appearance: appearance
});

Expand Down Expand Up @@ -789,16 +833,20 @@ goog.require('olcs.core.OlLayerPrimitive');
var position = olcs.core.ol4326CoordinateToCesiumCartesian(center);
var color;
var opacity = imageStyle.getOpacity();
if (goog.isDef(opacity)) {
color = new Cesium.Color(1.0, 1.0, 1.0, opacity);
if (!goog.isDef(opacity)) {
opacity = 1;
}
//FIXME Use Cesium.Color.fromAlpha after the next Cesium update
color = new Cesium.Color(1.0, 1.0, 1.0,
opacity * billboards.olLayerOpacity);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The olLayerOpacity will be undefined on first synchronization.
The correct way is to pass the layer to the synchronization function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why opacity will be initialized to 1 above if it is undefined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 1?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 means fully opaque, which is the default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The layer opacity may be different from the default; that is why it is necessary to read the actual value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I think we had a misunderstanding. billboards.olLayerOpacity will never be undefined here. Only opacity can, and this is when no opacity is set on the style.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several code paths where billboards.olLayerOpacity will not be set.
See BillboardCollection instantiations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you got me confused. As I said above, opacity is initialized to 1 if undefined, and stored as billboards.olLayerOpacity. billboard.olStyleOpacity will never be undefined, because it is always set (and initialized to 1 if undefined when a billboard is created.

To avoid such confusion in the future, we should make adding unit tests a priority.

var bb = billboards.add({
// always update Cesium externs before adding a property
image: image,
color: color,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
position: position
});
bb.olStyleOpacity = opacity;
if (opt_newBillboardCallback) {
opt_newBillboardCallback(bb);
}
Expand Down
5 changes: 5 additions & 0 deletions src/vectorsynchronizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ olcs.VectorSynchronizer.prototype.createSingleCounterpart = function(olLayer) {
csPrimitives.show = olLayer.getVisible();
});

olLayer.on('change:opacity', function(e) {
olcs.core.updateCesiumPrimitives(olLayer, csPrimitives);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing visibility should not trigger the expensive modification of all opacities.

});
olcs.core.updateCesiumPrimitives(olLayer, csPrimitives);

var onAddFeature = function(feature) {
goog.asserts.assertInstanceof(olLayer, ol.layer.Vector);
var prim = csPrimitives.convert(olLayer, view, feature);
Expand Down