forked from unitoftime/glitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
115 lines (95 loc) · 3.15 KB
/
batch.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
package glitch
type meshDraw struct {
mesh *Mesh
matrix glMat4
mask RGBA
material Material
translucent bool
}
// For batching multiple sprites into one
type DrawBatch struct {
draws []meshDraw
boundsSet bool
bounds Box
}
func NewDrawBatch() *DrawBatch {
return &DrawBatch{
draws: make([]meshDraw, 0),
}
}
// func (b *DrawBatch) Buffer(pass *RenderPass) *DrawBatch {
// return &DrawBatch{
// mesh: b.mesh.Buffer(pass, b.material, b.Translucent),
// material: b.material,
// Translucent: b.Translucent,
// }
// }
func (b *DrawBatch) Add(mesh *Mesh, matrix glMat4, mask RGBA, material Material, translucent bool) {
b.draws = append(b.draws, meshDraw{
mesh: mesh,
matrix: matrix,
mask: mask,
material: material,
translucent: translucent,
})
newBounds := mesh.Bounds().Apply(matrix)
// TODO: Does this improve performance?
// if matrix != glMat4Ident {
// newBounds = newBounds.Apply(matrix)
// }
if b.boundsSet {
b.bounds = b.bounds.Union(newBounds)
} else {
b.boundsSet = true
b.bounds = newBounds
}
}
func (b *DrawBatch) Clear() {
b.draws = b.draws[:0]
b.boundsSet = false
b.bounds = Box{}
}
func (b *DrawBatch) Draw(target BatchTarget, matrix Mat4) {
for i := range b.draws {
mat := matrix.gl()
mat.Mul(&b.draws[i].matrix)
target.Add(b.draws[i].mesh, mat, b.draws[i].mask, b.draws[i].material, b.draws[i].translucent)
}
// target.Add(b.mesh, matrix.gl(), RGBA{1.0, 1.0, 1.0, 1.0}, b.material, b.Translucent)
// b.DrawColorMask(target, matrix, White)
}
func (b *DrawBatch) DrawColorMask(target BatchTarget, matrix Mat4, color RGBA) {
for i := range b.draws {
mat := matrix.gl()
mat.Mul(&b.draws[i].matrix)
mask := b.draws[i].mask.Mult(color)
target.Add(b.draws[i].mesh, mat, mask, b.draws[i].material, b.draws[i].translucent)
}
// target.Add(b.mesh, matrix.gl(), color, b.material, b.Translucent)
// for i := range b.draws {
// target.Add(b.draws[i].mesh, b.draws[i].matrix, b.draws[i].color, b.draws[i].material, b.draws[i].translucent)
// }
}
func (b *DrawBatch) RectDraw(target BatchTarget, bounds Rect) {
batchBounds := b.Bounds().Rect()
matrix := Mat4Ident
matrix.Scale(bounds.W() / batchBounds.W(), bounds.H() / batchBounds.H(), 1).Translate(bounds.W()/2 + bounds.Min[0], bounds.H()/2 + bounds.Min[1], 0)
b.Draw(target, matrix)
// b.RectDrawColorMask(target, bounds, RGBA{1, 1, 1, 1})
}
// TODO: Generalize this rectdraw logic. Copy paseted from Sprite
func (b *DrawBatch) RectDrawColorMask(target BatchTarget, bounds Rect, mask RGBA) {
batchBounds := b.Bounds().Rect()
matrix := Mat4Ident
matrix.Scale(bounds.W() / batchBounds.W(), bounds.H() / batchBounds.H(), 1).Translate(bounds.W()/2 + bounds.Min[0], bounds.H()/2 + bounds.Min[1], 0)
b.DrawColorMask(target, matrix, mask)
// // pass.SetTexture(0, s.texture)
// // pass.Add(s.mesh, matrix, RGBA{1.0, 1.0, 1.0, 1.0}, s.material)
// batchBounds := b.Bounds().Rect()
// matrix := Mat4Ident
// matrix.Scale(bounds.W() / batchBounds.W(), bounds.H() / batchBounds.H(), 1).Translate(bounds.W()/2 + bounds.Min[0], bounds.H()/2 + bounds.Min[1], 0)
// target.Add(b.mesh, matrix.gl(), mask, b.material, false)
}
func (b *DrawBatch) Bounds() Box {
return b.bounds
}