Problem to retrieve geometry instance's attribute

Hi all,

How do I get the value of “geometryInstance.myValue” during the mouse over event? Below is the code I have now, it doesn’t work.

var widget = new Cesium.CesiumWidget(‘cesiumContainer’);

var instances = ;

var geometryInstance = new Cesium.GeometryInstance({

geometry: new Cesium.ExtentGeometry({

extent: new Extent(

CesiumMath.toRadians(170), //west

CesiumMath.toRadians(80), //south

CesiumMath.toRadians(180), //east

CesiumMath.toRadians(90) //north

)

}),

attributes:{

color: Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color(1, 0, 1, 0.5))

}

});

geometryInstance.myValue = 123; // adding a value to the geometry instance.

instances.push(geometryInstance);

widget.scene.getPrimitives().add(new Cesium.Primitive({

geometryInstances: instances,

appearance: new Cesium.PerInstanceColorAppearance(),

asynchronous: false

}));

var primitives = widget.scene.getPrimitives();

var handler = new Cesium.ScreenSpaceEventHandler(widget.scene.canvas);

handler.setInputAction(function(movement) {

var p = widget.scene.pick(movement.endPosition);

if (Cesium.defined§ && primitives.contains(p.primitive)) {

alert(p.primitive.myValue); //How do I retrieve the “geometryInstance.myValue” here?

}

}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

Thanks in advance.

Steven

Hi Steven,

To save memory, Primitive does not hold on to all the geometry or geometry instances once it creates WebGL resources, so it doesn’t have a reference to the geometry instance. Instead, provide an ‘id’ property (can be any type including an object) to the GeometryInstance constructor. The id is returned as part of the object from Scene.pick. See the picking section in the tutorial.

Patrick

Hi Patrick,

I am using ‘id’ property for distinguishing geometry instances already, and I need to store other data with each geometry instance. Do you have any suggestion of how should I give geometry instance a value and then I can get the value when mouse over event occurred?

Thank you so much for your time.

Steven

在 2014年3月26日星期三UTC-7上午11时49分16秒,Patrick Cozzi写道:

Steven,

Just make the id an object and store as many properties as you want. For example, if you had:

id : ‘some string’

Change it to:

id : {

oldValue : ‘some string’,

newProperty : ‘another string’,

anotherProperty : 5

}

Patrick

Hi Patrick,

I used ‘id’ property as a unique index when I create geometry instances, so when I want to change the color of a geometry instances, I can use “primitive.getGeometryInstanceAttributes(id)” to get the attribute of the geometry instance with the given id. If the ‘id’ property becomes a object, how can I get the attribute of a selected geometry instance? For example:

Below is how I create geometry instances now:

for(var i=0;i<5;i++){

instances.push(new GeometryInstance({

geometry: new ExtentGeometry({

extent: new Extent(

CesiumMath.toRadians(west), //west

CesiumMath.toRadians(south), //south

CesiumMath.toRadians(east), //east

CesiumMath.toRadians(north) //north

)

}),

attributes:{

color: ColorGeometryInstanceAttribute.fromColor(extentColor)

},

id: i

}));

}

So when I need to reset the color of a geometry instance, I can simply do this:

var attributes = primitive.getGeometryInstanceAttributes(0); //If the ‘id’ property becomes an object, how can I get the attribute of the geometry instance?

attributes.color = ColorGeometryInstanceAttribute.toValue(new Color(0,0,0,0));

Thanks again.

Steven

在 2014年3月27日星期四UTC-7上午5时31分29秒,Patrick Cozzi写道:

Hi Steven,

Primitive.getGeometryInstanceAttributes can take an object as an argument. If you want easy indexing, you could keep them in an array like this:

var ids = ;

for(var i=0;i<5;i++){

var id = {

someProperty : ‘some string’,

anotherProperty : 10

};

instances.push(new GeometryInstance({

geometry: new ExtentGeometry({

extent: new Extent(

CesiumMath.toRadians(west), //west

CesiumMath.toRadians(south), //south

CesiumMath.toRadians(east), //east

CesiumMath.toRadians(north) //north

)

}),

attributes:{

color: ColorGeometryInstanceAttribute.fromColor(extentColor)

},

id: id

}));

ids.push(id);

}

Later you can do this:

var attributes = primitive.getGeometryInstanceAttributes(ids[0]);

attributes.color = ColorGeometryInstanceAttribute.toValue(new Color(0,0,0,0));

Dan

Hi Patrick and Daniel,

Thanks so much, I’ve solved my problems.

Steven

在 2014年3月27日星期四UTC-7上午11时41分42秒,Daniel Bagnell写道: