diff --git a/src/AcDream.Core/World/SkyState.cs b/src/AcDream.Core/World/SkyState.cs index 31fdf3b..5acf2d3 100644 --- a/src/AcDream.Core/World/SkyState.cs +++ b/src/AcDream.Core/World/SkyState.cs @@ -67,19 +67,48 @@ public readonly record struct SkyKeyframe( FogMode FogMode = FogMode.Linear) { /// - /// Final directional sun color used by the shader = - /// × . Computed property - /// so the storage stays as separate channels (for retail-faithful - /// keyframe interpolation) while the shader interface stays simple. + /// Final directional sun color the shader feeds into N·L lighting. + /// Retail-faithful magnitude formula: + /// SunColor = DirColor × |sunVec| + /// where sunVec is retail's heading+pitch+brightness vector + /// (see ). + /// + /// + /// Why |sunVec| instead of DirBright directly: retail's + /// PrimD3DRender::UpdateLightsInternal at 0x0059b57c + /// (decomp line 424118-424119) computes + /// D3DLIGHT9.Diffuse.r = sunlight_color.r × sqrt(x²+y²+z²) + /// from the sun vector SkyDesc::GetLighting built at + /// 0x00500ac9 (decomp lines 261343-261353): + /// + /// sunVec.x = sin(H) × DirBright × cos(P) + /// sunVec.y = cos(P) // NOT scaled by DirBright + /// sunVec.z = DirBright × sin(P) + /// + /// Because Y is unscaled by DirBright, |sunVec| ≠ + /// DirBright in general — it varies with sun pitch and heading. + /// Using DirBright alone underweighted the warm directional + /// term, letting the cool ambient/fog dominate ⇒ acdream rendered + /// blue-white at keyframes where retail looked warm-gray. + /// /// - public Vector3 SunColor => DirColor * DirBright; + public Vector3 SunColor => DirColor * SkyStateProvider.RetailSunVector(this).Length(); /// - /// Final ambient color used by the shader = - /// × . See - /// for the rationale. + /// Final ambient color the shader feeds into the per-vertex tint. + /// Retail-faithful magnitude formula: + /// AmbientColor = AmbColor × (AmbBright + 0.2 × |sunVec|) + /// matching SmartBox::SetWorldAmbientLight as called at + /// 0x0050560b (decomp line 267117): + /// SetWorldAmbientLight(sqrt(|sunVec|²) × 0.2 + ambient_level, ambient_color) + /// Retail boosts the ambient brightness by 20% of the sun-vector + /// magnitude — i.e. ambient feels warmer when the sun is up, cooler + /// at night. acdream previously used AmbBright alone, which + /// is roughly 44% too dim mid-day ⇒ contributed to the blue-white + /// bias because the warm fill was missing. /// - public Vector3 AmbientColor => AmbColor * AmbBright; + public Vector3 AmbientColor => + AmbColor * (AmbBright + 0.2f * SkyStateProvider.RetailSunVector(this).Length()); } /// @@ -271,22 +300,52 @@ public sealed class SkyStateProvider return aDeg + delta * u; } + /// + /// Retail's raw sun vector (NOT normalized) — the same vector + /// SkyDesc::GetLighting writes at 0x00500ac9 + /// (decomp lines 261343, 261352, 261353): + /// + /// sunVec.x = sin(H_rad) × DirBright × cos(P_rad) + /// sunVec.y = cos(P_rad) // NOT scaled by DirBright + /// sunVec.z = DirBright × sin(P_rad) + /// + /// Y is unscaled by brightness on purpose — that's what makes + /// |sunVec|DirBright in general (the magnitude varies + /// with pitch/heading, which is the basis for retail's "sun is brighter + /// in some configurations than others" lighting behavior). The shader's + /// uSunDir uniform uses the NORMALIZED vector for N·L; the + /// magnitude feeds intensity and + /// the ambient brightness boost in . + /// + public static Vector3 RetailSunVector(SkyKeyframe kf) + { + float h = kf.SunHeadingDeg * (MathF.PI / 180f); + float p = kf.SunPitchDeg * (MathF.PI / 180f); + float cosP = MathF.Cos(p); + float sinP = MathF.Sin(p); + float B = kf.DirBright; + return new Vector3( + MathF.Sin(h) * B * cosP, // x = sin(H) × B × cos(P) + cosP, // y = cos(P) ← unscaled by B + B * sinP); // z = B × sin(P) + } + /// /// World-space sun direction unit vector pointing FROM the surface - /// TOWARDS the sun. Derived from heading + pitch in the returned - /// keyframe — shader sunDir uniform should use -this so lighting - /// math (N·L) works correctly for the side facing the sun. + /// TOWARDS the sun, derived from and + /// normalized. The shader sunDir uniform should use this directly + /// (or -this if the lighting math wants the L-vector pointing AT the + /// surface). The previous implementation used standard spherical + /// coordinates (sin(H)cos(P), cos(H)cos(P), sin(P)) which didn't match + /// retail's deliberate Y-decoupled-from-heading convention. Switching + /// to the retail vector subtly tilts the lighting on objects but + /// matches retail's screenshots when both clients view the same scene. /// public static Vector3 SunDirectionFromKeyframe(SkyKeyframe kf) { - float yaw = kf.SunHeadingDeg * (MathF.PI / 180f); - float pit = kf.SunPitchDeg * (MathF.PI / 180f); - // Heading 0 = +Y (north), +X=east. Pitch up from horizon. - float cosP = MathF.Cos(pit); - return new Vector3( - MathF.Sin(yaw) * cosP, - MathF.Cos(yaw) * cosP, - MathF.Sin(pit)); + var v = RetailSunVector(kf); + float len = v.Length(); + return len > 1e-6f ? v / len : Vector3.UnitZ; } } diff --git a/tests/AcDream.Core.Tests/World/SkyDescLoaderTests.cs b/tests/AcDream.Core.Tests/World/SkyDescLoaderTests.cs index bbb619d..3331e85 100644 --- a/tests/AcDream.Core.Tests/World/SkyDescLoaderTests.cs +++ b/tests/AcDream.Core.Tests/World/SkyDescLoaderTests.cs @@ -73,15 +73,26 @@ public sealed class SkyDescLoaderTests } [Fact] - public void LoadFromRegion_SunColor_IsPrepreMultipliedByBrightness() + public void LoadFromRegion_SunColor_UsesRetailSunVectorMagnitude() { + // The loader stores DirColor and DirBright RAW. The SunColor property + // composes them via |sunVec| per retail's UpdateLightsInternal at + // 0x59b57c (decomp 424118) — the diffuse magnitude is sqrt(x²+y²+z²) + // where the sun vector is built from heading/pitch/brightness with + // Y unscaled by brightness (decomp 261352). + // + // For this region: H=180°, P=70°, B=1.5 + // sunVec = (sin(180)*1.5*cos(70), cos(70), 1.5*sin(70)) + // = (0, 0.342, 1.410) + // |sunVec| = sqrt(0 + 0.117 + 1.988) = 1.4509 + // DirColor.X = 200/255 = 0.7843 + // SunColor.X = 0.7843 × 1.4509 = 1.138 var region = MakeRegion(dirBright: 1.5f, rBgrOrder: 200); var loaded = SkyDescLoader.LoadFromRegion(region); Assert.NotNull(loaded); var kf = loaded!.DayGroups[0].SkyTimes[0].Keyframe; - // R was 200/255 ≈ 0.784, times dirBright 1.5 = 1.176 - Assert.InRange(kf.SunColor.X, 1.17f, 1.19f); + Assert.InRange(kf.SunColor.X, 1.13f, 1.15f); } [Fact] diff --git a/tests/AcDream.Core.Tests/World/SkyStateTests.cs b/tests/AcDream.Core.Tests/World/SkyStateTests.cs index 7eac91a..bd3bc73 100644 --- a/tests/AcDream.Core.Tests/World/SkyStateTests.cs +++ b/tests/AcDream.Core.Tests/World/SkyStateTests.cs @@ -25,17 +25,105 @@ public sealed class SkyStateTests } [Fact] - public void Interpolate_BetweenKeyframes_LerpsColors() + public void Interpolate_BetweenKeyframes_LerpsRawInputs() { var sky = SkyStateProvider.Default(); var dawn = sky.Interpolate(0.25f); var noon = sky.Interpolate(0.5f); var midPt = sky.Interpolate(0.375f); - // Midpoint should fall between dawn & noon for sun color Y (green channel). - float low = System.Math.Min(dawn.SunColor.Y, noon.SunColor.Y); - float high = System.Math.Max(dawn.SunColor.Y, noon.SunColor.Y); - Assert.InRange(midPt.SunColor.Y, low, high); + // The RAW per-channel inputs (DirColor, AmbColor, brightness scalars) + // lerp linearly between adjacent keyframes — that's the retail-faithful + // separate-channel interpolation. The composite SunColor / AmbientColor + // properties intentionally do NOT lerp linearly (their magnitude + // depends nonlinearly on heading/pitch/brightness via the retail + // sun-vector formula), so we assert on the raw inputs here. + float low = System.Math.Min(dawn.DirColor.Y, noon.DirColor.Y); + float high = System.Math.Max(dawn.DirColor.Y, noon.DirColor.Y); + Assert.InRange(midPt.DirColor.Y, low, high); + } + + [Fact] + public void RetailSunVector_AtZenith_HasMagnitudeEqualToBrightness() + { + // Sun straight up (P=90°): cos(P)=0, sin(P)=1. + // sunVec = (sin(H)×B×0, 0, B×1) = (0, 0, B) + // |sunVec| = B + var kf = new SkyKeyframe( + Begin: 0.5f, + SunHeadingDeg: 0f, + SunPitchDeg: 90f, + DirColor: Vector3.One, + DirBright: 1.5f, + AmbColor: Vector3.One, + AmbBright: 0.3f, + FogColor: Vector3.One, + FogDensity: 0f); + + var v = SkyStateProvider.RetailSunVector(kf); + Assert.InRange(v.Length(), 1.49f, 1.51f); + } + + [Fact] + public void RetailSunVector_AtHorizonNorth_MagnitudeIsOne() + { + // Sun on horizon to the north (H=0°, P=0°): cos(P)=1, sin(P)=0. + // sunVec = (sin(0)×B×1, 1, B×0) = (0, 1, 0) + // |sunVec| = 1 regardless of B (because Y is unscaled by B) + var kf = new SkyKeyframe( + Begin: 0f, + SunHeadingDeg: 0f, + SunPitchDeg: 0f, + DirColor: Vector3.One, + DirBright: 2.0f, // anything + AmbColor: Vector3.One, + AmbBright: 1f, + FogColor: Vector3.One, + FogDensity: 0f); + + var v = SkyStateProvider.RetailSunVector(kf); + Assert.InRange(v.Length(), 0.99f, 1.01f); + } + + [Fact] + public void SunColor_UsesRetailMagnitudeNotDirBrightDirectly() + { + // At sun pitch 90° (zenith) with H=0, B=2: |sunVec| = 2. + // SunColor = DirColor × |sunVec| = (0.5, 0.5, 0.5) × 2 = (1, 1, 1). + var kf = new SkyKeyframe( + Begin: 0.5f, + SunHeadingDeg: 0f, + SunPitchDeg: 90f, + DirColor: new Vector3(0.5f, 0.5f, 0.5f), + DirBright: 2.0f, + AmbColor: Vector3.One, + AmbBright: 0.3f, + FogColor: Vector3.One, + FogDensity: 0f); + + Assert.InRange(kf.SunColor.X, 0.99f, 1.01f); + Assert.InRange(kf.SunColor.Y, 0.99f, 1.01f); + Assert.InRange(kf.SunColor.Z, 0.99f, 1.01f); + } + + [Fact] + public void AmbientColor_BoostsByTwentyPercentOfSunVectorLength() + { + // |sunVec| = 1 (horizon north), AmbBright = 0.4, AmbColor = (1,1,1). + // AmbientColor = AmbColor × (AmbBright + 0.2 × |sunVec|) + // = (1,1,1) × (0.4 + 0.2) = (0.6, 0.6, 0.6). + var kf = new SkyKeyframe( + Begin: 0f, + SunHeadingDeg: 0f, + SunPitchDeg: 0f, + DirColor: Vector3.One, + DirBright: 1f, + AmbColor: Vector3.One, + AmbBright: 0.4f, + FogColor: Vector3.One, + FogDensity: 0f); + + Assert.InRange(kf.AmbientColor.X, 0.59f, 0.61f); } [Fact]