| 1 |
//go:build !tinygo |
| 2 |
// +build !tinygo |
| 3 |
|
| 4 |
package gfx |
| 5 |
|
| 6 |
import ( |
| 7 |
"image/draw" |
| 8 |
"math" |
| 9 |
) |
| 10 |
|
| 11 |
// Block has a position, size and color. |
| 12 |
type Block struct { |
| 13 |
Pos Vec3 |
| 14 |
Size Vec3 |
| 15 |
Color BlockColor |
| 16 |
} |
| 17 |
|
| 18 |
// NewBlock creates a new Block. |
| 19 |
func NewBlock(pos, size Vec3, ic BlockColor) Block { |
| 20 |
return Block{Pos: pos, Size: size, Color: ic} |
| 21 |
} |
| 22 |
|
| 23 |
// Box creates a box for the Block. |
| 24 |
func (b Block) Box() Box { |
| 25 |
return NewBox(b.Pos, b.Pos.Add(b.Size)) |
| 26 |
} |
| 27 |
|
| 28 |
// Behind checks if the box for b is in front of the box for a. |
| 29 |
func (b Block) Behind(a Block) bool { |
| 30 |
return b.Box().Behind(a.Box()) |
| 31 |
} |
| 32 |
|
| 33 |
// TrianglesData creates triangles data for the Block. |
| 34 |
func (b Block) TrianglesData(origin Vec3) *TrianglesData { |
| 35 |
sv, c := b.Corners(origin), b.Color |
| 36 |
|
| 37 |
return &TrianglesData{ |
| 38 |
// Shape |
| 39 |
Vx(sv.LeftUp, c.Medium), |
| 40 |
Vx(sv.LeftDown, c.Medium), |
| 41 |
Vx(sv.RightDown, c.Medium), |
| 42 |
Vx(sv.LeftUp, c.Medium), |
| 43 |
Vx(sv.RightUp, c.Medium), |
| 44 |
Vx(sv.RightDown, c.Medium), |
| 45 |
Vx(sv.BackUp, c.Light), |
| 46 |
Vx(sv.LeftUp, c.Light), |
| 47 |
Vx(sv.RightUp, c.Light), |
| 48 |
Vx(sv.LeftDown, c.Medium), |
| 49 |
Vx(sv.FrontDown, c.Medium), |
| 50 |
Vx(sv.RightDown, c.Medium), |
| 51 |
|
| 52 |
// Top |
| 53 |
Vx(sv.LeftUp, c.Light), |
| 54 |
Vx(sv.BackUp, c.Light), |
| 55 |
Vx(sv.FrontUp, c.Light), |
| 56 |
Vx(sv.FrontUp, c.Light), |
| 57 |
Vx(sv.BackUp, c.Light), |
| 58 |
Vx(sv.RightUp, c.Light), |
| 59 |
|
| 60 |
// Left |
| 61 |
Vx(sv.LeftUp, c.Dark), |
| 62 |
Vx(sv.LeftDown, c.Dark), |
| 63 |
Vx(sv.FrontDown, c.Dark), |
| 64 |
Vx(sv.FrontUp, c.Dark), |
| 65 |
Vx(sv.LeftUp, c.Dark), |
| 66 |
Vx(sv.FrontDown, c.Dark), |
| 67 |
|
| 68 |
// Right |
| 69 |
Vx(sv.FrontUp, c.Medium), |
| 70 |
Vx(sv.RightUp, c.Medium), |
| 71 |
Vx(sv.RightDown, c.Medium), |
| 72 |
Vx(sv.FrontUp, c.Medium), |
| 73 |
Vx(sv.FrontDown, c.Medium), |
| 74 |
Vx(sv.RightDown, c.Medium), |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Triangles for the Block |
| 79 |
func (b Block) Triangles(origin Vec3) []Triangle { |
| 80 |
c, l, m, d := b.Corners(origin), b.Color.Light, b.Color.Medium, b.Color.Dark |
| 81 |
|
| 82 |
return []Triangle{ |
| 83 |
T(Vx(c.FrontUp, d), Vx(c.LeftDown), Vx(c.LeftUp)), |
| 84 |
T(Vx(c.FrontUp, d), Vx(c.FrontDown), Vx(c.LeftDown)), |
| 85 |
T(Vx(c.FrontUp, m), Vx(c.FrontDown), Vx(c.RightDown)), |
| 86 |
T(Vx(c.FrontUp, m), Vx(c.RightUp), Vx(c.RightDown)), |
| 87 |
T(Vx(c.FrontUp, l), Vx(c.BackUp), Vx(c.LeftUp)), |
| 88 |
T(Vx(c.FrontUp, l), Vx(c.BackUp), Vx(c.RightUp)), |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// Draw block on dst at origin. |
| 93 |
func (b Block) Draw(dst draw.Image, origin Vec3) { |
| 94 |
DrawTriangles(dst, b.Triangles(origin)) |
| 95 |
} |
| 96 |
|
| 97 |
// DrawOver draws block over dst at origin. |
| 98 |
func (b Block) DrawOver(dst draw.Image, origin Vec3) { |
| 99 |
DrawTrianglesOver(dst, b.Triangles(origin)) |
| 100 |
} |
| 101 |
|
| 102 |
// DrawPolygons for block on dst at origin. |
| 103 |
func (b Block) DrawPolygons(dst draw.Image, origin Vec3) { |
| 104 |
shape, top, left, _ := b.Polygons(origin) |
| 105 |
|
| 106 |
shape.Fill(dst, b.Color.Medium) |
| 107 |
left.Fill(dst, b.Color.Dark) |
| 108 |
top.Fill(dst, b.Color.Light) |
| 109 |
} |
| 110 |
|
| 111 |
// DrawRectangles for block on dst at origin. |
| 112 |
func (b Block) DrawRectangles(dst draw.Image, origin Vec3) { |
| 113 |
_, top, left, right := b.Polygons(origin) |
| 114 |
|
| 115 |
t := top.Bounds() |
| 116 |
l := left.Bounds() |
| 117 |
r := right.Bounds() |
| 118 |
|
| 119 |
tx6 := t.Dx() / 6 |
| 120 |
ty2 := t.Dy() / 4 |
| 121 |
|
| 122 |
DrawColor(dst, IR(l.Min.X, l.Min.Y, l.Max.X, l.Max.Y-ty2), b.Color.Dark) |
| 123 |
DrawColor(dst, IR(r.Min.X, r.Min.Y, r.Max.X, r.Max.Y-ty2), b.Color.Medium) |
| 124 |
DrawColor(dst, IR(t.Min.X+tx6, t.Min.Y+ty2, t.Max.X-tx6, t.Max.Y), b.Color.Light) |
| 125 |
} |
| 126 |
|
| 127 |
// DrawBounds for block on dst at origin. |
| 128 |
func (b Block) DrawBounds(dst draw.Image, origin Vec3) { |
| 129 |
r := b.Rect(origin) |
| 130 |
|
| 131 |
if r.Area() < 20 { |
| 132 |
DrawColor(dst, r.Bounds(), b.Color.Medium) |
| 133 |
return |
| 134 |
} |
| 135 |
|
| 136 |
rw6 := r.W() / 6 |
| 137 |
|
| 138 |
DrawColor(dst, IR(int(r.Min.X), int(r.Min.Y), int(r.Max.X), int(r.Max.Y-rw6)), b.Color.Light) |
| 139 |
|
| 140 |
DrawLineBresenham(dst, r.Min, V(r.Max.X, r.Min.Y), b.Color.Medium) |
| 141 |
DrawLineBresenham(dst, r.Min, V(r.Min.X, r.Max.Y-rw6), b.Color.Dark) |
| 142 |
} |
| 143 |
|
| 144 |
// DrawWireframe block on dst at origin. |
| 145 |
func (b Block) DrawWireframe(dst draw.Image, origin Vec3) { |
| 146 |
DrawTrianglesWireframe(dst, b.Triangles(origin)) |
| 147 |
} |
| 148 |
|
| 149 |
// Polygons returns the shape, top, left and right polygons with coordinates based on origin. |
| 150 |
func (b Block) Polygons(origin Vec3) (shape, top, left, right Polygon) { |
| 151 |
vs := b.Corners(origin) |
| 152 |
|
| 153 |
return vs.Shape(), vs.Top(), vs.Left(), vs.Right() |
| 154 |
} |
| 155 |
|
| 156 |
// Rect for the block. |
| 157 |
func (b Block) Rect(origin Vec3) Rect { |
| 158 |
return b.Corners(origin).Rect() |
| 159 |
} |
| 160 |
|
| 161 |
// Shape returns the shape Polygon |
| 162 |
func (b Block) Shape(origin Vec3) Polygon { |
| 163 |
return b.Corners(origin).Shape() |
| 164 |
} |
| 165 |
|
| 166 |
// Space returns the BlockSpace for the Block. |
| 167 |
func (b Block) Space() BlockSpace { |
| 168 |
p, s := b.Pos, b.Size |
| 169 |
|
| 170 |
return BlockSpace{ |
| 171 |
LeftUp: V3(p.X, p.Y+s.Y, p.Z+s.Z), |
| 172 |
LeftDown: V3(p.X, p.Y+s.Y, p.Z), |
| 173 |
FrontDown: V3(p.X, p.Y, p.Z), |
| 174 |
RightDown: V3(p.X+s.X, p.Y, p.Z), |
| 175 |
RightUp: V3(p.X+s.X, p.Y, p.Z+s.Z), |
| 176 |
BackUp: V3(p.X+s.X, p.Y+s.Y, p.Z+s.Z), |
| 177 |
BackDown: V3(p.X+s.X, p.Y+s.Y, p.Z), |
| 178 |
FrontUp: V3(p.X, p.Y, p.Z+s.Z), |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
// Corners returns the screen coordinates for the Block corners. |
| 183 |
func (b Block) Corners(origin Vec3) BlockCorners { |
| 184 |
return b.Space().Corners(origin) |
| 185 |
} |
| 186 |
|
| 187 |
// BlockCorners contains screen coordinates for all of the corners. |
| 188 |
type BlockCorners struct { |
| 189 |
LeftUp Vec |
| 190 |
LeftDown Vec |
| 191 |
FrontDown Vec |
| 192 |
RightDown Vec |
| 193 |
RightUp Vec |
| 194 |
BackUp Vec |
| 195 |
BackDown Vec |
| 196 |
FrontUp Vec |
| 197 |
} |
| 198 |
|
| 199 |
// Rect for the Block corners. |
| 200 |
func (bc BlockCorners) Rect() Rect { |
| 201 |
return R(bc.LeftUp.X, bc.BackUp.Y, bc.RightUp.X, bc.FrontDown.Y) |
| 202 |
} |
| 203 |
|
| 204 |
// Shape Polygon. |
| 205 |
func (bc BlockCorners) Shape() Polygon { |
| 206 |
return Polygon{bc.LeftUp, bc.LeftDown, bc.FrontDown, bc.RightDown, bc.RightUp, bc.BackUp} |
| 207 |
} |
| 208 |
|
| 209 |
// Top face Polygon. |
| 210 |
func (bc BlockCorners) Top() Polygon { |
| 211 |
return Polygon{bc.LeftUp, bc.FrontUp, bc.RightUp, bc.BackUp} |
| 212 |
} |
| 213 |
|
| 214 |
// Left face Polygon. |
| 215 |
func (bc BlockCorners) Left() Polygon { |
| 216 |
return Polygon{bc.LeftUp, bc.LeftDown, bc.FrontDown, bc.FrontUp} |
| 217 |
} |
| 218 |
|
| 219 |
// Right face Polygon. |
| 220 |
func (bc BlockCorners) Right() Polygon { |
| 221 |
return Polygon{bc.FrontUp, bc.FrontDown, bc.RightDown, bc.RightUp} |
| 222 |
} |
| 223 |
|
| 224 |
// BlockSpace contains 3D space coordinates for the block corners. |
| 225 |
type BlockSpace struct { |
| 226 |
LeftUp Vec3 |
| 227 |
LeftDown Vec3 |
| 228 |
FrontDown Vec3 |
| 229 |
RightDown Vec3 |
| 230 |
RightUp Vec3 |
| 231 |
BackUp Vec3 |
| 232 |
BackDown Vec3 |
| 233 |
FrontUp Vec3 |
| 234 |
} |
| 235 |
|
| 236 |
// Corners returns the screen coordinates for all of the Block corners. |
| 237 |
func (bs BlockSpace) Corners(origin Vec3) BlockCorners { |
| 238 |
return BlockCorners{ |
| 239 |
LeftUp: bs.CornerLeftUp(origin), |
| 240 |
LeftDown: bs.CornerLeftDown(origin), |
| 241 |
FrontDown: bs.CornerFrontDown(origin), |
| 242 |
RightDown: bs.CornerRightDown(origin), |
| 243 |
RightUp: bs.CornerRightUp(origin), |
| 244 |
BackUp: bs.CornerBackUp(origin), |
| 245 |
BackDown: bs.CornerBackDown(origin), |
| 246 |
FrontUp: bs.CornerFrontUp(origin), |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
// CornerLeftUp returns the screen coordinate for the LeftUp corner. |
| 251 |
func (bs BlockSpace) CornerLeftUp(origin Vec3) Vec { |
| 252 |
return blockCorner(bs.LeftUp, origin) |
| 253 |
} |
| 254 |
|
| 255 |
// CornerLeftDown returns the screen coordinate for the LeftDown corner. |
| 256 |
func (bs BlockSpace) CornerLeftDown(origin Vec3) Vec { |
| 257 |
return blockCorner(bs.LeftDown, origin) |
| 258 |
} |
| 259 |
|
| 260 |
// CornerFrontDown returns the screen coordinate for the FrontDown corner. |
| 261 |
func (bs BlockSpace) CornerFrontDown(origin Vec3) Vec { |
| 262 |
return blockCorner(bs.FrontDown, origin) |
| 263 |
} |
| 264 |
|
| 265 |
// CornerRightDown returns the screen coordinate for the RightDown corner. |
| 266 |
func (bs BlockSpace) CornerRightDown(origin Vec3) Vec { |
| 267 |
return blockCorner(bs.RightDown, origin) |
| 268 |
} |
| 269 |
|
| 270 |
// CornerRightUp returns the screen coordinate for the RightUp corner. |
| 271 |
func (bs BlockSpace) CornerRightUp(origin Vec3) Vec { |
| 272 |
return blockCorner(bs.RightUp, origin) |
| 273 |
} |
| 274 |
|
| 275 |
// CornerBackUp returns the screen coordinate for the BackUp corner. |
| 276 |
func (bs BlockSpace) CornerBackUp(origin Vec3) Vec { |
| 277 |
return blockCorner(bs.BackUp, origin) |
| 278 |
} |
| 279 |
|
| 280 |
// CornerBackDown returns the screen coordinate for the BackDown corner. |
| 281 |
func (bs BlockSpace) CornerBackDown(origin Vec3) Vec { |
| 282 |
return blockCorner(bs.BackDown, origin) |
| 283 |
} |
| 284 |
|
| 285 |
// CornerFrontUp returns the screen coordinate for the FrontUp corner. |
| 286 |
func (bs BlockSpace) CornerFrontUp(origin Vec3) Vec { |
| 287 |
return blockCorner(bs.FrontUp, origin) |
| 288 |
} |
| 289 |
|
| 290 |
// blockCorner converts a 3D space corner and origin into a screen coordinate corner. |
| 291 |
func blockCorner(pos, origin Vec3) Vec { |
| 292 |
h, v := spaceToIso(pos) |
| 293 |
|
| 294 |
// Convert the given 2D isometric coordinates to 2D screen coordinates. |
| 295 |
x := h*origin.Z + origin.X |
| 296 |
y := -(v*origin.Z + origin.Y) |
| 297 |
|
| 298 |
return V(x, y) |
| 299 |
} |
| 300 |
|
| 301 |
// Convert 3D space coordinates to flattened 2D isometric coordinates. |
| 302 |
// x and y coordinates are oblique axes separated by 120 degrees. |
| 303 |
// h,v are the horizontal and vertical distances from the origin. |
| 304 |
func spaceToIso(space Vec3) (h, v float64) { |
| 305 |
x, y := space.X+space.Z, space.Y+space.Z |
| 306 |
|
| 307 |
h = (x - y) * math.Sqrt(3) / 2 |
| 308 |
v = (x + y) / 2 |
| 309 |
|
| 310 |
return h, v |
| 311 |
} |