mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2025-06-28 14:16:24 +02:00
Render transparent tiles if command line flag is set.
This commit is contained in:
@ -54,9 +54,10 @@ type BaseTileCreator struct {
|
||||
func NewBaseTileCreator(
|
||||
client *RedisClient,
|
||||
colors *Colors,
|
||||
transparent bool,
|
||||
baseDir string,
|
||||
update bool) *BaseTileCreator {
|
||||
renderer := NewRenderer(tileWidth, tileHeight)
|
||||
renderer := NewRenderer(tileWidth, tileHeight, transparent)
|
||||
return &BaseTileCreator{
|
||||
client: client,
|
||||
colors: colors,
|
||||
@ -118,7 +119,7 @@ func (btc *BaseTileCreator) CreateTile(x, z int16, i, j int) error {
|
||||
|
||||
image := btc.renderer.CreateShadedImage(
|
||||
16, 16, (tileWidth-2)*16, (tileHeight-2)*16,
|
||||
btc.colors.Colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
|
||||
btc.colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
|
||||
|
||||
path := filepath.Join(btc.baseDir, strconv.Itoa(i), fmt.Sprintf("%d.png", j))
|
||||
|
||||
|
@ -115,8 +115,8 @@ func (colors *Colors) BlendColors(span *Span, col color.RGBA, pos int32) color.R
|
||||
}
|
||||
const scale = float32(1) / float32(100)
|
||||
for ; curr != nil; curr = curr.Next {
|
||||
// At least 20% attenuation + 5% extra for each depth meter.
|
||||
factor := float32(min(100, 20+(curr.To-curr.From)*5)) * scale
|
||||
// At least 35% attenuation + 5% extra for each depth meter.
|
||||
factor := float32(min(100, 35+(curr.To-curr.From)*5)) * scale
|
||||
col = BlendColor(colors.Colors[curr.Value], col, factor)
|
||||
}
|
||||
return col
|
||||
|
@ -27,6 +27,8 @@ type Renderer struct {
|
||||
RejectedBlocks int
|
||||
SolidBlocks int
|
||||
TransparentBlocks int
|
||||
spans *SpanPool
|
||||
tBuffer []*Span
|
||||
}
|
||||
|
||||
type YOrder struct {
|
||||
@ -122,19 +124,29 @@ func (yo *YOrder) Pop() (x interface{}) {
|
||||
return x
|
||||
}
|
||||
|
||||
func NewRenderer(width, height int) (renderer *Renderer) {
|
||||
func NewRenderer(width, height int, transparent bool) (renderer *Renderer) {
|
||||
dim := width * height
|
||||
pixSize := dim * 16 * 16
|
||||
yBuffer := make([]int32, pixSize)
|
||||
cBuffer := make([]int32, pixSize)
|
||||
yMin := make([]int32, dim)
|
||||
|
||||
var tBuffer []*Span
|
||||
var spans *SpanPool
|
||||
|
||||
if transparent {
|
||||
tBuffer = make([]*Span, pixSize)
|
||||
spans = NewSpanPool()
|
||||
}
|
||||
|
||||
renderer = &Renderer{
|
||||
width: width,
|
||||
height: height,
|
||||
yBuffer: yBuffer,
|
||||
cBuffer: cBuffer,
|
||||
yMin: yMin}
|
||||
yMin: yMin,
|
||||
tBuffer: tBuffer,
|
||||
spans: spans}
|
||||
|
||||
renderer.Reset()
|
||||
return
|
||||
@ -153,6 +165,15 @@ func (r *Renderer) Reset() {
|
||||
for i, n := 0, len(r.yMin); i < n; i++ {
|
||||
r.yMin[i] = math.MinInt32
|
||||
}
|
||||
|
||||
if r.tBuffer != nil {
|
||||
for i, t := range r.tBuffer {
|
||||
if t != nil {
|
||||
r.spans.FreeAll(t)
|
||||
r.tBuffer[i] = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Renderer) IsFilled() bool {
|
||||
@ -189,37 +210,64 @@ func (r *Renderer) RenderBlock(block *Block, colors *Colors) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
if db.Transparent {
|
||||
r.TransparentBlocks++
|
||||
} else {
|
||||
r.SolidBlocks++
|
||||
}
|
||||
|
||||
w := r.width << 4
|
||||
ofs := int(bz)*w<<4 + int(bx)<<4
|
||||
yB := r.yBuffer
|
||||
|
||||
yMin := int32(math.MaxInt32)
|
||||
for z := 0; z < 16; z++ {
|
||||
for x := 0; x < 16; x++ {
|
||||
currentY := yB[ofs]
|
||||
if currentY < blockY {
|
||||
for y := 15; y >= 0; y-- {
|
||||
if c, ok := db.Content(x, y, z); ok {
|
||||
r.cBuffer[ofs] = c
|
||||
currentY = blockY + int32(y)
|
||||
yB[ofs] = currentY
|
||||
break
|
||||
|
||||
if db.Transparent && r.tBuffer != nil {
|
||||
r.TransparentBlocks++
|
||||
|
||||
for z := 0; z < 16; z++ {
|
||||
for x := 0; x < 16; x++ {
|
||||
currentY := yB[ofs]
|
||||
if currentY < blockY {
|
||||
for y := 15; y >= 0; y-- {
|
||||
if c, ok := db.Content(x, y, z); ok {
|
||||
currentY = blockY + int32(y)
|
||||
|
||||
if colors.IsTransparent(c) {
|
||||
r.tBuffer[ofs] = r.spans.Insert(r.tBuffer[ofs], currentY, c)
|
||||
} else {
|
||||
r.cBuffer[ofs] = c
|
||||
yB[ofs] = currentY
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if currentY < yMin {
|
||||
yMin = currentY
|
||||
}
|
||||
ofs++
|
||||
}
|
||||
if currentY < yMin {
|
||||
yMin = currentY
|
||||
}
|
||||
ofs++
|
||||
ofs += w - 16
|
||||
}
|
||||
|
||||
} else {
|
||||
r.SolidBlocks++
|
||||
for z := 0; z < 16; z++ {
|
||||
for x := 0; x < 16; x++ {
|
||||
currentY := yB[ofs]
|
||||
if currentY < blockY {
|
||||
for y := 15; y >= 0; y-- {
|
||||
if c, ok := db.Content(x, y, z); ok {
|
||||
r.cBuffer[ofs] = c
|
||||
currentY = blockY + int32(y)
|
||||
yB[ofs] = currentY
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if currentY < yMin {
|
||||
yMin = currentY
|
||||
}
|
||||
ofs++
|
||||
}
|
||||
ofs += w - 16
|
||||
}
|
||||
ofs += w - 16
|
||||
}
|
||||
|
||||
r.yMin[pos] = yMin
|
||||
|
||||
return
|
||||
@ -349,13 +397,15 @@ func safeColor(x int32) uint8 {
|
||||
|
||||
func (r *Renderer) CreateShadedImage(
|
||||
xOfs, zOfs, width, height int,
|
||||
colors []color.RGBA, background color.RGBA) *image.RGBA {
|
||||
cols *Colors, background color.RGBA) *image.RGBA {
|
||||
|
||||
image := image.NewRGBA(image.Rect(0, 0, width, height))
|
||||
|
||||
pw := r.width << 4
|
||||
|
||||
ofs, numCols := zOfs*pw+xOfs, int32(len(colors))
|
||||
cs := cols.Colors
|
||||
|
||||
ofs, numCols := zOfs*pw+xOfs, int32(len(cs))
|
||||
|
||||
stride := pw - width
|
||||
|
||||
@ -365,42 +415,91 @@ func (r *Renderer) CreateShadedImage(
|
||||
|
||||
pix := image.Pix
|
||||
|
||||
for z := height - 1; z >= 0; z-- {
|
||||
for x := 0; x < width; x++ {
|
||||
colIdx := r.cBuffer[ofs]
|
||||
if colIdx < 0 || colIdx >= numCols {
|
||||
pix[iofs] = background.R
|
||||
pix[iofs+1] = background.G
|
||||
pix[iofs+2] = background.B
|
||||
pix[iofs+3] = 0xff
|
||||
} else {
|
||||
var y, y1, y2 int32
|
||||
y = r.yBuffer[ofs]
|
||||
if x == 0 {
|
||||
y1 = y
|
||||
if r.tBuffer != nil { // Fast path for transparent images.
|
||||
for z := height - 1; z >= 0; z-- {
|
||||
for x := 0; x < width; x++ {
|
||||
colIdx := r.cBuffer[ofs]
|
||||
if colIdx < 0 || colIdx >= numCols {
|
||||
pix[iofs] = background.R
|
||||
pix[iofs+1] = background.G
|
||||
pix[iofs+2] = background.B
|
||||
pix[iofs+3] = 0xff
|
||||
} else {
|
||||
y1 = r.yBuffer[ofs-1]
|
||||
var y, y1, y2 int32
|
||||
y = r.yBuffer[ofs]
|
||||
if x == 0 {
|
||||
y1 = y
|
||||
} else {
|
||||
y1 = r.yBuffer[ofs-1]
|
||||
}
|
||||
if z == 0 {
|
||||
y2 = y
|
||||
} else {
|
||||
y2 = r.yBuffer[ofs+pw]
|
||||
}
|
||||
d := ((y - y1) + (y - y2)) * 12
|
||||
if d > 36 {
|
||||
d = 36
|
||||
}
|
||||
col := cs[colIdx]
|
||||
col = color.RGBA{
|
||||
R: safeColor(int32(col.R) + d),
|
||||
G: safeColor(int32(col.G) + d),
|
||||
B: safeColor(int32(col.B) + d),
|
||||
A: 0xff}
|
||||
if r.tBuffer[ofs] != nil {
|
||||
col = cols.BlendColors(r.tBuffer[ofs], col, y)
|
||||
}
|
||||
pix[iofs] = col.R
|
||||
pix[iofs+1] = col.G
|
||||
pix[iofs+2] = col.B
|
||||
pix[iofs+3] = col.A
|
||||
}
|
||||
if z == 0 {
|
||||
y2 = y
|
||||
} else {
|
||||
y2 = r.yBuffer[ofs+pw]
|
||||
}
|
||||
d := ((y - y1) + (y - y2)) * 12
|
||||
if d > 36 {
|
||||
d = 36
|
||||
}
|
||||
col := colors[colIdx]
|
||||
pix[iofs] = safeColor(int32(col.R) + d)
|
||||
pix[iofs+1] = safeColor(int32(col.G) + d)
|
||||
pix[iofs+2] = safeColor(int32(col.B) + d)
|
||||
pix[iofs+3] = 0xff
|
||||
iofs += 4
|
||||
ofs++
|
||||
}
|
||||
iofs += 4
|
||||
ofs++
|
||||
ofs += stride
|
||||
iofs -= istride
|
||||
}
|
||||
|
||||
} else { // Solid images.
|
||||
for z := height - 1; z >= 0; z-- {
|
||||
for x := 0; x < width; x++ {
|
||||
colIdx := r.cBuffer[ofs]
|
||||
if colIdx < 0 || colIdx >= numCols {
|
||||
pix[iofs] = background.R
|
||||
pix[iofs+1] = background.G
|
||||
pix[iofs+2] = background.B
|
||||
pix[iofs+3] = 0xff
|
||||
} else {
|
||||
var y, y1, y2 int32
|
||||
y = r.yBuffer[ofs]
|
||||
if x == 0 {
|
||||
y1 = y
|
||||
} else {
|
||||
y1 = r.yBuffer[ofs-1]
|
||||
}
|
||||
if z == 0 {
|
||||
y2 = y
|
||||
} else {
|
||||
y2 = r.yBuffer[ofs+pw]
|
||||
}
|
||||
d := ((y - y1) + (y - y2)) * 12
|
||||
if d > 36 {
|
||||
d = 36
|
||||
}
|
||||
col := cs[colIdx]
|
||||
pix[iofs] = safeColor(int32(col.R) + d)
|
||||
pix[iofs+1] = safeColor(int32(col.G) + d)
|
||||
pix[iofs+2] = safeColor(int32(col.B) + d)
|
||||
pix[iofs+3] = 0xff
|
||||
}
|
||||
iofs += 4
|
||||
ofs++
|
||||
}
|
||||
ofs += stride
|
||||
iofs -= istride
|
||||
}
|
||||
ofs += stride
|
||||
iofs -= istride
|
||||
}
|
||||
return image
|
||||
}
|
||||
|
Reference in New Issue
Block a user