I'm trying to change polygon color to Red in Left_Click event, but I get error for following code.
viewer.entities.getById("SOMEID").polygon.material.color = Cesium.Color.RED;
TypeError: Cannot set property color of #<Object> which has only a getter
Cesium 1.48 is used.
omar
September 13, 2018, 3:06pm
2
Could you share a bit more of your code or a Sandcastle example? I just tried this out and it seems to work:
var viewer = new Cesium.Viewer(‘cesiumContainer’);
viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(movement) {
viewer.entities.getById(“1”).polygon.material.color = Cesium.Color.BLUE;
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
var redPolygon = viewer.entities.add({
name : ‘Red polygon on surface’,
id: ‘1’,
polygon : {
hierarchy : Cesium.Cartesian3.fromDegreesArray([-115.0, 37.0,
-115.0, 32.0,
-107.0, 33.0,
-102.0, 31.0,
-102.0, 35.0]),
material : Cesium.Color.RED
}
});
viewer.zoomTo(viewer.entities);
``
Here’s a running example .
When I tried your example it worked fine. But when I try to do the same, it doesn't work.
When I logged your polygon.material.color, I see following.
omar
September 17, 2018, 2:28pm
4
Can you show how you’re creating your entities/polygons?
The other thing you could try is using a CallbackProperty for the color, to make sure Cesium knows it’s dynamic and can change. This example shows a callback property being used for a position but it can work for color (or any dynamic other property):
Here is sample code.
var parentTest = viewer.entities.add(new Cesium.Entity());
var entity = viewer.entities.add({
id : "someId",
parent : parentTest,
customVar1 : Value1,
customVar1 : Value2,
description : "testDescription",
polygon : {
hierarchy : new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(eval("[COORDINATES FOR POLYGON]"))),
material : Cesium.Color.BLUE
}
});
omar
September 17, 2018, 3:03pm
6
Yeah, that looks like the same thing I’ve got it. Did you try a CallbackProperty for the material?
Not yet, I'll try it in an hour and let you know.
I get Cesium error when I try this. I guess I messed up with callBack implementation.
var parentTest = viewer.entities.add(new Cesium.Entity());
var entity = viewer.entities.add({
id : "someId",
parent : parentTest,
customVar1 : Value1,
customVar1 : Value2,
description : "testDescription",
polygon : {
hierarchy : new Cesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArrayHeights(eval("[COORDINATES FOR POLYGON]"))),
material : new Cesium.CallbackProperty(function(material, result) {
console.log("In Here : ", material);
return Cesium.Color.BLUE;
}, isConstant),
}
});
omar
September 18, 2018, 2:12pm
9
My bad, it looks like you need to wrap it in a ColorMaterialProperty so it looks like this:
material : new Cesium.ColorMaterialProperty(
new Cesium.CallbackProperty(function(time, result) {
return color;
}, false))
``
Here’s a running example . Click to change the color.
I figured out the issue, At least that's what I think it could be.
When I create entity, If I show/hide it , and then try to change the color, it was throwing the error.
Then Instead of show/hide, I remove that element and create again, that way whenever I click on it I was able to change the color.
Thank you for help, learned new things for callback.
omar
September 21, 2018, 5:11pm
11
Thanks for digging deeper into this! You’re right, I was able to reproduce it, and it looks like this has just been fixed with this PR:
https://github.com/AnalyticalGraphicsInc/cesium/pull/7053
So once 1.50 comes out you should be able to just change the color without having to remove and recreate the entity. This will be far better for performance compared to using a CallbackProperty especially if you have thousands of entities.
I just hit this issue too on 1.49. I am going to try 1.50 and hope it fixes it. We are changing color and doing show/hide a lot on existing Entities.