z fighting save

This commit is contained in:
Daniel Zhong 2025-08-22 20:31:53 -04:00
parent af97e57fc2
commit 78c4d02c70
4 changed files with 28 additions and 55 deletions

View File

@ -1,6 +1,6 @@
import defined from "../../Core/defined.js";
import ShaderDestination from "../../Renderer/ShaderDestination.js";
import Pass from "../../Renderer/Pass.js";
import EdgeDetectionStageFS from "../../Shaders/Model/EdgeDetectionStageFS.js";
/**
* A pipeline stage for edge detection and discard logic for planar surfaces.
@ -32,29 +32,9 @@ EdgeDetectionPipelineStage.process = function (
primitive,
frameState,
) {
// Skip edge detection for edge passes themselves
if (renderResources.alphaOptions.pass === Pass.CESIUM_3D_TILE_EDGES) {
return;
}
// Only apply edge detection if we have an edge framebuffer available
if (
!defined(frameState.scene) ||
!defined(frameState.scene._view) ||
!defined(frameState.scene._view.edgeFramebuffer)
) {
return;
}
const shaderBuilder = renderResources.shaderBuilder;
const uniformMap = renderResources.uniformMap;
shaderBuilder.addDefine(
"HAS_EDGE_DETECTION",
undefined,
ShaderDestination.FRAGMENT,
);
// Add uniform for edge detection (czm_globeDepthTexture is already an automatic uniform)
shaderBuilder.addUniform(
"sampler2D",
@ -70,7 +50,7 @@ EdgeDetectionPipelineStage.process = function (
) {
return frameState.scene._view.edgeFramebuffer.idTexture;
}
return undefined;
return frameState.context.defaultTexture;
};
shaderBuilder.addUniform(
@ -83,36 +63,7 @@ EdgeDetectionPipelineStage.process = function (
};
// Add the edge detection function to fragment shader
shaderBuilder.addFragmentLines([
"#ifdef HAS_EDGE_DETECTION",
"void performEdgeDetection() {",
" // Get screen coordinate for texture lookup",
" vec2 screenCoord = gl_FragCoord.xy / czm_viewport.zw;",
" ",
" // Read edge ID from the edge buffer",
" vec4 edgeId = texture(czm_edgeIdTexture, screenCoord);",
" ",
" // Read depth from the globe depth texture (includes 3D Tiles)",
" float edgeDepth = texture(czm_globeDepthTexture, screenCoord).r;",
" ",
" // Convert fragment depth to same coordinate system",
" float fragmentDepth = gl_FragCoord.z;",
" ",
" // If there's an edge at this pixel and the depths are close, discard",
" // This prevents z-fighting between edges and underlying surfaces",
" if (edgeId.a > 0.0 && abs(fragmentDepth - edgeDepth) < czm_edgeDepthTolerance) {",
" discard;",
" }",
"}",
"#endif",
]);
// Add the call to edge detection in the fragment shader main function
shaderBuilder.addFunctionLines("fragmentMain", [
"#ifdef HAS_EDGE_DETECTION",
" performEdgeDetection();",
"#endif",
]);
shaderBuilder.addFragmentLines([EdgeDetectionStageFS]);
};
export default EdgeDetectionPipelineStage;

View File

@ -330,9 +330,6 @@ ModelRuntimePrimitive.prototype.configurePipeline = function (frameState) {
if (hasEdgeVisibility) {
pipelineStages.push(EdgeVisibilityPipelineStage);
}
if (hasEdgeVisibility) {
pipelineStages.push(EdgeDetectionPipelineStage);
}

View File

@ -0,0 +1,24 @@
void edgeDetectionStage() {
// Skip edge detection for edge passes themselves
if (u_isEdgePass) {
return;
}
// Get screen coordinate for texture lookup
vec2 screenCoord = gl_FragCoord.xy / czm_viewport.zw;
// Read edge ID from the edge buffer
vec4 edgeId = texture(czm_edgeIdTexture, screenCoord);
// Read depth from the globe depth texture (includes 3D Tiles)
float edgeDepth = texture(czm_globeDepthTexture, screenCoord).r;
// Convert fragment depth to same coordinate system
float fragmentDepth = gl_FragCoord.z;
// If there's an edge at this pixel and the depths are close, discard
// This prevents z-fighting between edges and underlying surfaces
if (edgeId.a > 0.0 && abs(fragmentDepth - edgeDepth) < czm_edgeDepthTolerance) {
discard;
}
}

View File

@ -121,6 +121,7 @@ void main()
#ifdef HAS_EDGE_VISIBILITY
edgeVisibilityStage(color);
edgeDetectionStage();
#endif
#endif