Last active
November 11, 2020 15:23
-
-
Save peterhellberg/723946589855dc61fa45df81e3f70bfc to your computer and use it in GitHub Desktop.
Blocks rendered by Ebiten with gfx.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/hajimehoshi/ebiten" | |
"github.com/peterhellberg/gfx" | |
) | |
const ( | |
title = "Ebiten GFX Blocks" | |
scale = 4 | |
w, h = 256, 128 | |
fw, fh = float64(w), float64(h) | |
fw2, fh2 = fw / 2, fh / 2 | |
) | |
var ( | |
pix = make([]uint8, w*h*4) | |
scaler = gfx.NewLinearScaler().Range(6, 0.5).Domain(0, fh) | |
flipY = gfx.V(1, -1) | |
bounds = gfx.R(0, 0, fw, fh) | |
defaultPos = gfx.V3(fw2, fh2, 0) | |
defaultOrigin = bounds.Center().ScaledXY(flipY).Vec3(1) | |
origin = defaultOrigin | |
pos = defaultPos | |
blocks = createBlocks() | |
) | |
func createBlocks() (blocks gfx.Blocks) { | |
for j := -6.0; j < 6; j++ { | |
for i := -6.0; i < 6; i++ { | |
aj, ai := gfx.MathAbs(j), gfx.MathAbs(i) | |
p := pos.AddXYZ(-fw2+(i*10), -fh2+(j*10), 0) | |
s := gfx.V3(3+aj, 3+ai, 3*ai+aj) | |
c := gfx.BlockColors[int(ai+aj)%8] | |
blocks.AddNewBlock(p, s, c) | |
blocks.AddNewBlock(p.AddXYZ(0, 0, -7*(ai+aj)), s.AddXYZ(-3, -3, 0), gfx.BlockColorBlack) | |
} | |
} | |
return blocks | |
} | |
func render() { | |
dst := gfx.NewImage(w, h, gfx.BlockColorBlack.Dark) | |
var drawCount int | |
for i := len(blocks) - 1; i >= 0; i-- { | |
b := blocks[i] | |
if shape, top, left, _ := b.Polygons(origin); bounds.Overlaps(shape.Rect()) { | |
drawCount += shape.Fill(dst, b.Color.Medium) | |
drawCount += left.Fill(dst, b.Color.Dark) | |
drawCount += top.Fill(dst, b.Color.Light) | |
} | |
drawCount++ | |
} | |
gfx.Log("drawCount: %d", drawCount) | |
pix = dst.Pix | |
} | |
func main() { | |
render() | |
ebiten.SetWindowDecorated(false) | |
ebiten.Run(func(screen *ebiten.Image) error { | |
if ebiten.IsKeyPressed(ebiten.KeyEscape) { | |
return gfx.ErrDone | |
} | |
if ebiten.IsDrawingSkipped() { | |
return nil | |
} | |
if updatePos(pos, origin) { | |
render() | |
} | |
return screen.ReplacePixels(pix) | |
}, w, h, scale, title) | |
} | |
func updatePos(oldPos, oldOrigin gfx.Vec3) bool { | |
if ebiten.IsKeyPressed(ebiten.KeyH) { | |
pos.Y += 0.6 // Diagonal up left | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyL) { | |
pos.X += 0.6 // Diagonal up right | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyJ) { | |
pos.X -= 0.6 // Diagonal down left | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyK) { | |
pos.Y -= 0.6 // Diagonal down right | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyW) { | |
pos.Z += 0.5 // Straight up | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyS) { | |
pos.Z -= 0.5 // Straight down | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyA) { | |
origin.X -= 1 // Straight left | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyD) { | |
origin.X += 1 // Straight right | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyC) { | |
pos, origin = defaultPos, defaultOrigin // Reset pos and origin | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyX) && origin.Z > 0.5 { | |
origin.Z -= 0.05 // Scale down | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyZ) && origin.Z < 10 { | |
origin.Z += 0.05 // Scale up | |
} | |
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { | |
origin.X, origin.Y = gfx.IV(ebiten.CursorPosition()).ScaledXY(flipY).XY() | |
} | |
switch touches := ebiten.Touches(); len(touches) { | |
case 1: | |
origin.X, origin.Y = gfx.IV(touches[0].Position()).ScaledXY(flipY).XY() | |
case 2: | |
tv := gfx.IV(touches[1].Position()) | |
origin.Z = scaler.ScaleFloat64(tv.Y) | |
origin.X, origin.Y = gfx.IV(touches[0].Position()).ScaledXY(flipY).XY() | |
} | |
return pos != oldPos || origin != oldOrigin | |
} |
Author
peterhellberg
commented
Feb 8, 2019
if shape := b.Shape(origin); bounds.Overlaps(shape.Rect()) {
drawCount += shape.Fill(dst, b.Color.Medium)
dt := gfx.NewDrawTarget(dst)
dt.MakeTriangles(b.TrianglesData(origin).Slice(0, 15)).Draw()
}
gfx-many-blocks.go
package main
import (
"flag"
"github.com/peterhellberg/gfx"
)
func main() {
var (
w int
h int
z float64
)
flag.IntVar(&w, "w", 2560, "Width of image")
flag.IntVar(&h, "h", 1440, "Height of image")
flag.Float64Var(&z, "z", 24, "Zoom level")
flag.Parse()
var (
blockColors = gfx.BlockColors
dst = gfx.NewImage(w, h)
rect = gfx.BoundsToRect(dst.Bounds())
origin = rect.Center().ScaledXY(gfx.V(1, -1)).Vec3(z)
)
var blocks gfx.Blocks
sn := gfx.NewSimplexNoise(123)
for x := -296.0; x < 296; x += 10 {
for y := -296.0; y < 296; y += 18 {
n := sn.Noise2D(x, y)
pos := gfx.V3(x, y, 0)
size := gfx.V3(8, 6+12*gfx.RandFloat64(), gfx.MathAbs(n)*10)
bc := blockColors[gfx.RandIntn(len(blockColors)-2)]
blocks.AddNewBlock(pos, size, bc)
}
}
gfx.SortSlice(blocks, func(i, j int) bool {
a, b := blocks[i], blocks[j]
av := a.Shape(origin).Bounds().Max.Y
bv := b.Shape(origin).Bounds().Max.Y
return av < bv
})
blocks.DrawPolygons(dst, origin)
gfx.SavePNG(gfx.Sprintf("gfx-many-blocks-%dx%d-z%g.png", w, h, z), dst)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment