bedrocktool/subcommands/world/chunk_render.go

103 lines
2.5 KiB
Go
Raw Normal View History

2022-09-04 14:26:32 +00:00
package world
2022-08-11 20:22:39 +00:00
import (
"image"
"image/color"
2022-09-04 14:53:21 +00:00
"github.com/bedrock-tool/bedrocktool/utils"
2022-09-04 14:26:32 +00:00
"github.com/df-mc/dragonfly/server/block"
2022-09-02 13:05:23 +00:00
"github.com/df-mc/dragonfly/server/block/cube"
2022-08-12 16:54:29 +00:00
"github.com/df-mc/dragonfly/server/world"
2022-08-11 20:22:39 +00:00
"github.com/df-mc/dragonfly/server/world/chunk"
)
2022-09-04 23:40:55 +00:00
func blockColorAt(c *chunk.Chunk, x uint8, y int16, z uint8) (blockColor color.RGBA) {
blockColor = color.RGBA{255, 0, 255, 255}
rid := c.Block(x, y, z, 0)
if rid == 0 && y == 0 { // void
blockColor = color.RGBA{0, 0, 0, 255}
2022-09-02 13:05:23 +00:00
} else {
2022-09-04 23:40:55 +00:00
b, found := world.BlockByRuntimeID(rid)
2022-09-02 13:05:23 +00:00
if found {
2023-03-05 19:44:04 +00:00
if d, ok := b.(block.LightDiffuser); ok && d.LightDiffusionLevel() == 0 && y > int16(c.Range().Min()) {
return blockColorAt(c, x, y-1, z)
}
2022-09-04 23:40:55 +00:00
if _, ok := b.(block.Water); ok {
y2 := c.HeightMap().At(x, z)
depth := y - y2
2022-09-05 11:13:43 +00:00
if depth > 0 {
blockColor = blockColorAt(c, x, y2, z)
}
2022-09-04 23:40:55 +00:00
bw := (&block.Water{}).Color()
bw.A = uint8(utils.Clamp(int(150+depth*7), 255))
blockColor = utils.BlendColors(blockColor, bw)
2022-09-05 19:08:57 +00:00
blockColor.R -= uint8(depth * 2)
blockColor.G -= uint8(depth * 2)
blockColor.B -= uint8(depth * 2)
2022-09-04 23:40:55 +00:00
} else {
blockColor = b.Color()
}
2022-09-02 13:05:23 +00:00
}
/*
2022-09-04 23:40:55 +00:00
if blockColor.R == 0 || blockColor.R == 255 && blockColor.B == 255 {
2022-09-02 13:05:23 +00:00
name, nbt := b.EncodeBlock()
2022-09-04 23:40:55 +00:00
fmt.Printf("unknown color %d %s %s %s\n", rid, reflect.TypeOf(b), name, nbt)
2022-09-02 13:05:23 +00:00
b.Color()
}
*/
}
2022-09-04 23:40:55 +00:00
return blockColor
2022-09-03 17:32:30 +00:00
}
func chunkGetColorAt(c *chunk.Chunk, x uint8, y int16, z uint8) color.RGBA {
p := cube.Pos{int(x), int(y), int(z)}
2023-01-29 21:20:13 +00:00
haveUp := false
2022-09-03 17:32:30 +00:00
p.Side(cube.FaceUp).Neighbours(func(neighbour cube.Pos) {
2022-10-14 16:33:56 +00:00
if neighbour.X() < 0 || neighbour.X() >= 16 || neighbour.Z() < 0 || neighbour.Z() >= 16 || neighbour.Y() > c.Range().Max() {
2022-09-03 17:32:30 +00:00
return
}
2023-01-29 21:20:13 +00:00
if !haveUp {
blockRid := c.Block(uint8(neighbour[0]), int16(neighbour[1]), uint8(neighbour[2]), 0)
if blockRid > 0 {
b, found := world.BlockByRuntimeID(blockRid)
2022-09-03 17:32:30 +00:00
if found {
if _, ok := b.(block.Air); !ok {
2023-01-29 21:20:13 +00:00
haveUp = true
2022-09-03 17:32:30 +00:00
}
}
}
}
}, cube.Range{int(y + 1), int(y + 1)})
col := blockColorAt(c, x, y, z)
2022-09-02 13:05:23 +00:00
2023-01-29 21:20:13 +00:00
if haveUp {
2022-09-02 16:20:06 +00:00
if col.R > 10 {
col.R -= 10
}
if col.G > 10 {
col.G -= 10
}
if col.B > 10 {
col.B -= 10
}
2022-09-02 13:05:23 +00:00
}
return col
}
2022-08-11 20:22:39 +00:00
func Chunk2Img(c *chunk.Chunk) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, 16, 16))
2022-09-04 23:40:55 +00:00
hm := c.HeightMapWithWater()
2022-08-12 16:54:29 +00:00
2022-08-11 20:22:39 +00:00
for x := uint8(0); x < 16; x++ {
for z := uint8(0); z < 16; z++ {
2022-08-12 16:54:29 +00:00
height := hm.At(x, z)
2022-09-02 13:05:23 +00:00
col := chunkGetColorAt(c, x, height, z)
2022-08-12 16:54:29 +00:00
img.SetRGBA(int(x), int(z), col)
2022-08-11 20:22:39 +00:00
}
}
return img
}