Skip to content

KBE3D / KBCore / Cesium / Primitive

类: Primitive

A primitive represents geometry in the Scene. The geometry can be from a single GeometryInstance 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 as shown in Code Example 2. <p> 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. </p> <p> 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. </p> <p> Geometry 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 <code>createGeometry</code> method. </p>

Examples

ts
// 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')
  })
}));
ts
// 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()
}));
ts
// 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
}));

参数

Object with the following properties:

参数

The geometry instances - or a single geometry instance - to render.

参数

The appearance used to render the primitive.

参数

The appearance used to shade this primitive when it fails the depth test.

参数

Determines if this primitive will be shown.

参数

The 4x4 transformation matrix that transforms the primitive (all geometry instances) from model to world coordinates.

参数

When <code>true</code>, geometry vertices are optimized for the pre and post-vertex-shader caches.

参数

When <code>true</code>, geometry vertex attributes are interleaved, which can slightly improve rendering performance but increases load time.

参数

When <code>true</code>, the geometry vertices are compressed, which will save memory.

参数

When <code>true</code>, the primitive does not keep a reference to the input <code>geometryInstances</code> to save memory.

参数

When <code>true</code>, each geometry instance will only be pickable with Scene#pick. When <code>false</code>, GPU memory is saved.

参数

When <code>true</code>, the renderer frustum culls and horizon culls the primitive's commands based on their bounding volume. Set this to <code>false</code> for a small performance gain if you are manually culling the primitive.

参数

Determines if the primitive will be created asynchronously or block until ready.

参数

For debugging only. Determines if this primitive's commands' bounding spheres are shown.

参数

Determines whether this primitive casts or receives shadows from light sources.

属性

geometryInstances

readonly geometryInstances: GeometryInstance | GeometryInstance[]

The geometry instances rendered with this primitive. This may be <code>undefined</code> if <code>options.releaseGeometryInstances</code> is <code>true</code> when the primitive is constructed. <p> Changing this property after the primitive is rendered has no effect. </p>


appearance

appearance: Appearance

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.


depthFailAppearance

depthFailAppearance: Appearance

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.

<p> When using an appearance that requires a color attribute, like PerInstanceColorAppearance, add a depthFailColor per-instance attribute instead. </p>

<p> Requires the EXT_frag_depth WebGL extension to render properly. If the extension is not supported, there may be artifacts. </p>


modelMatrix

modelMatrix: Matrix4

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.

<p> This property is only supported in 3D mode. </p>

示例

ts
const origin = Cesium.Cartesian3.fromDegrees(-95.0, 40.0, 200000.0);
p.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(origin);

show

show: boolean

Determines if the primitive will be shown. This affects all geometry instances in the primitive.


cull

cull: boolean

When <code>true</code>, the renderer frustum culls and horizon culls the primitive's commands based on their bounding volume. Set this to <code>false</code> for a small performance gain if you are manually culling the primitive.


debugShowBoundingVolume

debugShowBoundingVolume: boolean

This property is for debugging only; it is not for production use nor is it optimized. <p> Draws the bounding sphere for each draw command in the primitive. </p>


shadows

shadows: ShadowMode

Determines whether this primitive casts or receives shadows from light sources.


vertexCacheOptimize

readonly vertexCacheOptimize: boolean

When <code>true</code>, geometry vertices are optimized for the pre and post-vertex-shader caches.


interleave

readonly interleave: boolean

Determines if geometry vertex attributes are interleaved, which can slightly improve rendering performance.


releaseGeometryInstances

readonly releaseGeometryInstances: boolean

When <code>true</code>, the primitive does not keep a reference to the input <code>geometryInstances</code> to save memory.


allowPicking

readonly allowPicking: boolean

When <code>true</code>, each geometry instance will only be pickable with Scene#pick. When <code>false</code>, GPU memory is saved. *


asynchronous

readonly asynchronous: boolean

Determines if the geometry instances will be created and batched on a web worker.


compressVertices

readonly compressVertices: boolean

When <code>true</code>, geometry vertices are compressed, which will save memory.


ready

readonly ready: boolean

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.

示例

ts
// 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();
});

方法

update()

update(): void

Called when Viewer or CesiumWidget render the scene to get the draw commands needed to render this primitive. <p> Do not call this function directly. This is documented just to list the exceptions that may be propagated when the scene is rendered: </p>

返回

void


getGeometryInstanceAttributes()

getGeometryInstanceAttributes(id: any): any

Returns the modifiable per-instance attributes for a GeometryInstance.

参数

id

any

The id of the GeometryInstance.

返回

any

The typed array in the attribute's format or undefined if the is no instance with id.

示例

ts
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()

isDestroyed(): boolean

Returns true if this object was destroyed; otherwise, false. <p> If this object was destroyed, it should not be used; calling any function other than <code>isDestroyed</code> will result in a DeveloperError exception. </p>

返回

boolean

true if this object was destroyed; otherwise, false.


destroy()

destroy(): void

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. <p> Once an object is destroyed, it should not be used; calling any function other than <code>isDestroyed</code> will result in a DeveloperError exception. Therefore, assign the return value (<code>undefined</code>) to the object as done in the example. </p>

返回

void

示例

ts
e = e && e.destroy();

构造函数

构造函数

new Primitive(options?: { geometryInstances?: GeometryInstance | GeometryInstance[]; appearance?: Appearance; depthFailAppearance?: Appearance; show?: boolean; modelMatrix?: Matrix4; vertexCacheOptimize?: boolean; interleave?: boolean; compressVertices?: boolean; releaseGeometryInstances?: boolean; allowPicking?: boolean; cull?: boolean; asynchronous?: boolean; debugShowBoundingVolume?: boolean; shadows?: ShadowMode; }): Primitive

参数

options?
geometryInstances?

GeometryInstance | GeometryInstance[]

appearance?

Appearance

depthFailAppearance?

Appearance

show?

boolean

modelMatrix?

Matrix4

vertexCacheOptimize?

boolean

interleave?

boolean

compressVertices?

boolean

releaseGeometryInstances?

boolean

allowPicking?

boolean

cull?

boolean

asynchronous?

boolean

debugShowBoundingVolume?

boolean

shadows?

ShadowMode

返回

Primitive

KBE3D @3.0.0 Copyright © 2024-present KBE3D