diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/LivingVillage.Desktop.Tests/P47SceneDetailTests.fs | 28 | ||||
| -rw-r--r-- | src/LivingVillage.Desktop/Game.fs | 127 | ||||
| -rw-r--r-- | src/LivingVillage.Desktop/SceneDetail.fs | 74 | ||||
| -rw-r--r-- | src/LivingVillage.Desktop/SceneDetailRender.fs | 62 |
4 files changed, 190 insertions, 101 deletions
diff --git a/src/LivingVillage.Desktop.Tests/P47SceneDetailTests.fs b/src/LivingVillage.Desktop.Tests/P47SceneDetailTests.fs index 4ff0a66..ac8bf80 100644 --- a/src/LivingVillage.Desktop.Tests/P47SceneDetailTests.fs +++ b/src/LivingVillage.Desktop.Tests/P47SceneDetailTests.fs @@ -20,7 +20,7 @@ type P47SceneDetailTests () = assertInt 4 (snd SceneDetail.shadowOffsetPx) "shadow dy" // alpha strictly decreases with distance and never goes negative. let alphas = [ for d in 1 .. 6 -> SceneDetail.reflectionAlphaAt d ] - Assert.IsTrue(alphas |> List.forall (fun a -> a >= 0), "reflection alpha must be >= 0") + Assert.IsTrue(alphas |> List.forall (fun a -> a >= 96), "reflection alpha must stay legible (>= 96)") Assert.IsTrue(alphas.[0] > alphas.[1] && alphas.[1] > alphas.[2], "alpha must decrease with distance") // wobble is exactly the 3-phase water ramp mapped to -1/0/+1 and is deterministic. for tick in [ 0L; 32L; 64L; 96L ] do @@ -40,14 +40,26 @@ type P47SceneDetailTests () = Assert.IsTrue(not (List.isEmpty sources), "the generator map must have bank light sources") let streaks = SceneDetail.reflectionStreaks map Assert.IsTrue(not (List.isEmpty streaks), "there must be at least one reflection streak") - for (x, y, alpha) in streaks do - Assert.IsTrue(SceneDetail.isWater map x y, sprintf "reflection (%d,%d) must sit on water" x y) - Assert.IsTrue(alpha > 0, "reflection alpha must be positive") - // every streak sits directly below its source within reflectionRange. + for (x, y, alpha, _) in streaks do + Assert.IsTrue(SceneDetail.isWaterSurface map x y, sprintf "reflection (%d,%d) must sit on a reflective surface" x y) + Assert.IsTrue(alpha >= 96, "reflection alpha must stay legible (>= 96)") + // every streak sits in the same column as a source, within stream range + bridge/own-structure gap. let sourceSet = sources |> Set.ofList - for (x, y, _) in streaks do - Assert.IsTrue([ 1 .. SceneDetail.reflectionRange ] |> List.exists (fun d -> Set.contains (x, y - d) sourceSet), - sprintf "reflection (%d,%d) must be under a source" x y) + for (x, y, _, _) in streaks do + Assert.IsTrue( + [ 1 .. SceneDetail.reflectionRange + SceneDetail.reflectionGap ] + |> List.exists (fun d -> Set.contains (x, y - d) sourceSet), + sprintf "reflection (%d,%d) must be in the column below a source" x y) + // each light source keeps its kind so the renderer can tint per kind. + for (_, _, kind) in SceneDetail.lightSources map do + Assert.IsTrue(kind = SceneDetail.BridgeLantern || kind = SceneDetail.RoadLantern || kind = SceneDetail.RedLantern, + "light source kind must be one of the three known kinds") + // at least one source must produce a full >= 3 tile vertical reflection (leader acceptance). + let longest = + SceneDetail.lightSources map + |> List.map (fun (x, y, _) -> SceneDetail.reflectionLength map x y) + |> List.fold max 0 + Assert.IsTrue(longest >= 3, sprintf "the longest reflection must span >= 3 tiles, got %d" longest) [<TestMethod>] member _.BankReedsAndFloatersAreDenseAndDeterministic () = diff --git a/src/LivingVillage.Desktop/Game.fs b/src/LivingVillage.Desktop/Game.fs index a187cc4..05afdc2 100644 --- a/src/LivingVillage.Desktop/Game.fs +++ b/src/LivingVillage.Desktop/Game.fs @@ -1004,60 +1004,98 @@ type LivingVillageGame() as this = | None -> this.Exit() | Some map -> let riverside = - // 取第一处「光源正下方有水面」的倒影点,相机对准其光源格,倒影落在正下方。 - match SceneDetail.reflectionStreaks map with - | (rx, ry, _) :: _ -> (rx, ry - 1) - | [] -> - match VillageArt.cc0LanternTiles map 12 with - | (lx, ly) :: _ -> (lx, ly) - | [] -> - match map.Bridges |> List.sortBy (fun (x, y) -> (x, y)) |> List.tryHead with - | Some (bx, by) -> (bx, by) - | None -> (map.Core.MinX, map.Core.MinY) + // 取倒影长度最长、优先桥头/红灯笼的光源,相机对准其光源格,倒影落在正下方。 + let ranked = + SceneDetail.lightSources map + |> List.map (fun (x, y, kind) -> + let length = SceneDetail.reflectionLength map x y + let kindRank = + match kind with + | SceneDetail.RedLantern -> 2 + | SceneDetail.BridgeLantern -> 1 + | SceneDetail.RoadLantern -> 0 + x, y, kind, length, kindRank) + let deep = ranked |> List.filter (fun (_, _, _, length, _) -> length >= 3) + let pick = + (if List.isEmpty deep then ranked else deep) + |> List.sortByDescending (fun (_, _, _, length, kindRank) -> kindRank, length) + |> List.tryHead + match pick with + | Some (x, y, _, _, _) -> (x, y) + | None -> + match SceneDetail.reflectionStreaks map with + | (rx, ry, _, _) :: _ -> (rx, ry - 1) + | [] -> (map.Core.MinX, map.Core.MinY) let closeup = - match SceneDetail.floatingProps map with - | (fx, fy, _) :: _ -> (fx, fy) + // 近景:优先倒影更长、且离漂浮元素更近的光源,保证近景同时有倒影与漂浮元素。 + let waterSources = + SceneDetail.lightSources map + |> List.filter (fun (x, y, _) -> SceneDetail.reflectionLength map x y >= 1) + let floatTiles = SceneDetail.floatingProps map |> List.map (fun (x, y, _) -> x, y) + let distToFloat (sx: int) (sy: int) = + if List.isEmpty floatTiles then 0 + else floatTiles |> List.map (fun (fx, fy) -> abs (fx - sx) + abs (fy - sy)) |> List.min + match waterSources with | [] -> riverside - let reportVisibleReflections () = + | _ -> + waterSources + |> List.map (fun (x, y, _) -> x, y, SceneDetail.reflectionLength map x y, distToFloat x y) + |> List.sortBy (fun (_, _, length, distance) -> -length, distance) + |> List.head + |> fun (x, y, _, _) -> (x, y) + let reportVisibleReflections (shotName: string) = let vw = this.GraphicsDevice.Viewport.Width let vh = this.GraphicsDevice.Viewport.Height - for (lx, ly) in SceneDetail.lightSourceTiles map do - for d in 1 .. SceneDetail.reflectionRange do - let ry = ly + d - if SceneDetail.isWater map lx ry then - let ssx = lx * Sim.tilePixels - int camera.X - let ssy = ly * Sim.tilePixels - int camera.Y - let rsx = lx * Sim.tilePixels - int camera.X - let rsy = ry * Sim.tilePixels - int camera.Y - if ssx >= 0 && ssx + Sim.tilePixels <= vw && ssy >= 0 && ssy + Sim.tilePixels <= vh - && rsy + Sim.tilePixels <= vh then + for (lx, ly, kind) in SceneDetail.lightSources map do + for (rx, ry, alpha, _) in SceneDetail.reflectionStreakFor map lx ly kind do + let ssx = lx * Sim.tilePixels - int camera.X + let ssy = ly * Sim.tilePixels - int camera.Y + let rsx = rx * Sim.tilePixels - int camera.X + let rsy = ry * Sim.tilePixels - int camera.Y + // 空白对照:同排最近的非倒影水面格。 + let control = + [ (2, 0); (-2, 0); (4, 0); (-4, 0); (0, 1); (0, -1) ] + |> List.map (fun (ox, oy) -> rx + ox, ry + oy) + |> List.tryFind (fun (cx, cy) -> SceneDetail.isWaterSurface map cx cy) + if ssx >= 0 && ssx + Sim.tilePixels <= vw && ssy >= 0 && ssy + Sim.tilePixels <= vh + && rsy + Sim.tilePixels <= vh then + match control with + | Some (cx, cy) -> printfn - "p47-reflection src=%d,%d dst=%d,%d alpha=%d" - ssx ssy rsx rsy (SceneDetail.reflectionAlphaAt d) + "p47-reflection %s src=%d,%d dst=%d,%d ctrl=%d,%d alpha=%d" + shotName ssx ssy rsx rsy (cx * Sim.tilePixels - int camera.X) (cy * Sim.tilePixels - int camera.Y) alpha + | None -> + printfn "p47-reflection %s src=%d,%d dst=%d,%d ctrl=-1,-1 alpha=%d" shotName ssx ssy rsx rsy alpha + for (rx, ry, rw) in SceneDetail.foregroundRooftops vw vh world.Tick do + if rx + rw > 0 && rx < vw then + printfn "p47-roof %s x=%d y=%d w=%d" shotName rx ry rw printfn - "p47-elements reeds=%d floats=%d reflections=%d sources=%d" + "p47-elements reeds=%d floats=%d reflections=%d sources=%d maxstreak=%d" (List.length (SceneDetail.bankReedTiles map)) (List.length (SceneDetail.floatingProps map)) (List.length (SceneDetail.reflectionStreaks map)) - (List.length (SceneDetail.lightSourceTiles map)) + (List.length (SceneDetail.lightSources map)) + (SceneDetail.lightSources map + |> List.map (fun (x, y, _) -> SceneDetail.reflectionLength map x y) + |> List.fold max 0) match p47Step with | 0 -> setHour 12.0 placeCameraOn (fst riverside) (snd riverside) - reportVisibleReflections () p47PendingName <- "p47-day" + reportVisibleReflections "p47-day" p47Pending <- true | 1 -> setHour 23.5 placeCameraOn (fst riverside) (snd riverside) - reportVisibleReflections () p47PendingName <- "p47-night" + reportVisibleReflections "p47-night" p47Pending <- true | 2 -> setHour 12.0 placeCameraOn (fst closeup) (snd closeup) - reportVisibleReflections () p47PendingName <- "p47-close" + reportVisibleReflections "p47-close" p47Pending <- true | _ -> printfn "p47-shot=done" @@ -2144,20 +2182,6 @@ type LivingVillageGame() as this = world.Tick profile.WorldTint - // P47 场景细节:河道倒影 + 岸边芦苇加密度 + 漂浮元素 + 近景楼层层错落(统一光源阴影)。 - match riverscapeMap with - | Some map -> - SceneDetailRender.draw - spriteBatch - artTextures - pixel - camera - viewport.Width - viewport.Height - map - world.Tick - | None -> () - // Warm lantern / window glows strengthen as night falls (pure function of tick phase). // Concentric premultiplied circles follow a quadratic radial falloff (radius 3-4 tiles), // so the core stays a soft warm disc instead of saturating to a white block and no glow @@ -2196,6 +2220,21 @@ type LivingVillageGame() as this = VillageArt.drawCc0BoatOverlay spriteBatch artTextures camera map profile.WorldTint | _ -> () + // P47 场景细节:河道倒影 + 岸边芦苇加密度 + 漂浮元素 + 近景楼层层错落。 + // 画在夜光/船之后,保证倒影不被灯笼呼吸光晕盖掉、仍可辨认。 + match riverscapeMap with + | Some map -> + SceneDetailRender.draw + spriteBatch + artTextures + pixel + camera + viewport.Width + viewport.Height + map + world.Tick + | None -> () + VillageArt.drawGroundShadow spriteBatch pixel camera world.Avatar.Pos let avatarSpec = VillageArt.avatarSpriteSpec avatarFacing avatarMoving world.Tick VillageArt.drawCharacter spriteBatch artTextures camera world.Avatar.Pos avatarSpec profile.WorldTint diff --git a/src/LivingVillage.Desktop/SceneDetail.fs b/src/LivingVillage.Desktop/SceneDetail.fs index c51c47a..50313ac 100644 --- a/src/LivingVillage.Desktop/SceneDetail.fs +++ b/src/LivingVillage.Desktop/SceneDetail.fs @@ -17,14 +17,24 @@ module SceneDetail = x >= 0 && x < map.Width && y >= 0 && y < map.Height && map.Tiles.[y * map.Width + x] = int MapGen.GroundTile.Water + /// 可倒影水面:河面(Water)与水田(PaddyField,同样走水面动画渲染)。 + let isWaterSurface (map: MapGen.Result) (x: int) (y: int) : bool = + x >= 0 && x < map.Width && y >= 0 && y < map.Height + && (map.Tiles.[y * map.Width + x] = int MapGen.GroundTile.Water + || map.Tiles.[y * map.Width + x] = int MapGen.GroundTile.PaddyField) + // ---------------- 河道水面倒影 ---------------- - let reflectionMaxAlpha = 128 - let reflectionRange = 3 + let reflectionMaxAlpha = 200 + let reflectionRange = 4 + /// 倒影射线允许穿过的非水面格数(如灯笼所立的桥面/岸沿),使「桥下红灯笼」的倒影 + /// 仍落在其正下方的河面,而不是被桥面格截断。 + let reflectionGap = 3 - /// 倒影 alpha 随离光源距离递减;到 0 即止,绝不留下硬块。 + /// 倒影 alpha 随离光源距离递减;最低仍保持 96,保证整条暖色光柱在蓝灰水面上 + /// 始终可辨(不会淡到被水面波纹吞掉)。 let reflectionAlphaAt (distance: int) : int = - max 0 (reflectionMaxAlpha - distance * 34) + max 96 (reflectionMaxAlpha - (distance - 1) * 26) /// 倒影微抖:复用 waterFrameTickAt 的 3 相位,映射到 -1 / 0 / +1 像素水平位移。 let reflectionWobbleAt (tick: int64) (x: int) (y: int) : int = @@ -33,20 +43,50 @@ module SceneDetail = | 1 -> 0 | _ -> 1 - /// 岸上光源:桥头灯笼 + 主路石灯笼 + 门口红灯笼,去重后即倒影源。 + /// 光源类型:桥头灯笼 / 主路石灯笼 / 门口红灯笼。倒影按类型着色。 + type LightKind = + | BridgeLantern + | RoadLantern + | RedLantern + + /// 岸上光源(含类型),按位置去重。 + let lightSources (map: MapGen.Result) : (int * int * LightKind) list = + let bridge = VillageArt.bridgeLanternTiles map |> List.map (fun (x, y) -> x, y, BridgeLantern) + let road = VillageArt.cc0LanternTiles map 12 |> List.map (fun (x, y) -> x, y, RoadLantern) + let red = VillageArt.cc0RedLanternTiles map |> List.map (fun (x, y) -> x, y, RedLantern) + (bridge @ road @ red) + |> List.distinctBy (fun (x, y, _) -> x, y) + let lightSourceTiles (map: MapGen.Result) : (int * int) list = - (VillageArt.bridgeLanternTiles map - @ VillageArt.cc0LanternTiles map 12 - @ VillageArt.cc0RedLanternTiles map) - |> List.distinct - - /// 每个光源在其正下方连续水面格生成一条垂直倒影 (x, y, alpha)。 - let reflectionStreaks (map: MapGen.Result) : (int * int * int) list = - lightSourceTiles map - |> List.collect (fun (sx, sy) -> - [ for d in 1 .. reflectionRange do - let y = sy + d - if isWater map sx y then yield (sx, y, reflectionAlphaAt d) ]) + lightSources map |> List.map (fun (x, y, _) -> x, y) + + /// 某个光源向下的倒影:先穿过至多 reflectionGap 格非水面(桥面/岸沿),落到正下方 + /// 水面上,再沿连续水面画最多 reflectionRange 格;返回 (x, y, alpha, kind)。 + let reflectionStreakFor (map: MapGen.Result) (sx: int) (sy: int) (kind: LightKind) : (int * int * int * LightKind) list = + let mutable start = sy + 1 + let mutable gap = 0 + while gap <= reflectionGap && start < map.Height && not (isWaterSurface map sx start) do + start <- start + 1 + gap <- gap + 1 + if start >= map.Height || gap > reflectionGap || not (isWaterSurface map sx start) then + [] + else + [ for d in 0 .. reflectionRange - 1 do + let y = start + d + if isWaterSurface map sx y then yield (sx, y, reflectionAlphaAt (d + 1), kind) ] + + /// 光源正下方倒影的格数(用于挑选倒影长度足够的取证机位)。 + let reflectionLength (map: MapGen.Result) (x: int) (sy: int) : int = + lightSources map + |> List.filter (fun (sx, syy, _) -> sx = x && syy = sy) + |> List.tryHead + |> Option.map (fun (sx, syy, kind) -> reflectionStreakFor map sx syy kind |> List.length) + |> Option.defaultValue 0 + + /// 每个光源在其正下方连续水面格生成一条垂直倒影 (x, y, alpha, kind),最长 4 格。 + let reflectionStreaks (map: MapGen.Result) : (int * int * int * LightKind) list = + lightSources map + |> List.collect (fun (sx, sy, kind) -> reflectionStreakFor map sx sy kind) // ---------------- 岸边植被加密度 ---------------- diff --git a/src/LivingVillage.Desktop/SceneDetailRender.fs b/src/LivingVillage.Desktop/SceneDetailRender.fs index 2136d25..abe0b89 100644 --- a/src/LivingVillage.Desktop/SceneDetailRender.fs +++ b/src/LivingVillage.Desktop/SceneDetailRender.fs @@ -6,40 +6,31 @@ open Microsoft.Xna.Framework.Graphics open LivingVillage.Kernel /// P47 场景细节渲染层:把 `SceneDetail` 的纯函数结果落成画面。 -/// 反射用「光源精灵垂直镜像 + 相位微抖」,岸边芦苇/漂浮元素用既有瓦片精灵与原创点阵图集, -/// 近景屋顶用既有屋瓦精灵 + 统一方向阴影。全部只读,不改瓦片/可达性。 +/// 倒影为每个光源正下方一条暖色垂直光柱(核心 + 外晕,离光源越远越淡), +/// 岸芦苇/漂浮元素用既有瓦片精灵与原创点阵图集,近景屋顶按统一光源分受光面/背光面。 +/// 全部只读,不改瓦片/可达性。 module SceneDetailRender = let private tileX (camera: Vector2) (x: int) = x * Sim.tilePixels - int camera.X let private tileY (camera: Vector2) (y: int) = y * Sim.tilePixels - int camera.Y - let private drawFlipVertical + /// 河道倒影:每个光源在正下方水面格画一段暖色垂直光柱(外晕 20px + 核心 10px), + /// alpha 随离光源距离递减、逐格 ±1px 微抖,连成一条可辨认的暖色垂直条纹 + /// (明显区别于蓝灰横向波纹)。 + let private drawReflections (spriteBatch: SpriteBatch) - (atlas: Texture2D) - (source: Rectangle) - (x: int) - (y: int) - (color: Color) = - spriteBatch.Draw( - atlas, - Rectangle(x, y, Sim.tilePixels, Sim.tilePixels), - Nullable(source), - color, - 0.0f, - Vector2.Zero, - SpriteEffects.FlipVertically, - 0.0f) - - /// 河道倒影:岸上每个光源在其正下方水面格画一份垂直镜像,随 waterFrameTickAt 相位水平微抖。 - let private drawReflections (spriteBatch: SpriteBatch) (textures: VillageArt.ArtTextures) (camera: Vector2) (map: MapGen.Result) (tick: int64) = - let source, atlas = - match textures.Cc0TileAtlas with - | Some cc0 -> VillageArt.cc0TileRect VillageArt.cc0TileIndexStoneLantern, cc0 - | None -> VillageArt.atlasTileRectangle 20, textures.TileAtlas - for (x, y, alpha) in SceneDetail.reflectionStreaks map do + (pixel: Texture2D) + (camera: Vector2) + (map: MapGen.Result) + (tick: int64) = + for (x, y, alpha, _) in SceneDetail.reflectionStreaks map do let wobble = SceneDetail.reflectionWobbleAt tick x y - let color = VillageArt.premultiply 255 214 168 alpha - drawFlipVertical spriteBatch atlas source (tileX camera x + wobble) (tileY camera y) color + let cx = tileX camera x + let cy = tileY camera y + let halo = VillageArt.premultiply 255 150 70 (alpha * 3 / 5) + let core = VillageArt.premultiply 255 208 120 alpha + spriteBatch.Draw(pixel, Rectangle(cx + 4 + wobble, cy, 24, Sim.tilePixels), halo) + spriteBatch.Draw(pixel, Rectangle(cx + 10 + wobble, cy, 12, Sim.tilePixels), core) /// 岸边芦苇加密度:用既有芦苇精灵补绘一层贴水芦苇。 let private drawBankReeds (spriteBatch: SpriteBatch) (textures: VillageArt.ArtTextures) (camera: Vector2) (map: MapGen.Result) = @@ -69,17 +60,24 @@ module SceneDetailRender = Nullable(source), Color.White) - /// 近景楼层层错落:屏幕空间前景屋顶带,所有阴影统一投向右下(SceneDetail.shadowOffsetPx)。 - let private drawForeground (spriteBatch: SpriteBatch) (textures: VillageArt.ArtTextures) (viewportWidth: int) (viewportHeight: int) (tick: int64) = + /// 近景楼层层错落:屏幕空间前景屋顶带。统一光源来自左上,故每段屋顶 + /// 左半受光(暖亮)、右半背光(压暗),并整体投影到右下(shadowOffsetPx)。 + let private drawForeground (spriteBatch: SpriteBatch) (textures: VillageArt.ArtTextures) (pixel: Texture2D) (viewportWidth: int) (viewportHeight: int) (tick: int64) = let ridgeSource = VillageArt.atlasTileRectangle 8 let eaveSource = VillageArt.atlasTileRectangle 9 let shadowColor = VillageArt.premultiply 8 12 18 120 + let litColor = VillageArt.premultiply 255 236 196 46 + let darkColor = VillageArt.premultiply 6 10 18 110 let dx, dy = SceneDetail.shadowOffsetPx for (x, y, w) in SceneDetail.foregroundRooftops viewportWidth viewportHeight tick do - // 统一方向阴影:先把 eave 的暗色镜像平移一份到右下,再画本体。 + // 统一方向的投影:先把 eave 的暗色镜像平移一份到右下,再画本体。 spriteBatch.Draw(textures.TileAtlas, Rectangle(x + dx, y + dy, w, Sim.tilePixels), Nullable(eaveSource), shadowColor) spriteBatch.Draw(textures.TileAtlas, Rectangle(x, y - Sim.tilePixels, w, Sim.tilePixels), Nullable(ridgeSource), Color.White) spriteBatch.Draw(textures.TileAtlas, Rectangle(x, y, w, Sim.tilePixels), Nullable(eaveSource), Color.White) + // 受光面 / 背光面:左半加暖亮、右半压暗,形成两档以上 luma 差。 + let half = max 1 (w / 2) + spriteBatch.Draw(pixel, Rectangle(x, y - Sim.tilePixels, half, 2 * Sim.tilePixels), litColor) + spriteBatch.Draw(pixel, Rectangle(x + half, y - Sim.tilePixels, w - half, 2 * Sim.tilePixels), darkColor) /// 一次性绘制全部 P47 场景细节(倒影 → 岸边芦苇 → 漂浮元素 → 近景前景层)。 let draw @@ -91,7 +89,7 @@ module SceneDetailRender = (viewportHeight: int) (map: MapGen.Result) (tick: int64) = - drawReflections spriteBatch textures camera map tick + drawReflections spriteBatch pixel camera map tick drawBankReeds spriteBatch textures camera map drawFloaters spriteBatch textures camera map tick - drawForeground spriteBatch textures viewportWidth viewportHeight tick + drawForeground spriteBatch textures pixel viewportWidth viewportHeight tick |
