改变Dart中铯几何实例的颜色

时间:2014-11-13 06:12:30

标签: javascript dart cesium

我正在尝试更新Cesium几何实例的颜色。我目前在javascript中使用代码但是当我将其转换为dart时,它不再有效。当我尝试更改颜色时的结果是半透明(在PerInstanceColorAppearance中)设置为false时为黑色圆圈,如果半透明设置为true则不显示任何内容。我认为这与我的飞镖转换有关,但我无法确定问题所在。

这是正确运行的javascript代码:

var primitive;

<script>
    document.getElementById("test").onclick = function() {myFunction()};

    function myFunction() {
        var attributes = primitive.getGeometryInstanceAttributes('circle');
        attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue(Cesium.Color.fromRandom({alpha : 1.0}));
    }
</script>

<script>
    var viewer = new Cesium.Viewer('cesiumContainer');
    var scene = viewer.scene;

    var circleInstance = new Cesium.GeometryInstance({
        geometry : new Cesium.CircleGeometry({
            center : Cesium.Cartesian3.fromDegrees(-95.0, 43.0),
            radius : 250000.0,
            vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
        }),
        attributes : {
            color : Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color(1.0, 0.0, 0.0, 0.5))
        },
        id: 'circle'
    });

    primitive = new Cesium.Primitive({geometryInstances : circleInstance, 
                                     releaseGeometryInstances : false, 
                                     appearance : new Cesium.PerInstanceColorAppearance({
                                                                 translucent : false, 
                                                                 closed : true})});
    scene.primitives.add(primitive);
</script>

这是我对dart的转换:

var testPrimitive;
bool myTest = false;

void drawTestCircle() {
    if(!myTest) {
        var scene = _viewer['scene'];

        var color = new JsObject(context['Cesium']['Color'], [1.0, 0.0, 0.0, 0.5]);
        var colorInstance = context['Cesium']['ColorGeometryInstanceAttribute'].callMethod('fromColor', [color]);

        var vertexFormat = context['Cesium']['PerInstanceColorAppearance']['VERTEX_FORMAT'];
        var center = context['Cesium']['Cartesian3'].callMethod('fromDegrees', [-95.0, 43.0]);
        var geometry = new JsObject(context['Cesium']['CircleGeometry'], 
              [new JsObject.jsify({'center' : center, 
                                   'radius' : 250000.0, 
                                   'vertexFormat' : vertexFormat})]);

        var circleInstance = new JsObject(context['Cesium']['GeometryInstance'], 
               [new JsObject.jsify({'geometry' : geometry, 
                                    'attributes' : new JsObject.jsify({'color' : colorInstance}),
                                    'id' : 'circle'})]);

        var appearance = new JsObject(context['Cesium']['PerInstanceColorAppearance'], 
               [new JsObject.jsify({'translucent' : false, 'closed' : true})]);
        testPrimitive = new JsObject(context['Cesium']['Primitive'], 
               [new JsObject.jsify({'geometryInstances' : circleInstance, 
                                    'releaseGeometryInstances' : true, 
                                    'appearance' : appearance})]);

        scene['primitives'].callMethod('add', [testPrimitive]);
        myTest = true;
    } else {
        var attributes = testPrimitive.callMethod('getGeometryInstanceAttributes', ['circle']);

        var color = context['Cesium']['Color'].callMethod('fromRandom', [new JsObject.jsify({'alpha' : 1.0})]);
        attributes['color'] = context['Cesium']['ColorGeometryInstanceAttribute'].callMethod('toValue', [color]);
    }
}

在“测试”按钮上单击调用drawTestCircle。我在Cesium上使用的示例代码可以在“更新每个实例属性”下找到http://cesiumjs.org/2013/11/04/Geometry-and-Appearances/。最初我认为它与在绘制之后尝试访问testPrimitive有关,但我相信设置“releaseGeometryInstances”应该允许我操作它。任何想法或建议将不胜感激。

0 个答案:

没有答案