-
Notifications
You must be signed in to change notification settings - Fork 0
/
maprenderer.go
81 lines (66 loc) · 1.86 KB
/
maprenderer.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
package maprenderer
import (
"image"
"github.com/minetest-go/types"
)
type MapRenderOpts struct {
}
func NewDefaultMapRenderOpts() *MapRenderOpts {
return &MapRenderOpts{}
}
func RenderMap(na types.NodeAccessor, cr types.ColorResolver, from, to *types.Pos, opts *MapRenderOpts) (*image.NRGBA, error) {
// from = lowest, to = highest
from, to = types.SortPos(from, to)
search_dir := &types.Pos{0, -1, 0}
// prepare image
upLeft := image.Point{0, 0}
lowRight := image.Point{to[0] - from[0] + 1, to[2] - from[2] + 1}
img := image.NewNRGBA(image.Rectangle{upLeft, lowRight})
for x := from.X(); x <= to.X(); x++ {
for z := from.Z(); z <= to.Z(); z++ {
// top-down search
nodes, err := Probe(from, to, types.NewPos(x, to.Y(), z), search_dir, na, cr, false)
if err != nil {
return nil, err
}
if len(nodes) < 1 {
continue
}
top_node := nodes[0]
c := top_node.Color
if len(nodes) == 1 && top_node.Color.A == 255 {
// a single opaque node, draw with shadows
// add shadows for view-blocking neighbors
for _, above_pos := range []*types.Pos{{-1, 1, 0}, {0, 1, 1}} {
nn, err := na(top_node.Pos.Add(above_pos))
if err != nil {
return nil, err
}
if nn != nil && cr(nn.Name, 0) != nil {
c = ColorAdjust(c, -10)
}
}
// lighten up if no nodes directly nearby
for _, near_pos := range []*types.Pos{{-1, 0, 0}, {0, 0, 1}} {
nn, err := na(top_node.Pos.Add(near_pos))
if err != nil {
return nil, err
}
if nn == nil || cr(nn.Name, 0) == nil {
c = ColorAdjust(c, 10)
}
}
} else {
// multiple nodes with alpha channel
c = nodes[len(nodes)-1].Color
// bottom up color blending
for i := len(nodes) - 2; i >= 0; i-- {
node := nodes[i]
c = BlendColor(c, node.Color, 3)
}
}
img.Set(x-from.X(), to.Z()-z, *c)
}
}
return img, nil
}