DrawCommand.js 14.3 KB
Newer Older
Manggar Mahardhika's avatar
Manggar Mahardhika committed
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 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
import defaultValue from "../Core/defaultValue.js";
import defined from "../Core/defined.js";
import PrimitiveType from "../Core/PrimitiveType.js";

/**
 * Represents a command to the renderer for drawing.
 *
 * @private
 */
function DrawCommand(options) {
  options = defaultValue(options, defaultValue.EMPTY_OBJECT);

  this._boundingVolume = options.boundingVolume;
  this._orientedBoundingBox = options.orientedBoundingBox;
  this._cull = defaultValue(options.cull, true);
  this._occlude = defaultValue(options.occlude, true);
  this._modelMatrix = options.modelMatrix;
  this._primitiveType = defaultValue(
    options.primitiveType,
    PrimitiveType.TRIANGLES
  );
  this._vertexArray = options.vertexArray;
  this._count = options.count;
  this._offset = defaultValue(options.offset, 0);
  this._instanceCount = defaultValue(options.instanceCount, 0);
  this._shaderProgram = options.shaderProgram;
  this._uniformMap = options.uniformMap;
  this._renderState = options.renderState;
  this._framebuffer = options.framebuffer;
  this._pass = options.pass;
  this._executeInClosestFrustum = defaultValue(
    options.executeInClosestFrustum,
    false
  );
  this._owner = options.owner;
  this._debugShowBoundingVolume = defaultValue(
    options.debugShowBoundingVolume,
    false
  );
  this._debugOverlappingFrustums = 0;
  this._castShadows = defaultValue(options.castShadows, false);
  this._receiveShadows = defaultValue(options.receiveShadows, false);
  this._pickId = options.pickId;
  this._pickOnly = defaultValue(options.pickOnly, false);

  this._depthForTranslucentClassification = defaultValue(
    options.depthForTranslucentClassification,
    false
  );

  this.dirty = true;
  this.lastDirtyTime = 0;

  /**
   * @private
   */
  this.derivedCommands = {};
}

Object.defineProperties(DrawCommand.prototype, {
  /**
   * The bounding volume of the geometry in world space.  This is used for culling and frustum selection.
   * <p>
   * For best rendering performance, use the tightest possible bounding volume.  Although
   * <code>undefined</code> is allowed, always try to provide a bounding volume to
   * allow the tightest possible near and far planes to be computed for the scene, and
   * minimize the number of frustums needed.
   * </p>
   *
   * @memberof DrawCommand.prototype
   * @type {Object}
   * @default undefined
   *
   * @see DrawCommand#debugShowBoundingVolume
   */
  boundingVolume: {
    get: function () {
      return this._boundingVolume;
    },
    set: function (value) {
      if (this._boundingVolume !== value) {
        this._boundingVolume = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The oriented bounding box of the geometry in world space. If this is defined, it is used instead of
   * {@link DrawCommand#boundingVolume} for plane intersection testing.
   *
   * @memberof DrawCommand.prototype
   * @type {OrientedBoundingBox}
   * @default undefined
   *
   * @see DrawCommand#debugShowBoundingVolume
   */
  orientedBoundingBox: {
    get: function () {
      return this._orientedBoundingBox;
    },
    set: function (value) {
      if (this._orientedBoundingBox !== value) {
        this._orientedBoundingBox = value;
        this.dirty = true;
      }
    },
  },

  /**
   * When <code>true</code>, the renderer frustum and horizon culls the command based on its {@link DrawCommand#boundingVolume}.
   * If the command was already culled, set this to <code>false</code> for a performance improvement.
   *
   * @memberof DrawCommand.prototype
   * @type {Boolean}
   * @default true
   */
  cull: {
    get: function () {
      return this._cull;
    },
    set: function (value) {
      if (this._cull !== value) {
        this._cull = value;
        this.dirty = true;
      }
    },
  },

  /**
   * When <code>true</code>, the horizon culls the command based on its {@link DrawCommand#boundingVolume}.
   * {@link DrawCommand#cull} must also be <code>true</code> in order for the command to be culled.
   *
   * @memberof DrawCommand.prototype
   * @type {Boolean}
   * @default true
   */
  occlude: {
    get: function () {
      return this._occlude;
    },
    set: function (value) {
      if (this._occlude !== value) {
        this._occlude = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The transformation from the geometry in model space to world space.
   * <p>
   * When <code>undefined</code>, the geometry is assumed to be defined in world space.
   * </p>
   *
   * @memberof DrawCommand.prototype
   * @type {Matrix4}
   * @default undefined
   */
  modelMatrix: {
    get: function () {
      return this._modelMatrix;
    },
    set: function (value) {
      if (this._modelMatrix !== value) {
        this._modelMatrix = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The type of geometry in the vertex array.
   *
   * @memberof DrawCommand.prototype
   * @type {PrimitiveType}
   * @default PrimitiveType.TRIANGLES
   */
  primitiveType: {
    get: function () {
      return this._primitiveType;
    },
    set: function (value) {
      if (this._primitiveType !== value) {
        this._primitiveType = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The vertex array.
   *
   * @memberof DrawCommand.prototype
   * @type {VertexArray}
   * @default undefined
   */
  vertexArray: {
    get: function () {
      return this._vertexArray;
    },
    set: function (value) {
      if (this._vertexArray !== value) {
        this._vertexArray = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The number of vertices to draw in the vertex array.
   *
   * @memberof DrawCommand.prototype
   * @type {Number}
   * @default undefined
   */
  count: {
    get: function () {
      return this._count;
    },
    set: function (value) {
      if (this._count !== value) {
        this._count = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The offset to start drawing in the vertex array.
   *
   * @memberof DrawCommand.prototype
   * @type {Number}
   * @default 0
   */
  offset: {
    get: function () {
      return this._offset;
    },
    set: function (value) {
      if (this._offset !== value) {
        this._offset = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The number of instances to draw.
   *
   * @memberof DrawCommand.prototype
   * @type {Number}
   * @default 0
   */
  instanceCount: {
    get: function () {
      return this._instanceCount;
    },
    set: function (value) {
      if (this._instanceCount !== value) {
        this._instanceCount = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The shader program to apply.
   *
   * @memberof DrawCommand.prototype
   * @type {ShaderProgram}
   * @default undefined
   */
  shaderProgram: {
    get: function () {
      return this._shaderProgram;
    },
    set: function (value) {
      if (this._shaderProgram !== value) {
        this._shaderProgram = value;
        this.dirty = true;
      }
    },
  },

  /**
   * Whether this command should cast shadows when shadowing is enabled.
   *
   * @memberof DrawCommand.prototype
   * @type {Boolean}
   * @default false
   */
  castShadows: {
    get: function () {
      return this._castShadows;
    },
    set: function (value) {
      if (this._castShadows !== value) {
        this._castShadows = value;
        this.dirty = true;
      }
    },
  },

  /**
   * Whether this command should receive shadows when shadowing is enabled.
   *
   * @memberof DrawCommand.prototype
   * @type {Boolean}
   * @default false
   */
  receiveShadows: {
    get: function () {
      return this._receiveShadows;
    },
    set: function (value) {
      if (this._receiveShadows !== value) {
        this._receiveShadows = value;
        this.dirty = true;
      }
    },
  },

  /**
   * An object with functions whose names match the uniforms in the shader program
   * and return values to set those uniforms.
   *
   * @memberof DrawCommand.prototype
   * @type {Object}
   * @default undefined
   */
  uniformMap: {
    get: function () {
      return this._uniformMap;
    },
    set: function (value) {
      if (this._uniformMap !== value) {
        this._uniformMap = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The render state.
   *
   * @memberof DrawCommand.prototype
   * @type {RenderState}
   * @default undefined
   */
  renderState: {
    get: function () {
      return this._renderState;
    },
    set: function (value) {
      if (this._renderState !== value) {
        this._renderState = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The framebuffer to draw to.
   *
   * @memberof DrawCommand.prototype
   * @type {Framebuffer}
   * @default undefined
   */
  framebuffer: {
    get: function () {
      return this._framebuffer;
    },
    set: function (value) {
      if (this._framebuffer !== value) {
        this._framebuffer = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The pass when to render.
   *
   * @memberof DrawCommand.prototype
   * @type {Pass}
   * @default undefined
   */
  pass: {
    get: function () {
      return this._pass;
    },
    set: function (value) {
      if (this._pass !== value) {
        this._pass = value;
        this.dirty = true;
      }
    },
  },

  /**
   * Specifies if this command is only to be executed in the frustum closest
   * to the eye containing the bounding volume. Defaults to <code>false</code>.
   *
   * @memberof DrawCommand.prototype
   * @type {Boolean}
   * @default false
   */
  executeInClosestFrustum: {
    get: function () {
      return this._executeInClosestFrustum;
    },
    set: function (value) {
      if (this._executeInClosestFrustum !== value) {
        this._executeInClosestFrustum = value;
        this.dirty = true;
      }
    },
  },

  /**
   * The object who created this command.  This is useful for debugging command
   * execution; it allows us to see who created a command when we only have a
   * reference to the command, and can be used to selectively execute commands
   * with {@link Scene#debugCommandFilter}.
   *
   * @memberof DrawCommand.prototype
   * @type {Object}
   * @default undefined
   *
   * @see Scene#debugCommandFilter
   */
  owner: {
    get: function () {
      return this._owner;
    },
    set: function (value) {
      if (this._owner !== value) {
        this._owner = value;
        this.dirty = true;
      }
    },
  },

  /**
   * This property is for debugging only; it is not for production use nor is it optimized.
   * <p>
   * Draws the {@link DrawCommand#boundingVolume} for this command, assuming it is a sphere, when the command executes.
   * </p>
   *
   * @memberof DrawCommand.prototype
   * @type {Boolean}
   * @default false
   *
   * @see DrawCommand#boundingVolume
   */
  debugShowBoundingVolume: {
    get: function () {
      return this._debugShowBoundingVolume;
    },
    set: function (value) {
      if (this._debugShowBoundingVolume !== value) {
        this._debugShowBoundingVolume = value;
        this.dirty = true;
      }
    },
  },

  /**
   * Used to implement Scene.debugShowFrustums.
   * @private
   */
  debugOverlappingFrustums: {
    get: function () {
      return this._debugOverlappingFrustums;
    },
    set: function (value) {
      if (this._debugOverlappingFrustums !== value) {
        this._debugOverlappingFrustums = value;
        this.dirty = true;
      }
    },
  },
  /**
   * A GLSL string that will evaluate to a pick id. When <code>undefined</code>, the command will only draw depth
   * during the pick pass.
   *
   * @memberof DrawCommand.prototype
   * @type {String}
   * @default undefined
   */
  pickId: {
    get: function () {
      return this._pickId;
    },
    set: function (value) {
      if (this._pickId !== value) {
        this._pickId = value;
        this.dirty = true;
      }
    },
  },
  /**
   * Whether this command should be executed in the pick pass only.
   *
   * @memberof DrawCommand.prototype
   * @type {Boolean}
   * @default false
   */
  pickOnly: {
    get: function () {
      return this._pickOnly;
    },
    set: function (value) {
      if (this._pickOnly !== value) {
        this._pickOnly = value;
        this.dirty = true;
      }
    },
  },
  /**
   * Whether this command should be derived to draw depth for classification of translucent primitives.
   *
   * @memberof DrawCommand.prototype
   * @type {Boolean}
   * @default false
   */
  depthForTranslucentClassification: {
    get: function () {
      return this._depthForTranslucentClassification;
    },
    set: function (value) {
      if (this._depthForTranslucentClassification !== value) {
        this._depthForTranslucentClassification = value;
        this.dirty = true;
      }
    },
  },
});

/**
 * @private
 */
DrawCommand.shallowClone = function (command, result) {
  if (!defined(command)) {
    return undefined;
  }
  if (!defined(result)) {
    result = new DrawCommand();
  }

  result._boundingVolume = command._boundingVolume;
  result._orientedBoundingBox = command._orientedBoundingBox;
  result._cull = command._cull;
  result._occlude = command._occlude;
  result._modelMatrix = command._modelMatrix;
  result._primitiveType = command._primitiveType;
  result._vertexArray = command._vertexArray;
  result._count = command._count;
  result._offset = command._offset;
  result._instanceCount = command._instanceCount;
  result._shaderProgram = command._shaderProgram;
  result._uniformMap = command._uniformMap;
  result._renderState = command._renderState;
  result._framebuffer = command._framebuffer;
  result._pass = command._pass;
  result._executeInClosestFrustum = command._executeInClosestFrustum;
  result._owner = command._owner;
  result._debugShowBoundingVolume = command._debugShowBoundingVolume;
  result._debugOverlappingFrustums = command._debugOverlappingFrustums;
  result._castShadows = command._castShadows;
  result._receiveShadows = command._receiveShadows;
  result._pickId = command._pickId;
  result._pickOnly = command._pickOnly;
  result._depthForTranslucentClassification =
    command._depthForTranslucentClassification;

  result.dirty = true;
  result.lastDirtyTime = 0;

  return result;
};

/**
 * Executes the draw command.
 *
 * @param {Context} context The renderer context in which to draw.
 * @param {PassState} [passState] The state for the current render pass.
 */
DrawCommand.prototype.execute = function (context, passState) {
  context.draw(this, passState);
};
export default DrawCommand;