Scene 中的几何。几何可以来自单个 GeometryInstance
(如下面的示例 1),也可以来自一组实例,即使这些几何来自不同的
几何类型,例如 RectangleGeometry 和 EllipsoidGeometry(如代码示例 2 所示)。
图元将几何体实例与描述完整着色的 Appearance 结合在一起,包括
Material 和 RenderState。粗略地说,几何体实例定义了结构与位置,
而 appearance 定义了视觉特征。将几何与 appearance 解耦,使我们可以混用
大多数几何与 appearance,并独立地添加新的几何或 appearance。
将多个实例合并到一个图元中称为批处理(batching),可显著提升静态数据的性能。
实例可以被单独拾取;Scene#pick 会返回它们的 GeometryInstance#id。使用
PerInstanceColorAppearance 等逐实例 appearance,每个实例还可以拥有唯一的颜色。
Geometry 可以在 Web Worker 或主线程上创建并批处理。前两个示例
展示了如何通过将几何的描述在 Web Worker 上创建几何。第三个示例
展示了如何通过显式调用 createGeometry 方法在主线程上创建几何。
| Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
optional
Object with the following properties:
|
Examples:
// 1. Draw a translucent ellipse on the surface with a checkerboard pattern
const instance = new Cesium.GeometryInstance({
geometry : new Cesium.EllipseGeometry({
center : Cesium.Cartesian3.fromDegrees(-100.0, 20.0),
semiMinorAxis : 500000.0,
semiMajorAxis : 1000000.0,
rotation : Cesium.Math.PI_OVER_FOUR,
vertexFormat : Cesium.VertexFormat.POSITION_AND_ST
}),
id : 'object returned when this instance is picked and to get/set per-instance attributes'
});
scene.primitives.add(new Cesium.Primitive({
geometryInstances : instance,
appearance : new Cesium.EllipsoidSurfaceAppearance({
material : Cesium.Material.fromType('Checkerboard')
})
}));
// 2. Draw different instances each with a unique color
const rectangleInstance = new Cesium.GeometryInstance({
geometry : new Cesium.RectangleGeometry({
rectangle : Cesium.Rectangle.fromDegrees(-140.0, 30.0, -100.0, 40.0),
vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
}),
id : 'rectangle',
attributes : {
color : new Cesium.ColorGeometryInstanceAttribute(0.0, 1.0, 1.0, 0.5)
}
});
const ellipsoidInstance = new Cesium.GeometryInstance({
geometry : new Cesium.EllipsoidGeometry({
radii : new Cesium.Cartesian3(500000.0, 500000.0, 1000000.0),
vertexFormat : Cesium.VertexFormat.POSITION_AND_NORMAL
}),
modelMatrix : Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
Cesium.Cartesian3.fromDegrees(-95.59777, 40.03883)), new Cesium.Cartesian3(0.0, 0.0, 500000.0), new Cesium.Matrix4()),
id : 'ellipsoid',
attributes : {
color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.AQUA)
}
});
scene.primitives.add(new Cesium.Primitive({
geometryInstances : [rectangleInstance, ellipsoidInstance],
appearance : new Cesium.PerInstanceColorAppearance()
}));
// 3. Create the geometry on the main thread.
scene.primitives.add(new Cesium.Primitive({
geometryInstances : new Cesium.GeometryInstance({
geometry : Cesium.EllipsoidGeometry.createGeometry(new Cesium.EllipsoidGeometry({
radii : new Cesium.Cartesian3(500000.0, 500000.0, 1000000.0),
vertexFormat : Cesium.VertexFormat.POSITION_AND_NORMAL
})),
modelMatrix : Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
Cesium.Cartesian3.fromDegrees(-95.59777, 40.03883)), new Cesium.Cartesian3(0.0, 0.0, 500000.0), new Cesium.Matrix4()),
id : 'ellipsoid',
attributes : {
color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.AQUA)
}
}),
appearance : new Cesium.PerInstanceColorAppearance(),
asynchronous : false
}));
See:
Members
-
Default Value:
true
appearance : Appearance
Appearance。每个几何体
实例都使用相同的 appearance 着色。某些 appearance(例如
PerInstanceColorAppearance)允许为每个实例指定唯一的
属性。
-
Default Value:
undefined
-
Default Value:
true
true 时,几何体顶点会被压缩以节省内存。
-
Default Value:
true
true 时,渲染器会根据图元命令的包围体进行视锥剔除和地平线剔除。
将其设为 false 可获得少量性能提升
(如果你手动剔除图元)。
-
Default Value:
true
为图元中的每个绘制命令绘制包围球。
-
Default Value:
false
depthFailAppearance : Appearance
Appearance。每个几何体
实例都使用相同的 appearance 着色。某些 appearance(例如
PerInstanceColorAppearance)允许为每个实例指定唯一的
属性。
使用需要颜色属性的 appearance(如 PerInstanceColorAppearance)时, 请改用 depthFailColor 逐实例属性。
需要 EXT_frag_depth WebGL 扩展才能正确渲染。如果不支持该扩展, 可能会出现伪影。
-
Default Value:
undefined
readonly geometryInstances : Array.<GeometryInstance>|GeometryInstance
options.releaseGeometryInstances 设为 true,
则可能为 undefined。
在图元渲染后更改此属性无效。
-
Default Value:
undefined
-
Default Value:
false
modelMatrix : Matrix4
Transforms.eastNorthUpToFixedFrame 返回的矩阵)可使用局部参考系。
此属性仅在 3D 模式下受支持。
-
Default Value:
Matrix4.IDENTITY
Example:
const origin = Cesium.Cartesian3.fromDegrees(-95.0, 40.0, 200000.0);
p.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(origin);
Primitive#update
时会渲染该图元。
Example:
// Wait for a primitive to become ready before accessing attributes
const removeListener = scene.postRender.addEventListener(() => {
if (!frustumPrimitive.ready) {
return;
}
const attributes = primitive.getGeometryInstanceAttributes('an id');
attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue(Cesium.Color.AQUA);
removeListener();
});
true 时,为节省内存,图元不会保留对输入 geometryInstances 的引用。
-
Default Value:
true
-
Default Value:
ShadowMode.DISABLED
-
Default Value:
true
true 时,几何体顶点会针对顶点着色器前后缓存进行优化。
-
Default Value:
true
Methods
对象一旦被销毁就不应再使用;调用 isDestroyed 之外的任何函数
都会抛出 DeveloperError 异常。因此,
应像示例中那样将返回值(undefined)赋给该对象。
Throws:
-
DeveloperError : 此对象已被销毁,即已调用 destroy()。
Example:
e = e && e.destroy();
See:
GeometryInstance 的可修改逐实例属性。
| Name | Type | Description |
|---|---|---|
id |
* |
GeometryInstance 的 id。 |
Returns:
Throws:
-
DeveloperError : 在调用 getGeometryInstanceAttributes 之前必须先调用 update。
Example:
const attributes = primitive.getGeometryInstanceAttributes('an id');
attributes.color = Cesium.ColorGeometryInstanceAttribute.toValue(Cesium.Color.AQUA);
attributes.show = Cesium.ShowGeometryInstanceAttribute.toValue(true);
attributes.distanceDisplayCondition = Cesium.DistanceDisplayConditionGeometryInstanceAttribute.toValue(100.0, 10000.0);
attributes.offset = Cesium.OffsetGeometryInstanceAttribute.toValue(Cartesian3.IDENTITY);
如果此对象已被销毁,则不应再使用它;调用 isDestroyed 之外的任何函数
都会抛出 DeveloperError 异常。
Returns:
true 表示此对象已被销毁;否则为 false。
See:
Throws:
-
DeveloperError : 所有实例几何必须拥有相同的 primitiveType。
-
DeveloperError : Appearance 和 material 拥有同名的 uniform。
-
DeveloperError : Primitive.modelMatrix 仅在 3D 模式下受支持。
-
RuntimeError : 渲染具有逐实例属性的图元需要顶点纹理获取(vertex texture fetch)支持。顶点纹理图像单元的最大数量必须大于零。
