All files / engine/Source/DataSources PlaneGeometryUpdater.js

99.04% Statements 104/105
90.47% Branches 38/42
100% Functions 12/12
99.04% Lines 104/105

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386                                            1x 1x     64x 64x 64x 64x                         64x               64x     1x 1x 1x                     1x   14x   13x 1x           12x 12x         12x             12x   12x     12x   11x       11x   11x 1x   11x 11x           1x           12x 12x 12x 12x         12x           12x 12x   12x             12x                               1x   11x   10x 1x           9x 9x 9x             9x   9x 9x 9x 9x         9x           9x 9x   9x             9x                                       1x 31x               1x 25x     1x 29x                 1x   19x   19x 19x     19x 19x           1x                   5x               1x 1x     1x       1x         8x 8x         8x               1x         8x 8x 8x             1x 1x 1x 1x 1x 1x 1x   21x 21x   21x           21x 21x                   21x 21x 21x 21x   21x 21x 21x 21x   21x           21x           21x         21x           1x    
import Cartesian3 from "../Core/Cartesian3.js";
import Check from "../Core/Check.js";
import Color from "../Core/Color.js";
import ColorGeometryInstanceAttribute from "../Core/ColorGeometryInstanceAttribute.js";
import defined from "../Core/defined.js";
import DeveloperError from "../Core/DeveloperError.js";
import DistanceDisplayConditionGeometryInstanceAttribute from "../Core/DistanceDisplayConditionGeometryInstanceAttribute.js";
import GeometryInstance from "../Core/GeometryInstance.js";
import Iso8601 from "../Core/Iso8601.js";
import CesiumMath from "../Core/Math.js";
import Matrix3 from "../Core/Matrix3.js";
import Matrix4 from "../Core/Matrix4.js";
import PlaneGeometry from "../Core/PlaneGeometry.js";
import PlaneOutlineGeometry from "../Core/PlaneOutlineGeometry.js";
import ShowGeometryInstanceAttribute from "../Core/ShowGeometryInstanceAttribute.js";
import MaterialAppearance from "../Scene/MaterialAppearance.js";
import PerInstanceColorAppearance from "../Scene/PerInstanceColorAppearance.js";
import ColorMaterialProperty from "./ColorMaterialProperty.js";
import DynamicGeometryUpdater from "./DynamicGeometryUpdater.js";
import GeometryUpdater from "./GeometryUpdater.js";
import Property from "./Property.js";
 
const positionScratch = new Cartesian3();
const scratchColor = new Color();
 
function PlaneGeometryOptions(entity) {
  this.id = entity;
  this.vertexFormat = undefined;
  this.plane = undefined;
  this.dimensions = undefined;
}
 
/**
 * A {@link GeometryUpdater} for planes.
 * Clients do not normally create this class directly, but instead rely on {@link DataSourceDisplay}.
 * @alias PlaneGeometryUpdater
 * @constructor
 *
 * @param {Entity} entity The entity containing the geometry to be visualized.
 * @param {Scene} scene The scene where visualization is taking place.
 */
function PlaneGeometryUpdater(entity, scene) {
  GeometryUpdater.call(this, {
    entity: entity,
    scene: scene,
    geometryOptions: new PlaneGeometryOptions(entity),
    geometryPropertyName: "plane",
    observedPropertyNames: ["availability", "position", "orientation", "plane"],
  });
 
  this._onEntityPropertyChanged(entity, "plane", entity.plane, undefined);
}
 
Eif (defined(Object.create)) {
  PlaneGeometryUpdater.prototype = Object.create(GeometryUpdater.prototype);
  PlaneGeometryUpdater.prototype.constructor = PlaneGeometryUpdater;
}
 
/**
 * Creates the geometry instance which represents the fill of the geometry.
 *
 * @param {JulianDate} time The time to use when retrieving initial attribute values.
 * @returns {GeometryInstance} The geometry instance representing the filled portion of the geometry.
 *
 * @exception {DeveloperError} This instance does not represent a filled geometry.
 */
PlaneGeometryUpdater.prototype.createFillGeometryInstance = function (time) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("time", time);
 
  if (!this._fillEnabled) {
    throw new DeveloperError(
      "This instance does not represent a filled geometry.",
    );
  }
  //>>includeEnd('debug');
 
  const entity = this._entity;
  const isAvailable = entity.isAvailable(time);
 
  let attributes;
 
  let color;
  const show = new ShowGeometryInstanceAttribute(
    isAvailable &&
      entity.isShowing &&
      this._showProperty.getValue(time) &&
      this._fillProperty.getValue(time),
  );
  const distanceDisplayCondition =
    this._distanceDisplayConditionProperty.getValue(time);
  const distanceDisplayConditionAttribute =
    DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(
      distanceDisplayCondition,
    );
  if (this._materialProperty instanceof ColorMaterialProperty) {
    let currentColor;
    Eif (
      defined(this._materialProperty.color) &&
      (this._materialProperty.color.isConstant || isAvailable)
    ) {
      currentColor = this._materialProperty.color.getValue(time, scratchColor);
    }
    if (!defined(currentColor)) {
      currentColor = Color.WHITE;
    }
    color = ColorGeometryInstanceAttribute.fromColor(currentColor);
    attributes = {
      show: show,
      distanceDisplayCondition: distanceDisplayConditionAttribute,
      color: color,
    };
  } else {
    attributes = {
      show: show,
      distanceDisplayCondition: distanceDisplayConditionAttribute,
    };
  }
 
  const planeGraphics = entity.plane;
  const options = this._options;
  let modelMatrix = entity.computeModelMatrix(time);
  const plane = Property.getValueOrDefault(
    planeGraphics.plane,
    time,
    options.plane,
  );
  const dimensions = Property.getValueOrUndefined(
    planeGraphics.dimensions,
    time,
    options.dimensions,
  );
 
  options.plane = plane;
  options.dimensions = dimensions;
 
  modelMatrix = createPrimitiveMatrix(
    plane,
    dimensions,
    modelMatrix,
    modelMatrix,
  );
 
  return new GeometryInstance({
    id: entity,
    geometry: new PlaneGeometry(this._options),
    modelMatrix: modelMatrix,
    attributes: attributes,
  });
};
 
/**
 * Creates the geometry instance which represents the outline of the geometry.
 *
 * @param {JulianDate} time The time to use when retrieving initial attribute values.
 * @returns {GeometryInstance} The geometry instance representing the outline portion of the geometry.
 *
 * @exception {DeveloperError} This instance does not represent an outlined geometry.
 */
PlaneGeometryUpdater.prototype.createOutlineGeometryInstance = function (time) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("time", time);
 
  if (!this._outlineEnabled) {
    throw new DeveloperError(
      "This instance does not represent an outlined geometry.",
    );
  }
  //>>includeEnd('debug');
 
  const entity = this._entity;
  const isAvailable = entity.isAvailable(time);
  const outlineColor = Property.getValueOrDefault(
    this._outlineColorProperty,
    time,
    Color.BLACK,
    scratchColor,
  );
  const distanceDisplayCondition =
    this._distanceDisplayConditionProperty.getValue(time);
 
  const planeGraphics = entity.plane;
  const options = this._options;
  let modelMatrix = entity.computeModelMatrix(time);
  const plane = Property.getValueOrDefault(
    planeGraphics.plane,
    time,
    options.plane,
  );
  const dimensions = Property.getValueOrUndefined(
    planeGraphics.dimensions,
    time,
    options.dimensions,
  );
 
  options.plane = plane;
  options.dimensions = dimensions;
 
  modelMatrix = createPrimitiveMatrix(
    plane,
    dimensions,
    modelMatrix,
    modelMatrix,
  );
 
  return new GeometryInstance({
    id: entity,
    geometry: new PlaneOutlineGeometry(),
    modelMatrix: modelMatrix,
    attributes: {
      show: new ShowGeometryInstanceAttribute(
        isAvailable &&
          entity.isShowing &&
          this._showProperty.getValue(time) &&
          this._showOutlineProperty.getValue(time),
      ),
      color: ColorGeometryInstanceAttribute.fromColor(outlineColor),
      distanceDisplayCondition:
        DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(
          distanceDisplayCondition,
        ),
    },
  });
};
 
PlaneGeometryUpdater.prototype._isHidden = function (entity, plane) {
  return (
    !defined(plane.plane) ||
    !defined(plane.dimensions) ||
    !defined(entity.position) ||
    GeometryUpdater.prototype._isHidden.call(this, entity, plane)
  );
};
 
PlaneGeometryUpdater.prototype._getIsClosed = function (options) {
  return false;
};
 
PlaneGeometryUpdater.prototype._isDynamic = function (entity, plane) {
  return (
    !entity.position.isConstant || //
    !Property.isConstant(entity.orientation) || //
    !plane.plane.isConstant || //
    !plane.dimensions.isConstant || //
    !Property.isConstant(plane.outlineWidth)
  );
};
 
PlaneGeometryUpdater.prototype._setStaticOptions = function (entity, plane) {
  const isColorMaterial =
    this._materialProperty instanceof ColorMaterialProperty;
 
  const options = this._options;
  options.vertexFormat = isColorMaterial
    ? PerInstanceColorAppearance.VERTEX_FORMAT
    : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
  options.plane = plane.plane.getValue(Iso8601.MINIMUM_VALUE, options.plane);
  options.dimensions = plane.dimensions.getValue(
    Iso8601.MINIMUM_VALUE,
    options.dimensions,
  );
};
 
PlaneGeometryUpdater.DynamicGeometryUpdater = DynamicPlaneGeometryUpdater;
 
/**
 * @private
 */
function DynamicPlaneGeometryUpdater(
  geometryUpdater,
  primitives,
  groundPrimitives,
) {
  DynamicGeometryUpdater.call(
    this,
    geometryUpdater,
    primitives,
    groundPrimitives,
  );
}
 
Eif (defined(Object.create)) {
  DynamicPlaneGeometryUpdater.prototype = Object.create(
    DynamicGeometryUpdater.prototype,
  );
  DynamicPlaneGeometryUpdater.prototype.constructor =
    DynamicPlaneGeometryUpdater;
}
 
DynamicPlaneGeometryUpdater.prototype._isHidden = function (
  entity,
  plane,
  time,
) {
  const options = this._options;
  const position = Property.getValueOrUndefined(
    entity.position,
    time,
    positionScratch,
  );
  return (
    !defined(position) ||
    !defined(options.plane) ||
    !defined(options.dimensions) ||
    DynamicGeometryUpdater.prototype._isHidden.call(this, entity, plane, time)
  );
};
 
DynamicPlaneGeometryUpdater.prototype._setOptions = function (
  entity,
  plane,
  time,
) {
  const options = this._options;
  options.plane = Property.getValueOrDefault(plane.plane, time, options.plane);
  options.dimensions = Property.getValueOrUndefined(
    plane.dimensions,
    time,
    options.dimensions,
  );
};
 
const scratchAxis = new Cartesian3();
const scratchUp = new Cartesian3();
const scratchTranslation = new Cartesian3();
const scratchScale = new Cartesian3();
const scratchRotation = new Matrix3();
const scratchRotationScale = new Matrix3();
const scratchLocalTransform = new Matrix4();
function createPrimitiveMatrix(plane, dimensions, transform, result) {
  const normal = plane.normal;
  const distance = plane.distance;
 
  const translation = Cartesian3.multiplyByScalar(
    normal,
    -distance,
    scratchTranslation,
  );
 
  let up = Cartesian3.clone(Cartesian3.UNIT_Z, scratchUp);
  Iif (
    CesiumMath.equalsEpsilon(
      Math.abs(Cartesian3.dot(up, normal)),
      1.0,
      CesiumMath.EPSILON8,
    )
  ) {
    up = Cartesian3.clone(Cartesian3.UNIT_Y, up);
  }
 
  const left = Cartesian3.cross(up, normal, scratchAxis);
  up = Cartesian3.cross(normal, left, up);
  Cartesian3.normalize(left, left);
  Cartesian3.normalize(up, up);
 
  const rotationMatrix = scratchRotation;
  Matrix3.setColumn(rotationMatrix, 0, left, rotationMatrix);
  Matrix3.setColumn(rotationMatrix, 1, up, rotationMatrix);
  Matrix3.setColumn(rotationMatrix, 2, normal, rotationMatrix);
 
  const scale = Cartesian3.fromElements(
    dimensions.x,
    dimensions.y,
    1.0,
    scratchScale,
  );
  const rotationScaleMatrix = Matrix3.multiplyByScale(
    rotationMatrix,
    scale,
    scratchRotationScale,
  );
 
  const localTransform = Matrix4.fromRotationTranslation(
    rotationScaleMatrix,
    translation,
    scratchLocalTransform,
  );
  return Matrix4.multiplyTransformation(transform, localTransform, result);
}
 
/**
 * @private
 */
PlaneGeometryUpdater.createPrimitiveMatrix = createPrimitiveMatrix;
export default PlaneGeometryUpdater;