Created
February 19, 2021 22:36
-
-
Save peterhellberg/87ba104e9f54bb3e269dfc8f01614c48 to your computer and use it in GitHub Desktop.
Noise flow field using Ebiten and 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 ( | |
"image" | |
"github.com/hajimehoshi/ebiten/v2" | |
"github.com/peterhellberg/gfx" | |
) | |
func main() { | |
ff := newFlowField(200, 200, 2, 4, gfx.PaletteEDG36, 789) | |
ebiten.SetWindowSize(ff.width*ff.scale, ff.height*ff.scale) | |
ebiten.SetWindowDecorated(false) | |
ebiten.RunGame(ff) | |
} | |
type FlowField struct { | |
frame int | |
width int | |
height int | |
pixels int | |
scale int | |
size int | |
palette gfx.Palette | |
*gfx.SimplexNoise | |
*image.RGBA | |
} | |
func newFlowField(width, height, scale, size int, palette gfx.Palette, seed int64) *FlowField { | |
return &FlowField{ | |
width: width, | |
height: height, | |
pixels: width * height, | |
scale: scale, | |
size: size, | |
palette: palette, | |
SimplexNoise: gfx.NewSimplexNoise(seed), | |
RGBA: gfx.NewImage(width, height), | |
} | |
} | |
func (ff *FlowField) Update() error { | |
if ebiten.IsKeyPressed(ebiten.KeyEscape) { | |
return gfx.ErrDone | |
} | |
for x := 0; x < ff.width; x += ff.size { | |
for y := 0; y < ff.height; y += ff.size { | |
r := gfx.IR(x, y, x+ff.size, y+ff.size) | |
n := ff.Noise3D(float64(x)/100, float64(y)/100, float64(ff.frame)/100) * 100 | |
t := (100 + n) / 200 | |
c := ff.palette.At(t) | |
gfx.DrawColor(ff, r, c) | |
} | |
} | |
ff.frame++ | |
return nil | |
} | |
func (ff *FlowField) Draw(screen *ebiten.Image) { | |
screen.ReplacePixels(ff.Pix) | |
} | |
func (ff *FlowField) Layout(w, h int) (int, int) { | |
return ff.width, ff.height | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment