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 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 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 | 1818x 1818x 1818x 1818x 1817x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 3755x 3755x 3755x 3755x 3755x 3755x 3755x 1x 24x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1816x 1767x 1767x 1816x 52x 52x 52x 52x 1816x 22x 22x 22x 22x 1816x 1x 9x 2918x 8397x 1464x 2952x 1466x 1466x 1466x 5x 30x 30x 30x 6x 9x 3x 6x 6x 11x 1466x 1459x 7x 7x 3x 4x 1x 3x 3x 1451x 1451x 1451x 1451x 30x 30x 30x 30x 30x 63x 63x 59x 59x 59x 6x 6x 6x 15x 15x 7x 7x 7x 7x 7x 7x 7x 15x 15x 8x 8x 8x 8x 3x 3x 3x 9x 9x 7x 7x 1x 8341x 8341x 10x 10x 10x 8341x 1451x 1451x 8341x 8341x 6697x 83x 6697x 78x 8263x 240x 240x 64x 20x 64x 64x 64x 8199x 16x 16x 8183x 8183x 1x 13x 13x 13x 1x 8322x 8322x 8379x 8379x 15x 8354x 8331x 23x 23x 23x 23x 23x 23x 70x 46x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 10x 10x 10x 10x 10x 10x 10x 1767x 1767x 1767x 1767x 1767x 1767x 1767x 1767x 22x 22x 22x 22x 22x 2x 22x 22x 22x 22x 22x 22x 22x 22x 6x 6x 6x 22x 22x 22x 4x 22x 22x 22x 22x 22x 64x 64x 64x 64x 64x 8x 8x 8x 8x 8x 64x 64x 8x 52x 52x 52x 52x 52x 52x 52x 52x 52x 1x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x | import BoundingSphere from "../../Core/BoundingSphere.js";
import Cartesian2 from "../../Core/Cartesian2.js";
import CesiumMath from "../../Core/Math.js";
import Check from "../../Core/Check.js";
import clone from "../../Core/clone.js";
import Frozen from "../../Core/Frozen.js";
import defined from "../../Core/defined.js";
import Matrix4 from "../../Core/Matrix4.js";
import WebGLConstants from "../../Core/WebGLConstants.js";
import DrawCommand from "../../Renderer/DrawCommand.js";
import Pass from "../../Renderer/Pass.js";
import RenderState from "../../Renderer/RenderState.js";
import BlendingState from "../BlendingState.js";
import CullFace from "../CullFace.js";
import SceneMode from "../SceneMode.js";
import ShadowMode from "../ShadowMode.js";
import StencilConstants from "../StencilConstants.js";
import StencilFunction from "../StencilFunction.js";
import StencilOperation from "../StencilOperation.js";
import StyleCommandsNeeded from "./StyleCommandsNeeded.js";
/**
* A wrapper around the draw commands used to render a {@link ModelRuntimePrimitive}.
* This manages the derived commands and pushes only the necessary commands depending
* on the given frame state.
*
* @param {object} options An object containing the following options:
* @param {DrawCommand} options.command The draw command from which to derive other commands from.
* @param {PrimitiveRenderResources} options.primitiveRenderResources The render resources of the primitive associated with the command.
*
* @alias ModelDrawCommand
* @constructor
*
* @private
*/
function ModelDrawCommand(options) {
options = options ?? Frozen.EMPTY_OBJECT;
const command = options.command;
const renderResources = options.primitiveRenderResources;
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("options.command", command);
Check.typeOf.object("options.primitiveRenderResources", renderResources);
//>>includeEnd('debug');
const model = renderResources.model;
this._model = model;
const runtimePrimitive = renderResources.runtimePrimitive;
this._runtimePrimitive = runtimePrimitive;
// Store render resources for edge command creation
this._primitiveRenderResources = renderResources;
// If the command is translucent, or if the primitive's material is
// double-sided, then back-face culling is automatically disabled for
// the command. The user value for back-face culling will be ignored.
const isTranslucent = command.pass === Pass.TRANSLUCENT;
const isDoubleSided = runtimePrimitive.primitive.material.doubleSided;
const usesBackFaceCulling = !isDoubleSided && !isTranslucent;
const hasSilhouette = renderResources.hasSilhouette;
// If the command was already translucent, there's no need to derive a new
// translucent command. As of now, a style can't change an originally
// translucent feature to opaque since the style's alpha is modulated,
// not replaced. When this changes, we need to derive new opaque commands
// in initialize().
//
// Silhouettes for primitives with both opaque and translucent features
// are not yet supported.
const needsTranslucentCommand = !isTranslucent && !hasSilhouette;
const needsSkipLevelOfDetailCommands =
renderResources.hasSkipLevelOfDetail && !isTranslucent;
const needsSilhouetteCommands = hasSilhouette;
const needsEdgeCommands = defined(renderResources.edgeGeometry);
this._command = command;
// None of the derived commands (non-2D) use a different model matrix
// or bounding volume than the original, so they all point to the
// ModelDrawCommand's copy to save update time and memory.
this._modelMatrix = Matrix4.clone(command.modelMatrix);
this._boundingVolume = BoundingSphere.clone(command.boundingVolume);
// The 2D model matrix depends on the frame state's map projection,
// so it must be updated when the commands are handled in pushCommands.
this._modelMatrix2D = new Matrix4();
this._boundingVolume2D = new BoundingSphere();
this._modelMatrix2DDirty = false;
this._backFaceCulling = command.renderState.cull.enabled;
this._cullFace = command.renderState.cull.face;
this._shadows = model.shadows;
this._debugShowBoundingVolume = command.debugShowBoundingVolume;
this._usesBackFaceCulling = usesBackFaceCulling;
this._needsTranslucentCommand = needsTranslucentCommand;
this._needsSkipLevelOfDetailCommands = needsSkipLevelOfDetailCommands;
this._needsSilhouetteCommands = needsSilhouetteCommands;
this._needsEdgeCommands = needsEdgeCommands;
// Derived commands
this._originalCommand = undefined;
this._translucentCommand = undefined;
this._skipLodBackfaceCommand = undefined;
this._skipLodStencilCommand = undefined;
this._silhouetteModelCommand = undefined;
this._silhouetteColorCommand = undefined;
this._edgeCommand = undefined;
// All derived commands (including 2D commands)
this._derivedCommands = [];
this._has2DCommands = false;
initialize(this);
}
function ModelDerivedCommand(options) {
// The DrawCommand managed by this derived command.
this.command = options.command;
// These control whether the derived command should update the
// values of the DrawCommand for the corresponding properties.
this.updateShadows = options.updateShadows;
this.updateBackFaceCulling = options.updateBackFaceCulling;
this.updateCullFace = options.updateCullFace;
this.updateDebugShowBoundingVolume = options.updateDebugShowBoundingVolume;
// Whether this ModelDerivedCommand is in 2D.
this.is2D = options.is2D ?? false;
// A ModelDerivedCommand that is the 2D version of this one.
this.derivedCommand2D = undefined;
}
ModelDerivedCommand.clone = function (derivedCommand) {
return new ModelDerivedCommand({
command: derivedCommand.command,
updateShadows: derivedCommand.updateShadows,
updateBackFaceCulling: derivedCommand.updateBackFaceCulling,
updateCullFace: derivedCommand.updateCullFace,
updateDebugShowBoundingVolume: derivedCommand.updateDebugShowBoundingVolume,
is2D: derivedCommand.is2D,
derivedCommand2D: derivedCommand.derivedCommand2D,
});
};
function initialize(drawCommand) {
const command = drawCommand._command;
command.modelMatrix = drawCommand._modelMatrix;
command.boundingVolume = drawCommand._boundingVolume;
const model = drawCommand._model;
const usesBackFaceCulling = drawCommand._usesBackFaceCulling;
const derivedCommands = drawCommand._derivedCommands;
drawCommand._originalCommand = new ModelDerivedCommand({
command: command,
updateShadows: true,
updateBackFaceCulling: usesBackFaceCulling,
updateCullFace: usesBackFaceCulling,
updateDebugShowBoundingVolume: true,
is2D: false,
});
derivedCommands.push(drawCommand._originalCommand);
if (drawCommand._needsTranslucentCommand) {
drawCommand._translucentCommand = new ModelDerivedCommand({
command: deriveTranslucentCommand(command),
updateShadows: true,
updateBackFaceCulling: false,
updateCullFace: false,
updateDebugShowBoundingVolume: true,
});
derivedCommands.push(drawCommand._translucentCommand);
}
if (drawCommand._needsSkipLevelOfDetailCommands) {
drawCommand._skipLodBackfaceCommand = new ModelDerivedCommand({
command: deriveSkipLodBackfaceCommand(command),
updateShadows: false,
updateBackFaceCulling: false,
updateCullFace: usesBackFaceCulling,
updateDebugShowBoundingVolume: false,
});
drawCommand._skipLodStencilCommand = new ModelDerivedCommand({
command: deriveSkipLodStencilCommand(command, model),
updateShadows: true,
updateBackFaceCulling: usesBackFaceCulling,
updateCullFace: usesBackFaceCulling,
updateDebugShowBoundingVolume: true,
});
derivedCommands.push(drawCommand._skipLodBackfaceCommand);
derivedCommands.push(drawCommand._skipLodStencilCommand);
}
if (drawCommand._needsSilhouetteCommands) {
drawCommand._silhouetteModelCommand = new ModelDerivedCommand({
command: deriveSilhouetteModelCommand(command, model),
updateShadows: true,
updateBackFaceCulling: usesBackFaceCulling,
updateCullFace: usesBackFaceCulling,
updateDebugShowBoundingVolume: true,
});
drawCommand._silhouetteColorCommand = new ModelDerivedCommand({
command: deriveSilhouetteColorCommand(command, model),
updateShadows: false,
updateBackFaceCulling: false,
updateCullFace: false,
updateDebugShowBoundingVolume: false,
});
derivedCommands.push(drawCommand._silhouetteModelCommand);
derivedCommands.push(drawCommand._silhouetteColorCommand);
}
Iif (drawCommand._needsEdgeCommands) {
const renderResources = drawCommand._primitiveRenderResources;
drawCommand._edgeCommand = new ModelDerivedCommand({
command: deriveEdgeCommand(command, renderResources, model),
updateShadows: false,
updateBackFaceCulling: false,
updateCullFace: false,
updateDebugShowBoundingVolume: false,
});
derivedCommands.push(drawCommand._edgeCommand);
}
}
Object.defineProperties(ModelDrawCommand.prototype, {
/**
* The main draw command that the other commands are derived from.
*
* @memberof ModelDrawCommand.prototype
* @type {DrawCommand}
*
* @readonly
* @private
*/
command: {
get: function () {
return this._command;
},
},
/**
* The runtime primitive that the draw command belongs to.
*
* @memberof ModelDrawCommand.prototype
* @type {ModelRuntimePrimitive}
*
* @readonly
* @private
*/
runtimePrimitive: {
get: function () {
return this._runtimePrimitive;
},
},
/**
* The model that the draw command belongs to.
*
* @memberof ModelDrawCommand.prototype
* @type {Model}
*
* @readonly
* @private
*/
model: {
get: function () {
return this._model;
},
},
/**
* The primitive type of the draw command.
*
* @memberof ModelDrawCommand.prototype
* @type {PrimitiveType}
*
* @readonly
* @private
*/
primitiveType: {
get: function () {
return this._command.primitiveType;
},
},
/**
* The current model matrix applied to the draw commands. If there are
* 2D draw commands, their model matrix will be derived from the 3D one.
*
* @memberof ModelDrawCommand.prototype
* @type {Matrix4}
*
* @readonly
* @private
*/
modelMatrix: {
get: function () {
return this._modelMatrix;
},
set: function (value) {
this._modelMatrix = Matrix4.clone(value, this._modelMatrix);
this._modelMatrix2DDirty = true;
this._boundingVolume = BoundingSphere.transform(
this.runtimePrimitive.boundingSphere,
this._modelMatrix,
this._boundingVolume,
);
},
},
/**
* The bounding volume of the main draw command. This is equivalent
* to the primitive's bounding sphere transformed by the draw
* command's model matrix.
*
* @memberof ModelDrawCommand.prototype
* @type {BoundingSphere}
*
* @readonly
* @private
*/
boundingVolume: {
get: function () {
return this._boundingVolume;
},
},
/**
* Whether the geometry casts or receives shadows from light sources.
*
* @memberof ModelDrawCommand.prototype
* @type {ShadowMode}
*
* @private
*/
shadows: {
get: function () {
return this._shadows;
},
set: function (value) {
this._shadows = value;
updateShadows(this);
},
},
/**
* Whether to cull back-facing geometry. When true, back face culling is
* determined by the material's doubleSided property; when false, back face
* culling is disabled. Back faces are not culled if the command is
* translucent.
*
* @memberof ModelDrawCommand.prototype
* @type {boolean}
*
* @private
*/
backFaceCulling: {
get: function () {
return this._backFaceCulling;
},
set: function (value) {
if (this._backFaceCulling === value) {
return;
}
this._backFaceCulling = value;
updateBackFaceCulling(this);
},
},
/**
* Determines which faces to cull, if culling is enabled.
*
* @memberof ModelDrawCommand.prototype
* @type {CullFace}
*
* @private
*/
cullFace: {
get: function () {
return this._cullFace;
},
set: function (value) {
if (this._cullFace === value) {
return;
}
this._cullFace = value;
updateCullFace(this);
},
},
/**
* Whether to draw the bounding sphere associated with this draw command.
*
* @memberof ModelDrawCommand.prototype
* @type {boolean}
*
* @private
*/
debugShowBoundingVolume: {
get: function () {
return this._debugShowBoundingVolume;
},
set: function (value) {
if (this._debugShowBoundingVolume === value) {
return;
}
this._debugShowBoundingVolume = value;
updateDebugShowBoundingVolume(this);
},
},
});
function updateModelMatrix2D(drawCommand, frameState) {
const modelMatrix = drawCommand._modelMatrix;
drawCommand._modelMatrix2D = Matrix4.clone(
modelMatrix,
drawCommand._modelMatrix2D,
);
// Change the translation's y-component so it appears on the opposite side
// of the map.
drawCommand._modelMatrix2D[13] -=
CesiumMath.sign(modelMatrix[13]) *
2.0 *
CesiumMath.PI *
frameState.mapProjection.ellipsoid.maximumRadius;
drawCommand._boundingVolume2D = BoundingSphere.transform(
drawCommand.runtimePrimitive.boundingSphere,
drawCommand._modelMatrix2D,
drawCommand._boundingVolume2D,
);
}
function updateShadows(drawCommand) {
const shadows = drawCommand.shadows;
const castShadows = ShadowMode.castShadows(shadows);
const receiveShadows = ShadowMode.receiveShadows(shadows);
const derivedCommands = drawCommand._derivedCommands;
for (let i = 0; i < derivedCommands.length; ++i) {
const derivedCommand = derivedCommands[i];
if (derivedCommand.updateShadows) {
const command = derivedCommand.command;
command.castShadows = castShadows;
command.receiveShadows = receiveShadows;
}
}
}
function updateBackFaceCulling(drawCommand) {
const backFaceCulling = drawCommand.backFaceCulling;
const derivedCommands = drawCommand._derivedCommands;
for (let i = 0; i < derivedCommands.length; ++i) {
const derivedCommand = derivedCommands[i];
if (derivedCommand.updateBackFaceCulling) {
const command = derivedCommand.command;
const renderState = clone(command.renderState, true);
renderState.cull.enabled = backFaceCulling;
command.renderState = RenderState.fromCache(renderState);
}
}
}
function updateCullFace(drawCommand) {
const cullFace = drawCommand.cullFace;
const derivedCommands = drawCommand._derivedCommands;
for (let i = 0; i < derivedCommands.length; ++i) {
const derivedCommand = derivedCommands[i];
if (derivedCommand.updateCullFace) {
const command = derivedCommand.command;
const renderState = clone(command.renderState, true);
renderState.cull.face = cullFace;
command.renderState = RenderState.fromCache(renderState);
}
}
}
function updateDebugShowBoundingVolume(drawCommand) {
const debugShowBoundingVolume = drawCommand.debugShowBoundingVolume;
const derivedCommands = drawCommand._derivedCommands;
for (let i = 0; i < derivedCommands.length; ++i) {
const derivedCommand = derivedCommands[i];
if (derivedCommand.updateDebugShowBoundingVolume) {
const command = derivedCommand.command;
command.debugShowBoundingVolume = debugShowBoundingVolume;
}
}
}
/**
* Pushes the draw commands necessary to render the primitive.
* This does not include the draw commands that render its silhouette.
*
* @param {FrameState} frameState The frame state.
* @param {DrawCommand[]} result The array to push the draw commands to.
*
* @returns {DrawCommand[]} The modified result parameter.
*
* @private
*/
ModelDrawCommand.prototype.pushCommands = function (frameState, result) {
const use2D = shouldUse2DCommands(this, frameState);
if (use2D && !this._has2DCommands) {
derive2DCommands(this);
this._has2DCommands = true;
this._modelMatrix2DDirty = true;
}
if (this._modelMatrix2DDirty) {
updateModelMatrix2D(this, frameState);
this._modelMatrix2DDirty = false;
}
const styleCommandsNeeded = this.model.styleCommandsNeeded;
if (this._needsTranslucentCommand && defined(styleCommandsNeeded)) {
// StyleCommandsNeeded has three values: all opaque, all translucent, or both.
if (styleCommandsNeeded !== StyleCommandsNeeded.ALL_OPAQUE) {
pushCommand(result, this._translucentCommand, use2D);
}
// Continue only if opaque commands are needed.
if (styleCommandsNeeded === StyleCommandsNeeded.ALL_TRANSLUCENT) {
return;
}
}
if (this._needsSkipLevelOfDetailCommands) {
const { tileset, tile } = this._model.content;
if (tileset.hasMixedContent) {
if (!tile._finalResolution) {
pushCommand(
tileset._backfaceCommands,
this._skipLodBackfaceCommand,
use2D,
);
}
updateSkipLodStencilCommand(this, tile, use2D);
pushCommand(result, this._skipLodStencilCommand, use2D);
return;
}
}
if (this._needsSilhouetteCommands) {
pushCommand(result, this._silhouetteModelCommand, use2D);
return;
}
pushCommand(result, this._originalCommand, use2D);
return result;
};
/**
* Pushes the draw commands necessary to render the silhouette. These should
* be added to the command list after the draw commands of all primitives
* in the model have been added. This way, the silhouette won't render on
* top of the model.
* <p>
* This should only be called after pushCommands() has been invoked for
* the ModelDrawCommand this frame. Otherwise, the silhouette commands may
* not have been derived for 2D. The model matrix will also not have been
* updated for 2D commands.
* </p>
*
* @param {FrameState} frameState The frame state.
* @param {DrawCommand[]} result The array to push the silhouette commands to.
*
* @returns {DrawCommand[]} The modified result parameter.
*
* @private
*/
ModelDrawCommand.prototype.pushSilhouetteCommands = function (
frameState,
result,
) {
const use2D = shouldUse2DCommands(this, frameState);
pushCommand(result, this._silhouetteColorCommand, use2D);
return result;
};
/**
* @param {FrameState} frameState The frame state.
* @param {DrawCommand[]} result The draw commands to push to.
* @returns {DrawCommand[]} The modified command list.
*
* @private
*/
ModelDrawCommand.prototype.pushEdgeCommands = function (frameState, result) {
Eif (!defined(this._edgeCommand)) {
return result;
}
const use2D = shouldUse2DCommands(this, frameState);
pushCommand(result, this._edgeCommand, use2D);
return result;
};
function pushCommand(commandList, derivedCommand, use2D) {
commandList.push(derivedCommand.command);
if (use2D) {
commandList.push(derivedCommand.derivedCommand2D.command);
}
}
function shouldUse2DCommands(drawCommand, frameState) {
if (frameState.mode !== SceneMode.SCENE2D || drawCommand.model._projectTo2D) {
return false;
}
// The draw command's bounding sphere might cause primitives not to render
// over the IDL, even if they are part of the same model. Use the scene graph's
// bounding sphere instead.
const model = drawCommand.model;
const boundingSphere = model.sceneGraph._boundingSphere2D;
const left = boundingSphere.center.y - boundingSphere.radius;
const right = boundingSphere.center.y + boundingSphere.radius;
const idl2D =
frameState.mapProjection.ellipsoid.maximumRadius * CesiumMath.PI;
return (left < idl2D && right > idl2D) || (left < -idl2D && right > -idl2D);
}
function derive2DCommand(drawCommand, derivedCommand) {
if (!defined(derivedCommand)) {
return;
}
// If the model crosses the IDL in 2D, it will be drawn in one viewport but get
// clipped by the other viewport. We create a second command that translates
// the model matrix to the opposite side of the map so the part that was clipped
// in one viewport is drawn in the other.
const derivedCommand2D = ModelDerivedCommand.clone(derivedCommand);
const command2D = DrawCommand.shallowClone(derivedCommand.command);
command2D.modelMatrix = drawCommand._modelMatrix2D;
command2D.boundingVolume = drawCommand._boundingVolume2D;
derivedCommand2D.command = command2D;
derivedCommand2D.updateShadows = false; // Shadows are disabled for 2D
derivedCommand2D.is2D = true;
derivedCommand.derivedCommand2D = derivedCommand2D;
drawCommand._derivedCommands.push(derivedCommand2D);
return derivedCommand2D;
}
function derive2DCommands(drawCommand) {
derive2DCommand(drawCommand, drawCommand._originalCommand);
derive2DCommand(drawCommand, drawCommand._translucentCommand);
derive2DCommand(drawCommand, drawCommand._skipLodBackfaceCommand);
derive2DCommand(drawCommand, drawCommand._skipLodStencilCommand);
derive2DCommand(drawCommand, drawCommand._silhouetteModelCommand);
derive2DCommand(drawCommand, drawCommand._silhouetteColorCommand);
derive2DCommand(drawCommand, drawCommand._edgeCommand);
}
function deriveTranslucentCommand(command) {
const derivedCommand = DrawCommand.shallowClone(command);
derivedCommand.pass = Pass.TRANSLUCENT;
const rs = clone(command.renderState, true);
rs.cull.enabled = false;
rs.depthMask = false;
rs.blending = BlendingState.ALPHA_BLEND;
derivedCommand.renderState = RenderState.fromCache(rs);
return derivedCommand;
}
function deriveSilhouetteModelCommand(command, model) {
// Wrap around after exceeding the 8-bit stencil limit.
// The reference is unique to each model until this point.
const stencilReference = model._silhouetteId % 255;
const silhouetteModelCommand = DrawCommand.shallowClone(command);
const renderState = clone(command.renderState, true);
// Write the reference value into the stencil buffer.
renderState.stencilTest = {
enabled: true,
frontFunction: WebGLConstants.ALWAYS,
backFunction: WebGLConstants.ALWAYS,
reference: stencilReference,
mask: ~0,
frontOperation: {
fail: WebGLConstants.KEEP,
zFail: WebGLConstants.KEEP,
zPass: WebGLConstants.REPLACE,
},
backOperation: {
fail: WebGLConstants.KEEP,
zFail: WebGLConstants.KEEP,
zPass: WebGLConstants.REPLACE,
},
};
if (model.isInvisible()) {
renderState.colorMask = {
red: false,
green: false,
blue: false,
alpha: false,
};
}
silhouetteModelCommand.renderState = RenderState.fromCache(renderState);
return silhouetteModelCommand;
}
function deriveSilhouetteColorCommand(command, model) {
// Wrap around after exceeding the 8-bit stencil limit.
// The reference is unique to each model until this point.
const stencilReference = model._silhouetteId % 255;
const silhouetteColorCommand = DrawCommand.shallowClone(command);
const renderState = clone(command.renderState, true);
renderState.cull.enabled = false;
// Render the silhouette in the translucent pass if either the command
// pass or the silhouette color is translucent. This will account for
// translucent model color, since ModelColorPipelineStage sets the pass
// to translucent.
const silhouetteTranslucent =
command.pass === Pass.TRANSLUCENT || model.silhouetteColor.alpha < 1.0;
if (silhouetteTranslucent) {
silhouetteColorCommand.pass = Pass.TRANSLUCENT;
renderState.depthMask = false;
renderState.blending = BlendingState.ALPHA_BLEND;
}
// Only render the pixels of the silhouette that don't conflict with
// the stencil buffer. This way, the silhouette doesn't render over
// the original model.
renderState.stencilTest = {
enabled: true,
frontFunction: WebGLConstants.NOTEQUAL,
backFunction: WebGLConstants.NOTEQUAL,
reference: stencilReference,
mask: ~0,
frontOperation: {
fail: WebGLConstants.KEEP,
zFail: WebGLConstants.KEEP,
zPass: WebGLConstants.KEEP,
},
backOperation: {
fail: WebGLConstants.KEEP,
zFail: WebGLConstants.KEEP,
zPass: WebGLConstants.KEEP,
},
};
const uniformMap = clone(command.uniformMap);
uniformMap.model_silhouettePass = function () {
return true;
};
silhouetteColorCommand.renderState = RenderState.fromCache(renderState);
silhouetteColorCommand.uniformMap = uniformMap;
silhouetteColorCommand.castShadows = false;
silhouetteColorCommand.receiveShadows = false;
return silhouetteColorCommand;
}
function deriveEdgeCommand(command, renderResources) {
const edgeGeometry = renderResources.edgeGeometry;
const edgeCommand = DrawCommand.shallowClone(command);
// Use the edge geometry instead of the original geometry
edgeCommand.vertexArray = edgeGeometry.vertexArray;
edgeCommand.primitiveType = edgeGeometry.primitiveType;
edgeCommand.count = edgeGeometry.indexCount;
// Use the edge shader program if available
if (defined(edgeGeometry.shaderProgram)) {
edgeCommand.shaderProgram = edgeGeometry.shaderProgram;
}
// Set pass for edge rendering (use the pass specified in edgeGeometry)
edgeCommand.pass = edgeGeometry.pass;
// Override uniformMap to set u_isEdgePass to true for the edge pass
const uniformMap = clone(command.uniformMap);
uniformMap.u_isEdgePass = function () {
return true; // This is the edge pass
};
edgeCommand.uniformMap = uniformMap;
edgeCommand.castShadows = false;
edgeCommand.receiveShadows = false;
return edgeCommand;
}
function updateSkipLodStencilCommand(drawCommand, tile, use2D) {
const stencilDerivedComand = drawCommand._skipLodStencilCommand;
const stencilCommand = stencilDerivedComand.command;
const selectionDepth = tile._selectionDepth;
const lastSelectionDepth = getLastSelectionDepth(stencilCommand);
if (selectionDepth !== lastSelectionDepth) {
const skipLodStencilReference = getStencilReference(selectionDepth);
const renderState = clone(stencilCommand.renderState, true);
renderState.stencilTest.reference = skipLodStencilReference;
stencilCommand.renderState = RenderState.fromCache(renderState);
Iif (use2D) {
stencilDerivedComand.derivedCommand2D.renderState = renderState;
}
}
}
function getLastSelectionDepth(stencilCommand) {
// Isolate the selection depth from the stencil reference.
const reference = stencilCommand.renderState.stencilTest.reference;
return (
(reference & StencilConstants.SKIP_LOD_MASK) >>>
StencilConstants.SKIP_LOD_BIT_SHIFT
);
}
function getStencilReference(selectionDepth) {
// Stencil test is masked to the most significant 3 bits so the reference is shifted.
// Writes 0 for the terrain bit.
return (
StencilConstants.CESIUM_3D_TILE_MASK |
(selectionDepth << StencilConstants.SKIP_LOD_BIT_SHIFT)
);
}
function deriveSkipLodBackfaceCommand(command) {
// Write just backface depth of unresolved tiles so resolved stenciled tiles
// do not appear in front.
const backfaceCommand = DrawCommand.shallowClone(command);
const renderState = clone(command.renderState, true);
renderState.cull.enabled = true;
renderState.cull.face = CullFace.FRONT;
// Back faces do not need to write color.
renderState.colorMask = {
red: false,
green: false,
blue: false,
alpha: false,
};
// Push back face depth away from the camera so it is less likely that back faces and front faces of the same tile
// intersect and overlap. This helps avoid flickering for very thin double-sided walls.
renderState.polygonOffset = {
enabled: true,
factor: 5.0,
units: 5.0,
};
// The stencil test is set in TilesetPipelineStage.
const uniformMap = clone(backfaceCommand.uniformMap);
const polygonOffset = new Cartesian2(5.0, 5.0);
uniformMap.u_polygonOffset = function () {
return polygonOffset;
};
backfaceCommand.renderState = RenderState.fromCache(renderState);
backfaceCommand.uniformMap = uniformMap;
backfaceCommand.castShadows = false;
backfaceCommand.receiveShadows = false;
return backfaceCommand;
}
function deriveSkipLodStencilCommand(command) {
// Tiles only draw if their selection depth is >= the tile drawn already. They write their
// selection depth to the stencil buffer to prevent ancestor tiles from drawing on top
const stencilCommand = DrawCommand.shallowClone(command);
const renderState = clone(command.renderState, true);
// The stencil reference is updated dynamically; see updateSkipLodStencilCommand().
const { stencilTest } = renderState;
stencilTest.enabled = true;
stencilTest.mask = StencilConstants.SKIP_LOD_MASK;
stencilTest.reference = StencilConstants.CESIUM_3D_TILE_MASK;
stencilTest.frontFunction = StencilFunction.GREATER_OR_EQUAL;
stencilTest.frontOperation.zPass = StencilOperation.REPLACE;
stencilTest.backFunction = StencilFunction.GREATER_OR_EQUAL;
stencilTest.backOperation.zPass = StencilOperation.REPLACE;
renderState.stencilMask =
StencilConstants.CESIUM_3D_TILE_MASK | StencilConstants.SKIP_LOD_MASK;
stencilCommand.renderState = RenderState.fromCache(renderState);
return stencilCommand;
}
export default ModelDrawCommand;
|