Skip to content

Commit

Permalink
Merge pull request #9447 from CesiumGS/translucent-crash
Browse files Browse the repository at this point in the history
Fix classification crash when toggling translucency
  • Loading branch information
lilleyse authored Mar 29, 2021
2 parents c88d070 + 83ca0bb commit 73d886b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
2 changes: 2 additions & 0 deletions Source/Scene/Cesium3DTileBatchTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,8 @@ function getTranslucentRenderState(renderState) {
rs.depthTest.enabled = true;
rs.depthMask = false;
rs.blending = BlendingState.ALPHA_BLEND;
rs.stencilTest = StencilConstants.setCesium3DTileBit();
rs.stencilMask = StencilConstants.CESIUM_3D_TILE_MASK;

return RenderState.fromCache(rs);
}
Expand Down
17 changes: 14 additions & 3 deletions Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import processModelMaterialsCommon from "./processModelMaterialsCommon.js";
import processPbrMaterials from "./processPbrMaterials.js";
import SceneMode from "./SceneMode.js";
import ShadowMode from "./ShadowMode.js";
import StencilConstants from "./StencilConstants.js";

var boundingSphereCartesian3Scratch = new Cartesian3();

Expand Down Expand Up @@ -4482,20 +4483,26 @@ function updateShadows(model) {
}
}

function getTranslucentRenderState(renderState) {
function getTranslucentRenderState(model, renderState) {
var rs = clone(renderState, true);
rs.cull.enabled = false;
rs.depthTest.enabled = true;
rs.depthMask = false;
rs.blending = BlendingState.ALPHA_BLEND;

if (model.opaquePass === Pass.CESIUM_3D_TILE) {
rs.stencilTest = StencilConstants.setCesium3DTileBit();
rs.stencilMask = StencilConstants.CESIUM_3D_TILE_MASK;
}

return RenderState.fromCache(rs);
}

function deriveTranslucentCommand(command) {
function deriveTranslucentCommand(model, command) {
var translucentCommand = DrawCommand.shallowClone(command);
translucentCommand.pass = Pass.TRANSLUCENT;
translucentCommand.renderState = getTranslucentRenderState(
model,
command.renderState
);
return translucentCommand;
Expand All @@ -4515,10 +4522,14 @@ function updateColor(model, frameState, forceDerive) {
for (var i = 0; i < length; ++i) {
var nodeCommand = nodeCommands[i];
var command = nodeCommand.command;
nodeCommand.translucentCommand = deriveTranslucentCommand(command);
nodeCommand.translucentCommand = deriveTranslucentCommand(
model,
command
);
if (!scene3DOnly) {
var command2D = nodeCommand.command2D;
nodeCommand.translucentCommand2D = deriveTranslucentCommand(
model,
command2D
);
}
Expand Down
21 changes: 13 additions & 8 deletions Source/Scene/PointCloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -832,20 +832,25 @@ function createResources(pointCloud, frameState) {
},
};

var translucentRenderState = {
depthTest: {
enabled: true,
},
depthMask: false,
blending: BlendingState.ALPHA_BLEND,
};

if (pointCloud._opaquePass === Pass.CESIUM_3D_TILE) {
opaqueRenderState.stencilTest = StencilConstants.setCesium3DTileBit();
opaqueRenderState.stencilMask = StencilConstants.CESIUM_3D_TILE_MASK;
translucentRenderState.stencilTest = StencilConstants.setCesium3DTileBit();
translucentRenderState.stencilMask = StencilConstants.CESIUM_3D_TILE_MASK;
}

pointCloud._opaqueRenderState = RenderState.fromCache(opaqueRenderState);

pointCloud._translucentRenderState = RenderState.fromCache({
depthTest: {
enabled: true,
},
depthMask: false,
blending: BlendingState.ALPHA_BLEND,
});
pointCloud._translucentRenderState = RenderState.fromCache(
translucentRenderState
);

pointCloud._drawCommand = new DrawCommand({
boundingVolume: new BoundingSphere(),
Expand Down
25 changes: 3 additions & 22 deletions Source/Scene/TranslucentTileClassification.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Texture from "../Renderer/Texture.js";
import CompareAndPackTranslucentDepth from "../Shaders/CompareAndPackTranslucentDepth.js";
import CompositeTranslucentClassification from "../Shaders/PostProcessStages/CompositeTranslucentClassification.js";
import BlendingState from "./BlendingState.js";
import DerivedCommand from "./DerivedCommand.js";
import StencilConstants from "./StencilConstants.js";
import StencilFunction from "./StencilFunction.js";

Expand Down Expand Up @@ -451,8 +450,9 @@ TranslucentTileClassification.prototype.executeTranslucentCommands = function (
continue;
}

var derivedCommand = getDerivedCommand(command, scene, context);
executeCommand(derivedCommand.depthOnlyCommand, scene, context, passState);
// Depth-only commands are created for all translucent 3D Tiles commands
var depthOnlyCommand = command.derivedCommands.depth.depthOnlyCommand;
executeCommand(depthOnlyCommand, scene, context, passState);
}

this._frustumsDrawn += this._hasTranslucentDepth ? 1 : 0;
Expand Down Expand Up @@ -584,23 +584,4 @@ TranslucentTileClassification.prototype.destroy = function () {
return destroyObject(this);
};

function getDerivedCommand(command, scene, context) {
var derivedCommands = command.derivedCommands;
var depthForClassification = derivedCommands.depthForClassification;
if (!defined(depthForClassification)) {
depthForClassification = derivedCommands.depthForClassification = DerivedCommand.createDepthOnlyDerivedCommand(
scene,
command,
context,
derivedCommands.depthForClassification
);

var depthOnlyCommand = depthForClassification.depthOnlyCommand;
var rs = RenderState.getState(depthOnlyCommand.renderState);
rs.stencilTest = StencilConstants.setCesium3DTileBit();

depthOnlyCommand.renderState = RenderState.fromCache(rs);
}
return depthForClassification;
}
export default TranslucentTileClassification;

0 comments on commit 73d886b

Please sign in to comment.