Primitive

Primitive

new

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 ExtentGeometry and an EllipsoidGeometry 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; Context#pick returns their GeometryInstance#id. Using per-instance appearances like PerInstanceColorAppearance, each instance can also have a unique color.

Parameters:
Name Type Argument Default Description
options.geometryInstances Array | GeometryInstance <optional>
undefined The geometry instances - or a single geometry instance - to render.
options.appearance Appearance <optional>
undefined The appearance used to render the primitive.
options.vertexCacheOptimize Boolean <optional>
true When true, geometry vertices are optimized for the pre and post-vertex-shader caches.
options.releaseGeometryInstances Boolean <optional>
true When true, the primitive does not keep a reference to the input geometryInstances to save memory.
options.allow3DOnly Boolean <optional>
false When true, each geometry instance will only be rendered in 3D.
Example
// 1. Draw a translucent ellipse on the surface with a checkerboard pattern
var instance = new GeometryInstance({
  geometry : new EllipseGeometry({
      vertexFormat : VertexFormat.POSITION_AND_ST,
      ellipsoid : ellipsoid,
      center : ellipsoid.cartographicToCartesian(Cartographic.fromDegrees(-100, 20)),
      semiMinorAxis : 500000.0,
      semiMajorAxis : 1000000.0,
      bearing : CesiumMath.PI_OVER_FOUR
  }),
  id : 'object returned when this instance is picked and to get/set per-instance attributes'
});
var primitive = new Primitive({
  geometryInstances : instance,
  appearance : new EllipsoidSurfaceAppearance({
    material : Material.fromType(scene.getContext(), 'Checkerboard')
  })
});
scene.getPrimitives().add(primitive);

// 2. Draw different instances each with a unique color
var extentInstance = new GeometryInstance({
  geometry : new ExtentGeometry({
    vertexFormat : VertexFormat.POSITION_AND_NORMAL,
    extent : new Extent(
      CesiumMath.toRadians(-140.0),
      CesiumMath.toRadians(30.0),
      CesiumMath.toRadians(-100.0),
      CesiumMath.toRadians(40.0))
    }),
  id : 'extent',
  attribute : {
    color : new ColorGeometryInstanceAttribute(0.0, 1.0, 1.0, 0.5)
  }
});
var ellipsoidInstance = new GeometryInstance({
  geometry : new EllipsoidGeometry({
    vertexFormat : VertexFormat.POSITION_AND_NORMAL,
    radii : new Cartesian3(500000.0, 500000.0, 1000000.0)
  }),
  modelMatrix : Matrix4.multiplyByTranslation(Transforms.eastNorthUpToFixedFrame(
    ellipsoid.cartographicToCartesian(Cartographic.fromDegrees(-95.59777, 40.03883))), new Cartesian3(0.0, 0.0, 500000.0)),
  id : 'ellipsoid',
  attribute : {
    color : ColorGeometryInstanceAttribute.fromColor(Color.AQUA)
  }
});
var primitive = new Primitive({
  geometryInstances : [extentInstance, ellipsoidInstance],
  appearance : new PerInstanceColorAppearance()
});
scene.getPrimitives().add(primitive);
See:
Source:

Members

: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.
Default Value:
  • undefined

:Array

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

: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. This matrix is available to GLSL vertex and fragment shaders via czm_model and derived uniforms.
Example
var origin = ellipsoid.cartographicToCartesian(
  Cartographic.fromDegrees(-95.0, 40.0, 200000.0));
p.modelMatrix = Transforms.eastNorthUpToFixedFrame(origin);
Default Value:
  • Matrix4.IDENTITY
See:

:Boolean

Determines if the primitive will be shown. This affects all geometry instances in the primitive.
Default Value:
  • true

Methods

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.
Returns:
Example
e = e && e.destroy();
See:

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:
Boolean true if this object was destroyed; otherwise, false.
See:

<static>

Returns the modifiable per-instance attributes for a GeometryInstance.

Parameters:
Name Type Description
id Object The id of the GeometryInstance.
Throws:
Returns:
Object The typed array in the attribute's format or undefined if the is no instance with id.
Example
var attributes = primitive.getGeometryInstanceAttributes('an id');
attributes.color = ColorGeometryInstanceAttribute.toValue(Color.AQUA);
attributes.show = ShowGeometryInstanceAttribute.toValue(true);