-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
49 lines (44 loc) · 1.46 KB
/
util.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
package ui
import (
"github.com/deadsy/sdfx/sdf"
"github.com/deadsy/sdfx/vec/conv"
v2 "github.com/deadsy/sdfx/vec/v2"
"github.com/deadsy/sdfx/vec/v2i"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/text"
"golang.org/x/image/font/inconsolata"
"image/color"
"math"
"runtime"
)
func utilSdf2MinMax(s sdf.SDF2, bb sdf.Box2, cells v2i.Vec) (dmin, dmax float64) {
cellSize := bb.Size().Div(conv.V2iToV2(cells))
for x := 0; x < cells.X; x++ {
for y := 0; y < cells.Y; y++ {
// TODO: Reverse raycast (without limiting to a single direction) to find extreme values instead of 0s
// (should lower sample count for same results)
pos := bb.Min.Add((v2.Vec{X: float64(x), Y: float64(y)}).Mul(cellSize))
d := s.Evaluate(pos)
dmax = math.Max(dmax, d)
dmin = math.Min(dmin, d)
}
}
return
}
var defaultFont = inconsolata.Regular8x16 // Just a simple embedded font (to avoid problems with some platforms)
func drawDefaultTextWithShadow(screen *ebiten.Image, msg string, x, y int, c color.Color) {
if runtime.GOOS != "js" { // Rendering text is slow on JS
for dx := -1; dx <= 1; dx++ {
for dy := -1; dy <= 1; dy++ {
text.Draw(screen, msg, defaultFont, x+dx, y+dy, color.RGBA{R: 0, G: 0, B: 0, A: 50}) // Shadow first (background)
}
}
}
text.Draw(screen, msg, defaultFont, x, y, c)
}
func toBox2(box3 sdf.Box3) sdf.Box2 {
return sdf.Box2{
Min: v2.Vec{X: box3.Min.X, Y: box3.Min.Y},
Max: v2.Vec{X: box3.Max.X, Y: box3.Max.Y},
}
}