English中文

Primitive

new Cesium.Primitive(options)

基元代表几何体 A primitive represents geometry in the Scene。几何形状可以来自单个 . The geometry can be from a single GeometryInstance 如下面的示例 1 所示,或者来自实例数组,即使几何图形来自不同的几何类型,例如 as shown in example 1 below, or from an array of instances, even if the geometry is from different geometry types, e.g., an RectangleGeometry 和一个 and an EllipsoidGeometry 如代码示例 2 所示。 as shown in Code Example 2.

基元将几何实例与 A primitive combines geometry instances with an Appearance 描述了完整的阴影,包括 that describes the full shading, including Material and RenderState。粗略地说,几何实例定义了结构和位置,外观定义了视觉特征。几何图形和外观的解耦使我们能够混合和匹配它们中的大多数,并相互独立地添加新的几何图形或外观。 . Roughly, the geometry instance defines the structure and placement, and the appearance defines the visual characteristics. Decoupling geometry and appearance allows us to mix and match most of them and add a new geometry or appearance independently of each other.

将多个实例组合成一个原语称为批处理,可显着提高静态数据的性能。可以单独选取实例; Combining multiple instances into one primitive is called batching, and significantly improves performance for static data. Instances can be individually picked; Scene#pick 返回他们的 returns their GeometryInstance#id。使用每个实例的外观,例如 . Using per-instance appearances like PerInstanceColorAppearance,每个实例也可以有唯一的颜色。 , each instance can also have a unique color.

Geometry 可以在网络工作者或主线程上创建和批处理。前两个示例显示了将使用几何描述在 Web Worker 上创建的几何。第三个示例展示了如何通过显式调用在主线程上创建几何图形 can either be created and batched on a web worker or the main thread. The first two examples show geometry that will be created on a web worker by using the descriptions of the geometry. The third example shows how to create the geometry on the main thread by explicitly calling the createGeometry method.

名称 Name 类型 Type 说明 Description
options object 可选 optional 具有以下属性的对象: Object with the following properties:
名称 Name 类型 Type 默认值 Default 说明 Description
geometryInstances Array.<GeometryInstance> | GeometryInstance 可选 optional 要渲染的几何实例 - 或单个几何实例。 The geometry instances - or a single geometry instance - to render.
appearance Appearance 可选 optional 用于渲染图元的外观。 The appearance used to render the primitive.
depthFailAppearance Appearance 可选 optional 当该图元未通过深度测试时用于对其进行着色的外观。 The appearance used to shade this primitive when it fails the depth test.
show boolean true 可选 optional 确定是否显示该原语。 Determines if this primitive will be shown.
modelMatrix Matrix4 Matrix4.IDENTITY 可选 optional 将图元(所有几何实例)从模型转换为世界坐标的 4x4 转换矩阵。 The 4x4 transformation matrix that transforms the primitive (all geometry instances) from model to world coordinates.
vertexCacheOptimize boolean false 可选 optional When true,几何顶点针对顶点着色器前和后的缓存进行了优化。 , geometry vertices are optimized for the pre and post-vertex-shader caches.
interleave boolean false 可选 optional When true,几何体顶点属性是交错的,这可以稍微提高渲染性能但增加加载时间。 , geometry vertex attributes are interleaved, which can slightly improve rendering performance but increases load time.
compressVertices boolean true 可选 optional When true,几何顶点被压缩,这将节省内存。 , the geometry vertices are compressed, which will save memory.
releaseGeometryInstances boolean true 可选 optional When true,原语不保留对输入的引用 , the primitive does not keep a reference to the input geometryInstances 以节省内存。 to save memory.
allowPicking boolean true 可选 optional When true,每个几何实例只能通过以下方式选取 , each geometry instance will only be pickable with Scene#pick。当 . When false,节省了GPU内存。 , GPU memory is saved.
cull boolean true 可选 optional When true,渲染器视锥体剔除和地平线剔除基元的命令基于其边界体积。将其设置为 , the renderer frustum culls and horizon culls the primitive's commands based on their bounding volume. Set this to false 如果您手动剔除基元,则会获得较小的性能提升。 for a small performance gain if you are manually culling the primitive.
asynchronous boolean true 可选 optional 确定基元是异步创建还是阻塞直到准备好。 Determines if the primitive will be created asynchronously or block until ready.
debugShowBoundingVolume boolean false 可选 optional 仅用于调试。确定是否显示该图元的命令的边界球体。 For debugging only. Determines if this primitive's commands' bounding spheres are shown.
shadows ShadowMode ShadowMode.DISABLED 可选 optional 确定该图元是否投射或接收来自光源的阴影。 Determines whether this primitive casts or receives shadows from light sources.
示例: 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

readonly allowPicking : boolean

When true,每个几何实例只能通过以下方式选取 , each geometry instance will only be pickable with Scene#pick。当 . When false,节省了GPU内存。 * , GPU memory is saved. *
默认值: Default Value: true
The Appearance 用于对这个基元进行着色。每个几何体实例都具有相同外观的阴影。有些外表,比如 used to shade this primitive. Each geometry instance is shaded with the same appearance. Some appearances, like PerInstanceColorAppearance 允许赋予每个实例独特的属性。 allow giving each instance unique properties.
默认值: Default Value: undefined

readonly asynchronous : boolean

确定是否将在 Web Worker 上创建和批处理几何实例。 Determines if the geometry instances will be created and batched on a web worker.
默认值: Default Value: true

readonly compressVertices : boolean

When true,几何顶点被压缩,这将节省内存。 , geometry vertices are compressed, which will save memory.
默认值: Default Value: true
When true,渲染器视锥体剔除和地平线剔除基元的命令基于其边界体积。将其设置为 , the renderer frustum culls and horizon culls the primitive's commands based on their bounding volume. Set this to false 如果您手动剔除基元,则会获得较小的性能提升。 for a small performance gain if you are manually culling the primitive.
默认值: Default Value: true

debugShowBoundingVolume : boolean

该属性仅用于调试;它不适合生产用途,也没有经过优化。 This property is for debugging only; it is not for production use nor is it optimized.

Draws the bounding sphere for each draw command in the primitive.

默认值: Default Value: false
The Appearance 用于在深度测试失败时对该图元进行着色。每个几何体实例都具有相同外观的阴影。有些外表,比如 used to shade this primitive when it fails the depth test. Each geometry instance is shaded with the same appearance. Some appearances, like PerInstanceColorAppearance 允许赋予每个实例独特的属性。 allow giving each instance unique properties.

当使用需要颜色属性的外观(例如 PerInstanceColorAppearance)时,请改为添加每个实例的 depthFailColor 属性。 When using an appearance that requires a color attribute, like PerInstanceColorAppearance, add a depthFailColor per-instance attribute instead.

需要 EXT_frag_depth WebGL 扩展才能正确渲染。如果不支持扩展,则可能会出现工件。 Requires the EXT_frag_depth WebGL extension to render properly. If the extension is not supported, there may be artifacts.

默认值: Default Value: undefined
使用此图元渲染的几何实例。这可能是 The geometry instances rendered with this primitive. This may be undefined if options.releaseGeometryInstances is true 当构造原语时。 when the primitive is constructed.

在渲染图元后更改此属性没有任何效果。 Changing this property after the primitive is rendered has no effect.

默认值: Default Value: undefined

readonly interleave : boolean

确定几何体顶点属性是否交错,这可以稍微提高渲染性能。 Determines if geometry vertex attributes are interleaved, which can slightly improve rendering performance.
默认值: Default Value: false
将图元(所有几何实例)从模型转换为世界坐标的 4x4 转换矩阵。当这是单位矩阵时,基元是在世界坐标(即地球的 WGS84 坐标)中绘制的。可以通过提供不同的变换矩阵来使用局部参考系,例如返回的矩阵 The 4x4 transformation matrix that transforms the primitive (all geometry instances) from model to world coordinates. When this is the identity matrix, the primitive is drawn in world coordinates, i.e., Earth's WGS84 coordinates. Local reference frames can be used by providing a different transformation matrix, like that returned by Transforms.eastNorthUpToFixedFrame.

此属性仅在 3D 模式下受支持。 This property is only supported in 3D mode.

默认值: Default Value: Matrix4.IDENTITY
示例: Example:
const origin = Cesium.Cartesian3.fromDegrees(-95.0, 40.0, 200000.0);
p.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(origin);

readonly ready : boolean

确定图元是否完整并准备好渲染。如果此属性为 true,则图元将在下一次渲染 Determines if the primitive is complete and ready to render. If this property is true, the primitive will be rendered the next time that Primitive#update 被称为。 is called.
示例: 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();
});

readonly releaseGeometryInstances : boolean

When true,原语不保留对输入的引用 , the primitive does not keep a reference to the input geometryInstances 以节省内存。 to save memory.
默认值: Default Value: true
确定该图元是否投射或接收来自光源的阴影。 Determines whether this primitive casts or receives shadows from light sources.
默认值: Default Value: ShadowMode.DISABLED
确定是否显示图元。这会影响基元中的所有几何体实例。 Determines if the primitive will be shown. This affects all geometry instances in the primitive.
默认值: Default Value: true

readonly vertexCacheOptimize : boolean

When true,几何顶点针对顶点着色器前和后的缓存进行了优化。 , geometry vertices are optimized for the pre and post-vertex-shader caches.
默认值: Default Value: true

方法 Methods

销毁该对象持有的 WebGL 资源。销毁对象可以确定性地释放 WebGL 资源,而不是依赖垃圾收集器来销毁该对象。 Destroys the WebGL resources held by this object. Destroying an object allows for deterministic release of WebGL resources, instead of relying on the garbage collector to destroy this object.

一旦对象被销毁,就不应再使用;调用除 Once an object is destroyed, it should not be used; calling any function other than isDestroyed 将导致 will result in a DeveloperError 例外。因此,分配返回值( exception. Therefore, assign the return value (undefined) 到对象,如示例中所做的那样。 ) to the object as done in the example.

抛出: Throws:
  • DeveloperError : This object was destroyed, i.e., destroy() was called.
示例: Example:
e = e && e.destroy();
另见: See:

getGeometryInstanceAttributes(id)object

返回可修改的每个实例属性 Returns the modifiable per-instance attributes for a GeometryInstance.
名称 Name 类型 Type 说明 Description
id * 的 ID The id of the GeometryInstance.
返回: Returns:
属性格式的类型化数组,如果没有带 id 的实例,则为未定义。 The typed array in the attribute's format or undefined if the is no instance with id.
抛出: Throws:
  • DeveloperError :必须在调用 getGeometryInstanceAttributes 之前调用 update。 : must call update before calling getGeometryInstanceAttributes.
示例: 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()boolean

如果该对象被销毁则返回 true;否则为假。 Returns true if this object was destroyed; otherwise, false.

如果该对象被破坏,则不应使用它;调用除 If this object was destroyed, it should not be used; calling any function other than isDestroyed 将导致 will result in a DeveloperError exception.

返回: Returns:
true 如果该物体被摧毁;否则, if this object was destroyed; otherwise, false.
另见: See:
调用时间 Called when Viewer or CesiumWidget 渲染场景以获取渲染此图元所需的绘制命令。 render the scene to get the draw commands needed to render this primitive.

不要直接调用该函数。记录此内容只是为了列出渲染场景时可能传播的异常: Do not call this function directly. This is documented just to list the exceptions that may be propagated when the scene is rendered:

抛出: Throws:
  • DeveloperError :所有实例几何图形必须具有相同的primitiveType。 : All instance geometries must have the same primitiveType.
  • DeveloperError :外观和材质具有同名统一。 : Appearance and material have a uniform with the same name.
  • DeveloperError :Primitive.modelMatrix 仅在 3D 模式下受支持。 : Primitive.modelMatrix is only supported in 3D mode.
  • RuntimeError :需要顶点纹理获取支持来渲染具有每个实例属性的图元。顶点纹理图像单元的最大数量必须大于零。 : Vertex texture fetch support is required to render primitives with per-instance attributes. The maximum number of vertex texture image units must be greater than zero.
需要帮助吗?获得答案的最快方法是从社区和团队那里获得答案 Need help? The fastest way to get answers is from the community and team on the Cesium Forum.