LCOV - code coverage report
Current view: top level - render3d - projector.go Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 81 81
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package render3d
       2              : 
       3              : import (
       4              :         "math"
       5              : 
       6              :         gc "github.com/kijimaD/ruins/internal/components"
       7              :         "github.com/kijimaD/ruins/internal/consts"
       8              : )
       9              : 
      10              : // タイル世界の高さ。タイル1個分を 1.0 として測る
      11              : const (
      12              :         // WallHeight は壁の高さ。壁は箱として描かれるので、壁タイルを指す印は天面へ置く
      13              :         WallHeight = 1.0
      14              :         // BillboardHeight はエンティティの立て板の高さ。頭の上に出す表示の基準になる
      15              :         BillboardHeight = 1.0
      16              :         // cameraTargetHeight はカメラが見つめる高さ。床と壁の中間を見ることで立て板が画面中央に収まる
      17              :         cameraTargetHeight = 0.4
      18              : )
      19              : 
      20              : // カメラの投影パラメータ。視野角と可視距離の範囲を決める
      21              : const (
      22              :         fovDeg = 52.0
      23              :         near   = 0.1
      24              :         far    = 200.0
      25              : )
      26              : 
      27              : // Projector はタイル座標をスクリーン座標へ写す。
      28              : // カメラと画面寸法から組んだ view-projection 行列を保持し、1回の描画のあいだ使い回す。
      29              : type Projector struct {
      30              :         vp     mat
      31              :         view   mat
      32              :         eye    Vec
      33              :         target Vec
      34              :         sw, sh int
      35              : }
      36              : 
      37              : // NewProjector は視点からの投影を組む。
      38              : //
      39              : // orbit はカメラの向きと距離、center は注視するタイルで、通常はプレイヤーの立つタイルを渡す。
      40              : func NewProjector(orbit gc.Camera, center consts.Coord[consts.Tile], sw, sh int) Projector {
      41            1 :         target := Vec{float64(center.X) + 0.5, cameraTargetHeight, float64(center.Y) + 0.5}
      42            1 :         yaw, pitch := orbit.Yaw(), orbit.Pitch
      43            1 :         // カメラはプレイヤーの南側から北を見下ろす。画面の上を北に合わせ、北上のミニマップと向きをそろえる
      44            1 :         dir := Vec{math.Cos(pitch) * math.Sin(yaw), math.Sin(pitch), math.Cos(pitch) * math.Cos(yaw)}
      45            1 :         eye := Add(target, Scale(dir, orbit.Dist))
      46            1 :         view := lookAt(eye, target, Vec{0, 1, 0})
      47            1 : 
      48            1 :         return Projector{
      49            1 :                 vp:     mul(perspective(fovDeg, float64(sw)/float64(sh), near, far), view),
      50            1 :                 view:   view,
      51            1 :                 eye:    eye,
      52            1 :                 target: target,
      53            1 :                 sw:     sw,
      54            1 :                 sh:     sh,
      55            1 :         }
      56              : }
      57              : 
      58              : // Right はカメラから見た画面右方向の単位ベクトル。立て板をカメラへ正対させるのに使う
      59              : func (p Projector) Right() Vec {
      60            1 :         return norm(cross(norm(sub(p.target, p.eye)), Vec{0, 1, 0}))
      61            1 : }
      62              : 
      63              : // Depth は点のカメラ空間での奥行きを返す。奥ほど小さい。画家アルゴリズムの並べ替えに使う
      64              : func (p Projector) Depth(v Vec) float64 {
      65            1 :         _, _, z, _ := apply(p.view, v)
      66            1 :         return z
      67            1 : }
      68              : 
      69              : // Point は3D空間の点をスクリーン座標へ写す。カメラ後方なら ok=false
      70              : func (p Projector) Point(v Vec) (consts.Coord[consts.ScreenPixel], bool) {
      71            1 :         cx, cy, _, cw := apply(p.vp, v)
      72            1 :         if cw <= 0.001 {
      73            1 :                 return consts.Coord[consts.ScreenPixel]{}, false
      74            1 :         }
      75            1 :         return consts.Coord[consts.ScreenPixel]{
      76            1 :                 X: consts.ScreenPixel((cx/cw*0.5 + 0.5) * float64(p.sw)),
      77            1 :                 Y: consts.ScreenPixel((1 - (cy/cw*0.5 + 0.5)) * float64(p.sh)),
      78            1 :         }, true
      79              : }
      80              : 
      81              : // TileCenter はタイル中心をスクリーン座標へ写す。
      82              : // height はタイル上面の高さで、床なら 0、壁なら WallHeight を渡す。カメラ後方なら ok=false。
      83              : func (p Projector) TileCenter(c consts.Coord[consts.Tile], height float64) (consts.Coord[consts.ScreenPixel], bool) {
      84            1 :         return p.Point(Vec{float64(c.X) + 0.5, height, float64(c.Y) + 0.5})
      85            1 : }
      86              : 
      87              : // TileCorners はタイルの四隅を高さ height でスクリーン座標へ写す。並びは北西・北東・南東・南西。
      88              : // 透視投影ではタイルが台形になるので、タイルを囲う枠はこの4点を結んで描く。
      89              : // 4隅すべて投影できたときだけ ok=true を返す。
      90              : func (p Projector) TileCorners(c consts.Coord[consts.Tile], height float64) ([4]consts.Coord[consts.ScreenPixel], bool) {
      91            1 :         fx, fz := float64(c.X), float64(c.Y)
      92            1 :         src := [4]Vec{
      93            1 :                 {fx, height, fz},
      94            1 :                 {fx + 1, height, fz},
      95            1 :                 {fx + 1, height, fz + 1},
      96            1 :                 {fx, height, fz + 1},
      97            1 :         }
      98            1 :         var out [4]consts.Coord[consts.ScreenPixel]
      99            1 :         for i, v := range src {
     100            1 :                 sp, ok := p.Point(v)
     101            1 :                 if !ok {
     102            1 :                         return out, false
     103            1 :                 }
     104            1 :                 out[i] = sp
     105              :         }
     106            1 :         return out, true
     107              : }
     108              : 
     109              : // TileOnScreen はタイル c が画面に掛かるかを返す視錐台カリングの判定。基底 y=0 と天面 y=topHeight の
     110              : // 8隅を投影し、そのスクリーン bbox が画面矩形と重なるかを見る。壁やビルボードは上へ伸びるので、
     111              : // 天面の高さ topHeight を渡して縦の広がりも含める。
     112              : //
     113              : // 近平面をまたぐタイルは一部の隅がカメラ後方で投影が破綻するため、bbox を信じず保守的に描く。
     114              : // 全隅がカメラ後方のときだけ確実に落とす。bbox はタイルの実クアッドを必ず包むので、映るタイルを
     115              : // 誤って落とすことはなく、映らないタイルを稀に残すだけ。ゆえに描画結果は変わらない。
     116              : func (p Projector) TileOnScreen(c consts.Coord[consts.Tile], topHeight float64) bool {
     117            1 :         fx, fz := float64(c.X), float64(c.Y)
     118            1 :         corners := [8]Vec{
     119            1 :                 {fx, 0, fz}, {fx + 1, 0, fz}, {fx + 1, 0, fz + 1}, {fx, 0, fz + 1},
     120            1 :                 {fx, topHeight, fz}, {fx + 1, topHeight, fz}, {fx + 1, topHeight, fz + 1}, {fx, topHeight, fz + 1},
     121            1 :         }
     122            1 :         behind := 0
     123            1 :         front := false
     124            1 :         var minX, minY, maxX, maxY float64
     125            1 :         for _, v := range corners {
     126            1 :                 sp, ok := p.Point(v)
     127            1 :                 if !ok {
     128            1 :                         behind++
     129            1 :                         continue
     130              :                 }
     131            1 :                 x, y := float64(sp.X), float64(sp.Y)
     132            1 :                 if !front {
     133            1 :                         minX, maxX, minY, maxY = x, x, y, y
     134            1 :                         front = true
     135            1 :                         continue
     136              :                 }
     137            1 :                 minX, maxX = min(minX, x), max(maxX, x)
     138            1 :                 minY, maxY = min(minY, y), max(maxY, y)
     139              :         }
     140            1 :         if !front {
     141            1 :                 return false // 全隅がカメラ後方。確実に映らない
     142            1 :         }
     143            1 :         if behind > 0 {
     144            1 :                 return true // 近平面をまたぐ。bbox が壊れるので保守的に描く
     145            1 :         }
     146            1 :         sw, sh := float64(p.sw), float64(p.sh)
     147            1 :         return maxX >= 0 && minX <= sw && maxY >= 0 && minY <= sh
     148              : }
     149              : 
     150              : // BillboardTop はタイルに立つ立て板の頭をスクリーン座標へ写す。
     151              : // エンティティの上へ出すダメージテキストやHP表示の基準点にする。
     152              : func (p Projector) BillboardTop(c consts.Coord[consts.Tile]) (consts.Coord[consts.ScreenPixel], bool) {
     153            1 :         return p.Point(Vec{float64(c.X) + 0.5, BillboardHeight, float64(c.Y) + 0.5})
     154            1 : }
     155              : 
     156              : // BillboardScale は立て板1枚分の高さが画面上で何ピクセルになるかを返す。
     157              : // 立て板と同じ大きさで重ねたいスプライトの拡大率を決めるのに使う。投影できなければ ok=false。
     158              : func (p Projector) BillboardScale(c consts.Coord[consts.Tile]) (float64, bool) {
     159            1 :         top, okTop := p.BillboardTop(c)
     160            1 :         base, okBase := p.TileCenter(c, 0)
     161            1 :         if !okTop || !okBase {
     162            1 :                 return 0, false
     163            1 :         }
     164            1 :         return math.Abs(float64(base.Y - top.Y)), true
     165              : }
        

Generated by: LCOV version 2.0-1