-
Notifications
You must be signed in to change notification settings - Fork 327
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
base: master
Are you sure you want to change the base?
Conversation
@ahocevar, I will look at your code next week when I am back from FOSDEM. |
I'm guessing you'll need to define your OL opacity attributes in the Cesium externs to avoid making the compiler angry. Otherwise the approach looks sound to me. |
} | ||
color = new Cesium.Color(1.0, 1.0, 1.0, | ||
opacity * billboards.olLayerOpacity); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 1
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@ahocevar, please see my comments. |
@gberaudo I'll see if I can make this work for other geometry types as well. Points were a lower hanging fruit than the rest though. |
Did you run |
src/vectorsynchronizer.js
Outdated
} | ||
bbs.olLayerOpacity = opacity; | ||
var i, bb; | ||
for (i = bbs.length - 1; i >= 0; --i) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you iterate in reverse order?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Starting the iterator with bbs.length
saves me from having to store it in a separate variable. This is a small optimization I use frequently when the order of iteration does not matter.
d3ed6f5
to
34c74ed
Compare
@gberaudo The primitives are now updated by an ol.core function. I added FIXMEs for switching to Cesium.Color.fromAlpha once we update Cesium again. Making this work for geometry types other than point will be more effort, so I'd appreciate if we can get this in just for points for now. |
olLayer.on('change:visible', function(e) { | ||
csPrimitives.show = olLayer.getVisible(); | ||
olLayer.on(['change:visible', 'change:opacity'], function(e) { | ||
olcs.core.updateCesiumPrimitives(olLayer, csPrimitives); |
There was a problem hiding this comment.
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.
@ahocevar, I feel it would be better to merge this PR in a dedicated branch of Otherwise, it will look strange with only the points opacity changing. Is it OK for you? |
34c74ed
to
7d0944b
Compare
I do not see a way to change the opacity of other geometry types, other than recreating all primitives. |
You have to use Primitive#getGeometryInstanceAttributes and modify the color with the new opacity, but keep in mind that function will fail until the primitive is ready to render. We're also not setting the |
This is what I tried, but after rendering the geometry instances of all primitives are undefined, so nothing can be changed. |
@ahocevar, here is a working Sandcastle example Cesium.Math.setRandomNumberSeed(1234);
var viewer = new Cesium.Viewer('cesiumContainer');
var scene = viewer.scene;
var primitives = scene.primitives;
var solidWhite = Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.WHITE);
var id = 'anid';
var rectangle = Cesium.Rectangle.fromDegrees(-92.0, 20.0, -86.0, 27.0);
var rectangleOutlineInstance = new Cesium.GeometryInstance({
geometry : new Cesium.RectangleOutlineGeometry({
rectangle : rectangle
}),
attributes : {
color : solidWhite
},
id: id
});
var ro = primitives.add(new Cesium.Primitive({
geometryInstances : [rectangleOutlineInstance],
appearance : new Cesium.PerInstanceColorAppearance({
flat : true,
translucent : true,
renderState : {
lineWidth : Math.min(4.0, scene.maximumAliasedLineWidth)
}
})
}));
ro.readyPromise.then(function() {
var i = 0;
setInterval(function() {
var attrs = ro.getGeometryInstanceAttributes(id);
//var color = attrs.color;
attrs.color[3] = i;
attrs.color = attrs.color;
i = i + 20;
if (i > 255) {
i = 0;
}
}, 100);
}); |
I had found a similar example and implemented accordingly. As I said, the problem is that the primitive seems to forget about its geometry instances. I can push my non working code if you want to take a look. |
I just saw that we're not assigning an array to geometryInstances, but a single geometry instance. This is the cause of the problem. |
At least it is part of the problem. The correct approach is probably really to assign ids. |
Do you know if Cesium uses the geometry instance id for anything other than lookup with |
@ahocevar, have a look to gberaudo@84f566d for how to modify opacity after the primitive is rendered. |
Thanks @gberaudo, that works well. I'll try to complete this pull request when I get to it. Might be a long time from now though. |
7d0944b
to
9caf98e
Compare
See #144 (comment). This adds support for layer opacity on vector points. Layer and style opacity are combined on the billboard level.