From 0ba1da78b636775d42d24040ad5bed4aaa9ceb04 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Wed, 4 Feb 2015 10:39:58 +0100 Subject: [PATCH 1/2] Support layer opacity for vector points --- src/core.js | 32 ++++++++++++++++++++++++++++++-- src/vectorsynchronizer.js | 5 +++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/core.js b/src/core.js index 6d9b85ca4..7aa4b4739 100644 --- a/src/core.js +++ b/src/core.js @@ -412,6 +412,30 @@ 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) { + //FIXME Make this work for all geometry types, not just points + var bbs = csPrimitives.context.billboards; + var opacity = olLayer.getOpacity(); + if (!goog.isDef(opacity)) { + opacity = 1; + } + bbs.olLayerOpacity = opacity; + var i, bb; + 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. @@ -789,9 +813,12 @@ 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); var bb = billboards.add({ // always update Cesium externs before adding a property image: image, @@ -799,6 +826,7 @@ goog.require('olcs.core.OlLayerPrimitive'); verticalOrigin: Cesium.VerticalOrigin.BOTTOM, position: position }); + bb.olStyleOpacity = opacity; if (opt_newBillboardCallback) { opt_newBillboardCallback(bb); } diff --git a/src/vectorsynchronizer.js b/src/vectorsynchronizer.js index cfcddf19c..dfcca59fb 100644 --- a/src/vectorsynchronizer.js +++ b/src/vectorsynchronizer.js @@ -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); + }); + olcs.core.updateCesiumPrimitives(olLayer, csPrimitives); + var onAddFeature = function(feature) { goog.asserts.assertInstanceof(olLayer, ol.layer.Vector); var prim = csPrimitives.convert(olLayer, view, feature); From 9caf98e2ae39b6c033676c1fda8e9f847f91db86 Mon Sep 17 00:00:00 2001 From: gberaudo Date: Thu, 12 Feb 2015 13:48:58 +0100 Subject: [PATCH 2/2] Modify opacity after primitive was rendered --- cesium | 2 +- src/core.js | 46 +++++++++++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/cesium b/cesium index bad8b17f7..9f5fa6a23 160000 --- a/cesium +++ b/cesium @@ -1 +1 @@ -Subproject commit bad8b17f7899233497e51eb674e430356e41f4c5 +Subproject commit 9f5fa6a2337b854caae08a8b45c9a5045d4b43c2 diff --git a/src/core.js b/src/core.js index 7aa4b4739..ee74e579d 100644 --- a/src/core.js +++ b/src/core.js @@ -420,18 +420,36 @@ goog.require('olcs.core.OlLayerPrimitive'); * @api */ olcs.core.updateCesiumPrimitives = function(olLayer, csPrimitives) { - //FIXME Make this work for all geometry types, not just points - var bbs = csPrimitives.context.billboards; var opacity = olLayer.getOpacity(); if (!goog.isDef(opacity)) { opacity = 1; } - bbs.olLayerOpacity = opacity; - var i, bb; - 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); + 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); + } } }; @@ -504,7 +522,8 @@ goog.require('olcs.core.OlLayerPrimitive'); geometry: geometry, attributes: { color: Cesium.ColorGeometryInstanceAttribute.fromColor(color) - } + }, + id: "id" }); }; @@ -526,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 @@ -714,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 });