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