Skip to content

Commit

Permalink
transparency in map mode
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Jun 21, 2024
1 parent bcdd240 commit 2c8a5a2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/minetest-go/colormapping v1.0.4
github.com/minetest-go/colormapping v1.0.5
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/minetest-go/colormapping v1.0.4 h1:EoIAvdjAyO7kUFcBipbPZmGFSOoSiHwDHpUeH4LSJ4k=
github.com/minetest-go/colormapping v1.0.4/go.mod h1:oltuvIhwZBc5YMTCABzziVUHdOCkSOIHr46zDS23Ev0=
github.com/minetest-go/colormapping v1.0.5 h1:axZm0xbOz0RQEIFvZmbxr3kuvebYiG0AAADPr8/ZQfg=
github.com/minetest-go/colormapping v1.0.5/go.mod h1:6UVusIQTqfWbcru5HTktWwB2BCNHUE3W0VkkRBaMfcw=
github.com/minetest-go/mapparser v1.0.0 h1:wQz0DNIb239YalZJr6cnZka+FurYo5sZB/IOfGq7Nno=
github.com/minetest-go/mapparser v1.0.0/go.mod h1:MsLonoU5w+V57rQfPoVs9wx0GSyUuIXYC0meJg2HxDI=
github.com/minetest-go/mtdb v1.1.48 h1:OWMbwH4Hq+RzK9UaW1nSIGWe2E+nZPGO5NDAZVdRWLw=
Expand Down
53 changes: 31 additions & 22 deletions maprenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func RenderMap(na types.NodeAccessor, cr types.ColorResolver, from, to *types.Po
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, true)
nodes, err := Probe(from, to, types.NewPos(x, to.Y(), z), search_dir, na, cr, false)
if err != nil {
return nil, err
}
Expand All @@ -35,32 +35,41 @@ func RenderMap(na types.NodeAccessor, cr types.ColorResolver, from, to *types.Po
continue
}

node := nodes[0]
top_node := nodes[0]
c := top_node.Color

c := cr(node.Name, node.Param2)
if c == nil {
continue
}
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(node.Pos.Add(above_pos))
if err != nil {
return nil, err
}
if nn != nil && cr(nn.Name, 0) != nil {
c = ColorAdjust(c, -10)
// 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(node.Pos.Add(near_pos))
if err != nil {
return nil, err
// 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)
}
}
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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion maprenderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestRenderMap(t *testing.T) {
err = cm.LoadDefaults()
assert.NoError(t, err)

from := types.NewPos(-10, -20, -10)
from := types.NewPos(0, -20, 0)
to := types.NewPos(100, 50, 100)
opts := &maprenderer.MapRenderOpts{}

Expand Down
12 changes: 12 additions & 0 deletions iso_utils.go → utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,15 @@ func ColorAdjust(c *color.RGBA, value int) *color.RGBA {
A: c.A,
}
}

func BlendColor(bg, fg *color.RGBA) *color.RGBA {
a := float64(fg.A) / 255
ai := 1 - a

return &color.RGBA{
R: uint8((float64(fg.R) * a) + (float64(bg.R) * ai)),
G: uint8((float64(fg.G) * a) + (float64(bg.G) * ai)),
B: uint8((float64(fg.B) * a) + (float64(bg.B) * ai)),
A: max(bg.A, fg.A),
}
}
File renamed without changes.

0 comments on commit 2c8a5a2

Please sign in to comment.