Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created February 19, 2021 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/87ba104e9f54bb3e269dfc8f01614c48 to your computer and use it in GitHub Desktop.
Save peterhellberg/87ba104e9f54bb3e269dfc8f01614c48 to your computer and use it in GitHub Desktop.
Noise flow field using Ebiten and GFX
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
}
@peterhellberg
Copy link
Author

noise-flow-field

@peterhellberg
Copy link
Author

Screenshot 2021-02-20 at 01 03 37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment