Skip to content

Commit

Permalink
Wind Waker / Twilight Princess: Fix sun PeekZ
Browse files Browse the repository at this point in the history
We were testing if the sun's position was obscured by something closer. However, the game only places the sun 8000 units away, which is very easy to hit with one of the bigger maps. The original game seems to test if the depth buffer has *anything* in it at all. I can't recall exactly why I went with the projected Z thing, so just do this instead.
  • Loading branch information
magcius committed Oct 19, 2024
1 parent 4ec0e0e commit b83cc74
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 26 deletions.
16 changes: 3 additions & 13 deletions src/ZeldaTwilightPrincess/d_kankyo_wether.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { mDoLib_project, mDoLib_projectFB } from "../ZeldaWindWaker/m_do_ext.js"
import { MtxTrans, calc_mtx, mDoMtx_XrotM, mDoMtx_ZrotM } from "../ZeldaWindWaker/m_do_mtx.js";
import { fullscreenMegaState, setAttachmentStateSimple } from "../gfx/helpers/GfxMegaStateDescriptorHelpers.js";
import { GfxShaderLibrary } from "../gfx/helpers/GfxShaderLibrary.js";
import { compareDepthValues } from "../gfx/helpers/ReversedDepthHelpers.js";
import { compareDepthValues, reverseDepthForClearValue } from "../gfx/helpers/ReversedDepthHelpers.js";
import { fillColor, fillVec4 } from "../gfx/helpers/UniformBufferHelpers.js";
import { GfxBindingLayoutDescriptor, GfxBlendFactor, GfxBlendMode, GfxClipSpaceNearZ, GfxCompareMode, GfxDevice, GfxFormat, GfxMipFilterMode, GfxTexFilterMode, GfxWrapMode } from "../gfx/platform/GfxPlatform.js";
import { GfxProgram } from "../gfx/platform/GfxPlatformImpl.js";
Expand Down Expand Up @@ -1481,18 +1481,8 @@ function dKyr_sun_move__PeekZ(dst: PeekZResult, peekZ: PeekZManager, v: Readonly
if (dst.value === null)
return SunPeekZResult.Obscured;

// Test if the depth buffer is less than our projected Z coordinate.
// Depth buffer readback should result in 0.0 for the near plane, and 1.0 for the far plane.
// Put projected coordinate in 0-1 normalized space.
let projectedZ = v[2];

if (clipSpaceNearZ === GfxClipSpaceNearZ.NegativeOne)
projectedZ = projectedZ * 0.5 + 0.5;

// Point is visible if our projected Z is in front of the depth buffer.
const visible = compareDepthValues(projectedZ, dst.value, GfxCompareMode.Less);

return visible ? SunPeekZResult.Visible : SunPeekZResult.Obscured;
const obscured = compareDepthValues(dst.value, reverseDepthForClearValue(1.0), GfxCompareMode.Less);
return obscured ? SunPeekZResult.Obscured : SunPeekZResult.Visible;
}

function dKyr_sun_move(globals: dGlobals, deltaTimeFrames: number): void {
Expand Down
16 changes: 3 additions & 13 deletions src/ZeldaWindWaker/d_kankyo_wether.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BTIData, BTI_Texture } from "../Common/JSYSTEM/JUTTexture.js";
import { dfRange, dfShow } from "../DebugFloaters.js";
import { MathConstants, clamp, computeMatrixWithoutTranslation, computeModelMatrixR, invlerp, saturate, vec3SetAll } from "../MathHelpers.js";
import { TDDraw } from "../SuperMarioGalaxy/DDraw.js";
import { compareDepthValues } from "../gfx/helpers/ReversedDepthHelpers.js";
import { compareDepthValues, reverseDepthForClearValue } from "../gfx/helpers/ReversedDepthHelpers.js";
import { GfxClipSpaceNearZ, GfxCompareMode, GfxDevice } from "../gfx/platform/GfxPlatform.js";
import { GfxRenderInst, GfxRenderInstManager } from "../gfx/render/GfxRenderInstManager.js";
import { GXMaterialBuilder } from "../gx/GXMaterialBuilder.js";
Expand Down Expand Up @@ -1578,18 +1578,8 @@ function dKyr_sun_move__PeekZ(dst: PeekZResult, peekZ: PeekZManager, v: Readonly
if (dst.value === null)
return SunPeekZResult.Obscured;

// Test if the depth buffer is less than our projected Z coordinate.
// Depth buffer readback should result in 0.0 for the near plane, and 1.0 for the far plane.
// Put projected coordinate in 0-1 normalized space.
let projectedZ = v[2];

if (clipSpaceNearZ === GfxClipSpaceNearZ.NegativeOne)
projectedZ = projectedZ * 0.5 + 0.5;

// Point is visible if our projected Z is in front of the depth buffer.
const visible = compareDepthValues(projectedZ, dst.value, GfxCompareMode.Less);

return visible ? SunPeekZResult.Visible : SunPeekZResult.Obscured;
const obscured = compareDepthValues(dst.value, reverseDepthForClearValue(1.0), GfxCompareMode.Less);
return obscured ? SunPeekZResult.Obscured : SunPeekZResult.Visible;
}

function dKyr_sun_move(globals: dGlobals): void {
Expand Down

0 comments on commit b83cc74

Please sign in to comment.