import ApproximateTerrainHeights from "../Core/ApproximateTerrainHeights.js"; import ComponentDatatype from "../Core/ComponentDatatype.js"; import defaultValue from "../Core/defaultValue.js"; import defined from "../Core/defined.js"; import destroyObject from "../Core/destroyObject.js"; import DeveloperError from "../Core/DeveloperError.js"; import deprecationWarning from "../Core/deprecationWarning.js"; import GeometryInstance from "../Core/GeometryInstance.js"; import GeometryInstanceAttribute from "../Core/GeometryInstanceAttribute.js"; import GroundPolylineGeometry from "../Core/GroundPolylineGeometry.js"; import DrawCommand from "../Renderer/DrawCommand.js"; import Pass from "../Renderer/Pass.js"; import RenderState from "../Renderer/RenderState.js"; import ShaderProgram from "../Renderer/ShaderProgram.js"; import ShaderSource from "../Renderer/ShaderSource.js"; import PolylineShadowVolumeFS from "../Shaders/PolylineShadowVolumeFS.js"; import PolylineShadowVolumeMorphFS from "../Shaders/PolylineShadowVolumeMorphFS.js"; import PolylineShadowVolumeMorphVS from "../Shaders/PolylineShadowVolumeMorphVS.js"; import PolylineShadowVolumeVS from "../Shaders/PolylineShadowVolumeVS.js"; import BlendingState from "./BlendingState.js"; import ClassificationType from "./ClassificationType.js"; import CullFace from "./CullFace.js"; import PolylineColorAppearance from "./PolylineColorAppearance.js"; import PolylineMaterialAppearance from "./PolylineMaterialAppearance.js"; import Primitive from "./Primitive.js"; import SceneMode from "./SceneMode.js"; import StencilConstants from "./StencilConstants.js"; import StencilFunction from "./StencilFunction.js"; import StencilOperation from "./StencilOperation.js"; /** * A GroundPolylinePrimitive represents a polyline draped over the terrain or 3D Tiles in the {@link Scene}. *
* Only to be used with GeometryInstances containing {@link GroundPolylineGeometry}. *
* * @alias GroundPolylinePrimitive * @constructor * * @param {object} [options] Object with the following properties: * @param {Array|GeometryInstance} [options.geometryInstances] GeometryInstances containing GroundPolylineGeometry * @param {Appearance} [options.appearance] The Appearance used to render the polyline. Defaults to a white color {@link Material} on a {@link PolylineMaterialAppearance}. * @param {boolean} [options.show=true] Determines if this primitive will be shown. * @param {boolean} [options.interleave=false] Whentrue
, geometry vertex attributes are interleaved, which can slightly improve rendering performance but increases load time.
* @param {boolean} [options.releaseGeometryInstances=true] When true
, the primitive does not keep a reference to the input geometryInstances
to save memory.
* @param {boolean} [options.allowPicking=true] When true
, each geometry instance will only be pickable with {@link Scene#pick}. When false
, GPU memory is saved.
* @param {boolean} [options.asynchronous=true] Determines if the primitive will be created asynchronously or block until ready. If false initializeTerrainHeights() must be called first.
* @param {ClassificationType} [options.classificationType=ClassificationType.BOTH] Determines whether terrain, 3D Tiles or both will be classified.
* @param {boolean} [options.debugShowBoundingVolume=false] For debugging only. Determines if this primitive's commands' bounding spheres are shown.
* @param {boolean} [options.debugShowShadowVolume=false] For debugging only. Determines if the shadow volume for each geometry in the primitive is drawn. Must be true
on creation to have effect.
*
* @example
* // 1. Draw a polyline on terrain with a basic color material
*
* const instance = new Cesium.GeometryInstance({
* geometry : new Cesium.GroundPolylineGeometry({
* positions : Cesium.Cartesian3.fromDegreesArray([
* -112.1340164450331, 36.05494287836128,
* -112.08821010582645, 36.097804071380715
* ]),
* width : 4.0
* }),
* id : 'object returned when this instance is picked and to get/set per-instance attributes'
* });
*
* scene.groundPrimitives.add(new Cesium.GroundPolylinePrimitive({
* geometryInstances : instance,
* appearance : new Cesium.PolylineMaterialAppearance()
* }));
*
* // 2. Draw a looped polyline on terrain with per-instance color and a distance display condition.
* // Distance display conditions for polylines on terrain are based on an approximate terrain height
* // instead of true terrain height.
*
* const instance2 = new Cesium.GeometryInstance({
* geometry : new Cesium.GroundPolylineGeometry({
* positions : Cesium.Cartesian3.fromDegreesArray([
* -112.1340164450331, 36.05494287836128,
* -112.08821010582645, 36.097804071380715,
* -112.13296079730024, 36.168769146801104
* ]),
* loop : true,
* width : 4.0
* }),
* attributes : {
* color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromCssColorString('green').withAlpha(0.7)),
* distanceDisplayCondition : new Cesium.DistanceDisplayConditionGeometryInstanceAttribute(1000, 30000)
* },
* id : 'object returned when this instance is picked and to get/set per-instance attributes'
* });
*
* scene.groundPrimitives.add(new Cesium.GroundPolylinePrimitive({
* geometryInstances : instance2,
* appearance : new Cesium.PolylineColorAppearance()
* }));
*/
function GroundPolylinePrimitive(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
/**
* 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. *
* * @readonly * @type {Array|GeometryInstance} * * @default undefined */ this.geometryInstances = options.geometryInstances; this._hasPerInstanceColors = true; let appearance = options.appearance; if (!defined(appearance)) { appearance = new PolylineMaterialAppearance(); } /** * The {@link Appearance} used to shade this primitive. Each geometry * instance is shaded with the same appearance. Some appearances, like * {@link PolylineColorAppearance} allow giving each instance unique * properties. * * @type Appearance * * @default undefined */ this.appearance = appearance; /** * Determines if the primitive will be shown. This affects all geometry * instances in the primitive. * * @type {boolean} * * @default true */ this.show = defaultValue(options.show, true); /** * Determines whether terrain, 3D Tiles or both will be classified. * * @type {ClassificationType} * * @default ClassificationType.BOTH */ this.classificationType = defaultValue( options.classificationType, ClassificationType.BOTH ); /** * 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. *
* * @type {boolean} * * @default false */ this.debugShowBoundingVolume = defaultValue( options.debugShowBoundingVolume, false ); // Shadow volume is shown by removing a discard in the shader, so this isn't toggleable. this._debugShowShadowVolume = defaultValue( options.debugShowShadowVolume, false ); this._primitiveOptions = { geometryInstances: undefined, appearance: undefined, vertexCacheOptimize: false, interleave: defaultValue(options.interleave, false), releaseGeometryInstances: defaultValue( options.releaseGeometryInstances, true ), allowPicking: defaultValue(options.allowPicking, true), asynchronous: defaultValue(options.asynchronous, true), compressVertices: false, _createShaderProgramFunction: undefined, _createCommandsFunction: undefined, _updateAndQueueCommandsFunction: undefined, }; // Used when inserting in an OrderedPrimitiveCollection this._zIndex = undefined; this._ready = false; const groundPolylinePrimitive = this; // This is here for backwards compatibility. This promise wrapper can be removed once readyPromise is removed. this._readyPromise = new Promise((resolve, reject) => { groundPolylinePrimitive._completeLoad = () => { this._ready = true; if (this.releaseGeometryInstances) { this.geometryInstances = undefined; } const error = this._error; if (!defined(error)) { resolve(this); } else { reject(error); } }; }); this._primitive = undefined; this._sp = undefined; this._sp2D = undefined; this._spMorph = undefined; this._renderState = getRenderState(false); this._renderState3DTiles = getRenderState(true); this._renderStateMorph = RenderState.fromCache({ cull: { enabled: true, face: CullFace.FRONT, // Geometry is "inverted," so cull front when materials on volume instead of on terrain (morph) }, depthTest: { enabled: true, }, blending: BlendingState.PRE_MULTIPLIED_ALPHA_BLEND, depthMask: false, }); } Object.defineProperties(GroundPolylinePrimitive.prototype, { /** * Determines if geometry vertex attributes are interleaved, which can slightly improve rendering performance. * * @memberof GroundPolylinePrimitive.prototype * * @type {boolean} * @readonly * * @default false */ interleave: { get: function () { return this._primitiveOptions.interleave; }, }, /** * Whentrue
, the primitive does not keep a reference to the input geometryInstances
to save memory.
*
* @memberof GroundPolylinePrimitive.prototype
*
* @type {boolean}
* @readonly
*
* @default true
*/
releaseGeometryInstances: {
get: function () {
return this._primitiveOptions.releaseGeometryInstances;
},
},
/**
* When true
, each geometry instance will only be pickable with {@link Scene#pick}. When false
, GPU memory is saved.
*
* @memberof GroundPolylinePrimitive.prototype
*
* @type {boolean}
* @readonly
*
* @default true
*/
allowPicking: {
get: function () {
return this._primitiveOptions.allowPicking;
},
},
/**
* Determines if the geometry instances will be created and batched on a web worker.
*
* @memberof GroundPolylinePrimitive.prototype
*
* @type {boolean}
* @readonly
*
* @default true
*/
asynchronous: {
get: function () {
return this._primitiveOptions.asynchronous;
},
},
/**
* Determines if the primitive is complete and ready to render. If this property is
* true, the primitive will be rendered the next time that {@link GroundPolylinePrimitive#update}
* is called.
*
* @memberof GroundPolylinePrimitive.prototype
*
* @type {boolean}
* @readonly
*/
ready: {
get: function () {
return this._ready;
},
},
/**
* Gets a promise that resolves when the primitive is ready to render.
* @memberof GroundPolylinePrimitive.prototype
* @type {Promise* If true, draws the shadow volume for each geometry in the primitive. *
* * @memberof GroundPolylinePrimitive.prototype * * @type {boolean} * @readonly * * @default false */ debugShowShadowVolume: { get: function () { return this._debugShowShadowVolume; }, }, }); /** * Initializes the minimum and maximum terrain heights. This only needs to be called if you are creating the * GroundPolylinePrimitive synchronously. * * @returns {Promise* Do not call this function directly. This is documented just to * list the exceptions that may be propagated when the scene is rendered: *
* * @exception {DeveloperError} For synchronous GroundPolylinePrimitives, you must call GroundPolylinePrimitives.initializeTerrainHeights() and wait for the returned promise to resolve. * @exception {DeveloperError} All GeometryInstances must have color attributes to use PolylineColorAppearance with GroundPolylinePrimitive. */ GroundPolylinePrimitive.prototype.update = function (frameState) { if (!defined(this._primitive) && !defined(this.geometryInstances)) { return; } if (!ApproximateTerrainHeights.initialized) { //>>includeStart('debug', pragmas.debug); if (!this.asynchronous) { throw new DeveloperError( "For synchronous GroundPolylinePrimitives, you must call GroundPolylinePrimitives.initializeTerrainHeights() and wait for the returned promise to resolve." ); } //>>includeEnd('debug'); GroundPolylinePrimitive.initializeTerrainHeights(); return; } let i; const that = this; const primitiveOptions = this._primitiveOptions; if (!defined(this._primitive)) { const geometryInstances = Array.isArray(this.geometryInstances) ? this.geometryInstances : [this.geometryInstances]; const geometryInstancesLength = geometryInstances.length; const groundInstances = new Array(geometryInstancesLength); let attributes; // Check if each instance has a color attribute. for (i = 0; i < geometryInstancesLength; ++i) { attributes = geometryInstances[i].attributes; if (!defined(attributes) || !defined(attributes.color)) { this._hasPerInstanceColors = false; break; } } for (i = 0; i < geometryInstancesLength; ++i) { const geometryInstance = geometryInstances[i]; attributes = {}; const instanceAttributes = geometryInstance.attributes; for (const attributeKey in instanceAttributes) { if (instanceAttributes.hasOwnProperty(attributeKey)) { attributes[attributeKey] = instanceAttributes[attributeKey]; } } // Automatically create line width attribute if not already given if (!defined(attributes.width)) { attributes.width = new GeometryInstanceAttribute({ componentDatatype: ComponentDatatype.UNSIGNED_BYTE, componentsPerAttribute: 1.0, value: [geometryInstance.geometry.width], }); } // Update each geometry for framestate.scene3DOnly = true and projection geometryInstance.geometry._scene3DOnly = frameState.scene3DOnly; GroundPolylineGeometry.setProjectionAndEllipsoid( geometryInstance.geometry, frameState.mapProjection ); groundInstances[i] = new GeometryInstance({ geometry: geometryInstance.geometry, attributes: attributes, id: geometryInstance.id, pickPrimitive: that, }); } primitiveOptions.geometryInstances = groundInstances; primitiveOptions.appearance = this.appearance; primitiveOptions._createShaderProgramFunction = function ( primitive, frameState, appearance ) { createShaderProgram(that, frameState, appearance); }; primitiveOptions._createCommandsFunction = function ( primitive, appearance, material, translucent, twoPasses, colorCommands, pickCommands ) { createCommands( that, appearance, material, translucent, colorCommands, pickCommands ); }; primitiveOptions._updateAndQueueCommandsFunction = function ( primitive, frameState, colorCommands, pickCommands, modelMatrix, cull, debugShowBoundingVolume, twoPasses ) { updateAndQueueCommands( that, frameState, colorCommands, pickCommands, modelMatrix, cull, debugShowBoundingVolume ); }; this._primitive = new Primitive(primitiveOptions); } if ( this.appearance instanceof PolylineColorAppearance && !this._hasPerInstanceColors ) { throw new DeveloperError( "All GeometryInstances must have color attributes to use PolylineColorAppearance with GroundPolylinePrimitive." ); } this._primitive.appearance = this.appearance; this._primitive.show = this.show; this._primitive.debugShowBoundingVolume = this.debugShowBoundingVolume; this._primitive.update(frameState); frameState.afterRender.push(() => { if (!this._ready && defined(this._primitive) && this._primitive.ready) { this._completeLoad(); } }); }; /** * Returns the modifiable per-instance attributes for a {@link GeometryInstance}. * * @param {*} id The id of the {@link GeometryInstance}. * @returns {object} The typed array in the attribute's format or undefined if the is no instance with id. * * @exception {DeveloperError} 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); */ GroundPolylinePrimitive.prototype.getGeometryInstanceAttributes = function ( id ) { //>>includeStart('debug', pragmas.debug); if (!defined(this._primitive)) { throw new DeveloperError( "must call update before calling getGeometryInstanceAttributes" ); } //>>includeEnd('debug'); return this._primitive.getGeometryInstanceAttributes(id); }; /** * Checks if the given Scene supports GroundPolylinePrimitives. * GroundPolylinePrimitives require support for the WEBGL_depth_texture extension. * * @param {Scene} scene The current scene. * @returns {boolean} Whether or not the current scene supports GroundPolylinePrimitives. */ GroundPolylinePrimitive.isSupported = function (scene) { return scene.frameState.context.depthTexture; }; /** * 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 {@link DeveloperError} exception.
*
true
if this object was destroyed; otherwise, false
.
*
* @see GroundPolylinePrimitive#destroy
*/
GroundPolylinePrimitive.prototype.isDestroyed = function () {
return false;
};
/**
* 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 {@link DeveloperError} exception. Therefore,
* assign the return value (undefined
) to the object as done in the example.
*