diff --git a/.gitignore b/.gitignore index e3effb5..8a9853b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ community/seascape-shader/seascape-shader + +/.idea +*.exe diff --git a/README.md b/README.md index e8e5057..21dbb42 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This repository contains a few examples demonstrating [Pixel](https://github.com/faiface/pixel)'s functionality. -**To run an example**, navigate to it's directory, then `go run` the `main.go` file. For example: +**To run an example**, navigate to its directory, then `go run` the `main.go` file. For example: ``` $ cd platformer diff --git a/community/line-collisions/README.md b/community/line-collisions/README.md new file mode 100644 index 0000000..399034d --- /dev/null +++ b/community/line-collisions/README.md @@ -0,0 +1,12 @@ +# Line Collisions +Line collisions is a basic example of how to detect line collisions with a rectangle. + +![Screenshot](lineCollisionScreenshot.png) + +## Instructions +You can set the rectangle position using a left-click of the mouse. + +You can set the line position with two right-clicks of the mouse; the first right-click will set the beginning of the +line the second will set the end of the line. + +Any intersection points where the rectangle and line intersection will appear as red dots. diff --git a/community/line-collisions/lineCollisionScreenshot.png b/community/line-collisions/lineCollisionScreenshot.png new file mode 100644 index 0000000..d6af306 Binary files /dev/null and b/community/line-collisions/lineCollisionScreenshot.png differ diff --git a/community/line-collisions/main.go b/community/line-collisions/main.go new file mode 100644 index 0000000..2cc0ce6 --- /dev/null +++ b/community/line-collisions/main.go @@ -0,0 +1,89 @@ +package main + +import ( + "image/color" + + "github.com/faiface/pixel" + "github.com/faiface/pixel/imdraw" + "github.com/faiface/pixel/pixelgl" +) + +// These hold the state of whether we're placing the first or second point of the line. +const ( + clickLineA = iota + clickLineB +) + +var ( + winBounds = pixel.R(0, 0, 1024, 768) + + r = pixel.R(10, 10, 70, 50) + l = pixel.L(pixel.V(20, 20), pixel.V(100, 30)) + + clickLine = clickLineA +) + +func run() { + cfg := pixelgl.WindowConfig{ + Title: "Line collision", + Bounds: winBounds, + VSync: true, + } + + win, err := pixelgl.NewWindow(cfg) + if err != nil { + panic(err) + } + + imd := imdraw.New(nil) + + for !win.Closed() { + win.Clear(color.RGBA{R: 23, G: 39, B: 58, A: 125}) + imd.Clear() + + // When mouse left-click, move the rectangle so its' center is at the mouse position + if win.JustPressed(pixelgl.MouseButtonLeft) { + rectToMouse := r.Center().To(win.MousePosition()) + r = r.Moved(rectToMouse) + } + + // When mouse right-click, set either the beginning or end of the line. + if win.JustPressed(pixelgl.MouseButtonRight) { + if clickLine == clickLineA { + // Set the beginning of the line to the mouse position. + // To make it clearer to the user, set the end position 1 pixel (in each direction) away from the first + // point. + l = pixel.L(win.MousePosition(), win.MousePosition().Add(pixel.V(1, 1))) + clickLine = clickLineB + } else { + // Set the end point of the line. + l = pixel.L(l.A, win.MousePosition()) + clickLine = clickLineA + } + } + + // Draw the rectangle. + imd.Color = color.Black + imd.Push(r.Min, r.Max) + imd.Rectangle(3) + + // Draw the line. + imd.Color = color.RGBA{R: 10, G: 10, B: 250, A: 255} + imd.Push(l.A, l.B) + imd.Line(3) + + imd.Color = color.RGBA{R: 250, G: 10, B: 10, A: 255} + // Draw any intersection points. + for _, i := range r.IntersectionPoints(l) { + imd.Push(i) + imd.Circle(4, 0) + } + + imd.Draw(win) + win.Update() + } +} + +func main() { + pixelgl.Run(run) +} diff --git a/community/sudoku/sudoku.go b/community/sudoku/sudoku.go index 882987c..c1b3888 100644 --- a/community/sudoku/sudoku.go +++ b/community/sudoku/sudoku.go @@ -95,6 +95,9 @@ func run() { // initialize batch batch := pixel.NewBatch(&pixel.TrianglesData{}, atlas.Picture()) + // initialize text placeholder + num := text.New(pixel.ZV, atlas) + // setup game for i := 0; i < 9; i++ { for j := 0; j < 9; j++ { @@ -185,7 +188,7 @@ func run() { for a, sa := range board { for b, sb := range sa { if sb != 0 { - num := text.New(pixel.ZV, atlas) + num.Clear() num.WriteString(strconv.Itoa(sb)) num.DrawColorMask(batch, pixel.IM. diff --git a/community/tilemap/.gitignore b/community/tilemap/.gitignore new file mode 100644 index 0000000..912305e --- /dev/null +++ b/community/tilemap/.gitignore @@ -0,0 +1 @@ +tilemap diff --git a/community/tilemap/README.md b/community/tilemap/README.md new file mode 100644 index 0000000..766dd7b --- /dev/null +++ b/community/tilemap/README.md @@ -0,0 +1,11 @@ +# Pixel Tiles Example +This is a very simple example of how you might load a tilemap generated with [Tiled](https://www.mapeditor.org/) (i.e. a .tmx file), +in a game with [Pixel](https://github.com/faiface/pixel) for Go. + +The tilemap used in this example is humbly borrowed from James Bowman's repo here: [https://github.com/jamesbowman/tiled-maps](https://github.com/jamesbowman/tiled-maps). + +## Preview +![Screenshot of Tiles Window](./screenshot.png) + +## Tilemap +![Tilemap](./gameart2d-desert.png) diff --git a/community/tilemap/gameart2d-desert.png b/community/tilemap/gameart2d-desert.png new file mode 100644 index 0000000..89170fd Binary files /dev/null and b/community/tilemap/gameart2d-desert.png differ diff --git a/community/tilemap/gameart2d-desert.tmx b/community/tilemap/gameart2d-desert.tmx new file mode 100644 index 0000000..3102285 --- /dev/null +++ b/community/tilemap/gameart2d-desert.tmx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,25,25,36,37,36,23,23,37,25,23,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +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,20,0,0,0,32,2,11,12,8,8,8,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,37,0,26,0,0,0,0,17,19,0,0,0,0,0,0,0,0,0,0,0,0,0,0,29,30,0,0,0,0,0,0,0,0,0,0,0,0,40,2,3,11,12,8,9,0,0,0,0,0,2,3,4,0,0,0,0,0,0,0,0,0,0,2,11,12,8,8,8,8,8,13,13,13,13,20,0,0,0,0,2,11,12,8,8,8,8,8,9,0,0,0,17,19,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,1,2,3,3,3,3,4,5,0,0,0,0,0,0,0,0,0,0,0,23,1,2,3,3,3,3,4,0,0,0,33,34,35,0,0,40,2,3,11,12,8,8,8,8,9,0,0,0,0,0,7,8,9,0,33,34,35,0,0,0,0,2,11,12,8,8,8,8,8,8,8,9,0,0,0,0,0,0,2,11,12,8,8,8,8,8,8,8,9,0,0,0,0,33,34,35,33,34,35,33,34,35,0,0,0,0,0,27,0, +27,0,0,37,37,0,0,0,0,2,3,11,12,8,8,8,8,9,10,0,0,0,0,0,25,0,0,0,0,2,3,11,12,8,8,8,8,9,0,0,0,38,39,40,2,3,11,12,8,8,8,8,8,8,8,9,0,0,0,0,0,7,8,9,0,38,39,40,0,0,2,11,12,8,8,8,8,8,8,8,8,8,9,0,0,32,0,2,11,12,8,8,8,8,8,8,8,8,8,9,0,0,0,31,37,39,37,32,39,21,22,39,37,0,2,3,3,3,3,3, +3,3,3,3,3,3,3,3,11,12,8,8,8,8,8,8,8,14,15,3,3,3,3,3,3,3,3,3,11,12,8,8,8,8,8,8,8,9,0,0,2,3,3,11,12,8,8,8,8,8,8,8,8,8,8,9,0,0,0,0,0,7,8,14,15,3,3,3,3,11,12,8,8,8,8,8,8,8,8,8,8,8,14,15,3,3,11,12,8,8,8,8,8,8,8,8,8,8,8,9,0,0,0,17,18,18,18,18,18,18,18,18,19,0,7,8,8,8,8,8 + + + diff --git a/community/tilemap/main.go b/community/tilemap/main.go new file mode 100644 index 0000000..a10d6d4 --- /dev/null +++ b/community/tilemap/main.go @@ -0,0 +1,158 @@ +package main + +import ( + "fmt" + "image/png" + "math" + "os" + "time" + + "golang.org/x/image/colornames" + + "github.com/faiface/pixel" + "github.com/faiface/pixel/pixelgl" + "github.com/salviati/go-tmx/tmx" +) + +var clearColor = colornames.Skyblue + +func gameloop(win *pixelgl.Window, tilemap *tmx.Map) { + batches := make([]*pixel.Batch, 0) + batchIndices := make(map[string]int) + batchCounter := 0 + + // Load the sprites + sprites := make(map[string]*pixel.Sprite) + for _, tileset := range tilemap.Tilesets { + if _, alreadyLoaded := sprites[tileset.Image.Source]; !alreadyLoaded { + sprite, pictureData := loadSprite(tileset.Image.Source) + sprites[tileset.Image.Source] = sprite + batches = append(batches, pixel.NewBatch(&pixel.TrianglesData{}, pictureData)) + batchIndices[tileset.Image.Source] = batchCounter + batchCounter++ + } + } + + var ( + camPos = pixel.ZV + camSpeed = 1000.0 + camZoom = 0.2 + camZoomSpeed = 1.2 + ) + + last := time.Now() + for !win.Closed() { + dt := time.Since(last).Seconds() + last = time.Now() + + // Camera movement + cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos)) + win.SetMatrix(cam) + if win.Pressed(pixelgl.KeyLeft) { + camPos.X -= camSpeed * dt + } + if win.Pressed(pixelgl.KeyRight) { + camPos.X += camSpeed * dt + } + if win.Pressed(pixelgl.KeyDown) { + camPos.Y -= camSpeed * dt + } + if win.Pressed(pixelgl.KeyUp) { + camPos.Y += camSpeed * dt + } + camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y) + + win.Clear(clearColor) + + // Draw tiles + for _, batch := range batches { + batch.Clear() + } + + for _, layer := range tilemap.Layers { + for tileIndex, tile := range layer.DecodedTiles { + ts := layer.Tileset + tID := int(tile.ID) + + if tID == 0 { + // Tile ID 0 means blank, skip it. + continue + } + + // Calculate the framing for the tile within its tileset's source image + numRows := ts.Tilecount / ts.Columns + x, y := tileIDToCoord(tID, ts.Columns, numRows) + gamePos := indexToGamePos(tileIndex, tilemap.Width, tilemap.Height) + + iX := float64(x) * float64(ts.TileWidth) + fX := iX + float64(ts.TileWidth) + iY := float64(y) * float64(ts.TileHeight) + fY := iY + float64(ts.TileHeight) + + sprite := sprites[ts.Image.Source] + sprite.Set(sprite.Picture(), pixel.R(iX, iY, fX, fY)) + pos := gamePos.ScaledXY(pixel.V(float64(ts.TileWidth), float64(ts.TileHeight))) + sprite.Draw(batches[batchIndices[ts.Image.Source]], pixel.IM.Moved(pos)) + } + } + + for _, batch := range batches { + batch.Draw(win) + } + win.Update() + } +} + +func tileIDToCoord(tID int, numColumns int, numRows int) (x int, y int) { + x = tID % numColumns + y = numRows - (tID / numColumns) - 1 + return +} + +func indexToGamePos(idx int, width int, height int) pixel.Vec { + gamePos := pixel.V( + float64(idx%width)-1, + float64(height)-float64(idx/width), + ) + return gamePos +} + +func run() { + // Create the window with OpenGL + cfg := pixelgl.WindowConfig{ + Title: "Pixel Tilemaps", + Bounds: pixel.R(0, 0, 800, 600), + VSync: true, + } + + win, err := pixelgl.NewWindow(cfg) + panicIfErr(err) + + // Initialize art assets (i.e. the tilemap) + tilemap, err := tmx.ReadFile("gameart2d-desert.tmx") + panicIfErr(err) + + fmt.Println("use WASD to move camera around") + gameloop(win, tilemap) +} + +func loadSprite(path string) (*pixel.Sprite, *pixel.PictureData) { + f, err := os.Open(path) + panicIfErr(err) + + img, err := png.Decode(f) + panicIfErr(err) + + pd := pixel.PictureDataFromImage(img) + return pixel.NewSprite(pd, pd.Bounds()), pd +} + +func main() { + pixelgl.Run(run) +} + +func panicIfErr(err error) { + if err != nil { + panic(err) + } +} diff --git a/community/tilemap/screenshot.png b/community/tilemap/screenshot.png new file mode 100644 index 0000000..afbd7f3 Binary files /dev/null and b/community/tilemap/screenshot.png differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5dae761 --- /dev/null +++ b/go.mod @@ -0,0 +1,17 @@ +module github.com/faiface/pixel-examples + +go 1.16 + +require ( + github.com/aquilax/go-perlin v1.0.0 + github.com/faiface/beep v1.0.2 + github.com/faiface/pixel v0.10.0 + github.com/go-gl/glfw v0.0.0-20210410170116-ea3d685f79fb + github.com/go-gl/mathgl v1.0.0 + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 + github.com/pkg/errors v0.9.1 + github.com/pkg/profile v1.5.0 + github.com/salviati/go-tmx v0.0.0-20180901011116-8dae25beffeb + github.com/sqweek/dialog v0.0.0-20200911184034-8a3d98e8211d + golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2bf3d78 --- /dev/null +++ b/go.sum @@ -0,0 +1,71 @@ +github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf h1:FPsprx82rdrX2jiKyS17BH6IrTmUBYqZa/CXT4uvb+I= +github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf/go.mod h1:peYoMncQljjNS6tZwI9WVyQB3qZS6u79/N3mBOcnd3I= +github.com/aquilax/go-perlin v1.0.0 h1:7KBttX3KwqipwhmIVE/B2cEZVYiOZpoE/q8HsS6HBoQ= +github.com/aquilax/go-perlin v1.0.0/go.mod h1:z9Rl7EM4BZY0Ikp2fEN1I5mKSOJ26HQpk0O2TBdN2HE= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/faiface/beep v1.0.2 h1:UB5DiRNmA4erfUYnHbgU4UB6DlBOrsdEFRtcc8sCkdQ= +github.com/faiface/beep v1.0.2/go.mod h1:1yLb5yRdHMsovYYWVqYLioXkVuziCSITW1oarTeduQM= +github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380 h1:FvZ0mIGh6b3kOITxUnxS3tLZMh7yEoHo75v3/AgUqg0= +github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380/go.mod h1:zqnPFFIuYFFxl7uH2gYByJwIVKG7fRqlqQCbzAnHs9g= +github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 h1:baVdMKlASEHrj19iqjARrPbaRisD7EuZEVJj6ZMLl1Q= +github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3/go.mod h1:VEPNJUlxl5KdWjDvz6Q1l+rJlxF2i6xqDeGuGAxa87M= +github.com/faiface/pixel v0.10.0 h1:EHm3ZdQw2Ck4y51cZqFfqQpwLqNHOoXwbNEc9Dijql0= +github.com/faiface/pixel v0.10.0/go.mod h1:lU0YYcW77vL0F1CG8oX51GXurymL45MXd57otHNLK7A= +github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= +github.com/gdamore/tcell v1.1.1/go.mod h1:K1udHkiR3cOtlpKG5tZPD5XxrF7v2y7lDq7Whcj+xkQ= +github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw= +github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk= +github.com/go-gl/glfw v0.0.0-20210410170116-ea3d685f79fb h1:yMlfD+jmoG8wwBVLaFwtcgNBY207iWsYwtNC9u+T2yk= +github.com/go-gl/glfw v0.0.0-20210410170116-ea3d685f79fb/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72 h1:b+9H1GAsx5RsjvDFLoS5zkNBzIQMuVKUYQDmxU3N5XE= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/go-gl/mathgl v0.0.0-20190416160123-c4601bc793c7/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ= +github.com/go-gl/mathgl v1.0.0 h1:t9DznWJlXxxjeeKLIdovCOVJQk/GzDEL7h/h+Ro2B68= +github.com/go-gl/mathgl v1.0.0/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= +github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/gopherjs/gopherjs v0.0.0-20180628210949-0892b62f0d9f/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c h1:16eHWuMGvCjSfgRJKqIzapE78onvvTbdi1rMkU00lZw= +github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gopherjs/gopherwasm v0.1.1/go.mod h1:kx4n9a+MzHH0BJJhvlsQ65hqLFXDO/m256AsaDPQ+/4= +github.com/gopherjs/gopherwasm v1.0.0 h1:32nge/RlujS1Im4HNCJPp0NbBOAeBXFuT1KonUuLl+Y= +github.com/gopherjs/gopherwasm v1.0.0/go.mod h1:SkZ8z7CWBz5VXbhJel8TxCmAcsQqzgWGR/8nMhyhZSI= +github.com/hajimehoshi/go-mp3 v0.1.1/go.mod h1:4i+c5pDNKDrxl1iu9iG90/+fhP37lio6gNhjCx9WBJw= +github.com/hajimehoshi/oto v0.1.1/go.mod h1:hUiLWeBQnbDu4pZsAhOnGqMI1ZGibS6e2qhQdfpwz04= +github.com/hajimehoshi/oto v0.3.1 h1:cpf/uIv4Q0oc5uf9loQn7PIehv+mZerh+0KKma6gzMk= +github.com/hajimehoshi/oto v0.3.1/go.mod h1:e9eTLBB9iZto045HLbzfHJIc+jP3xaKrjZTghvb6fdM= +github.com/jfreymuth/oggvorbis v1.0.0 h1:aOpiihGrFLXpsh2osOlEvTcg5/aluzGQeC7m3uYWOZ0= +github.com/jfreymuth/oggvorbis v1.0.0/go.mod h1:abe6F9QRjuU9l+2jek3gj46lu40N4qlYxh2grqkLEDM= +github.com/jfreymuth/vorbis v1.0.0 h1:SmDf783s82lIjGZi8EGUUaS7YxPHgRj4ZXW/h7rUi7U= +github.com/jfreymuth/vorbis v1.0.0/go.mod h1:8zy3lUAm9K/rJJk223RKy6vjCZTWC61NA2QD06bfOE0= +github.com/lucasb-eyer/go-colorful v0.0.0-20181028223441-12d3b2882a08/go.mod h1:NXg0ArsFk0Y01623LgUqoqcouGDB+PwCCQlrwrG6xJ4= +github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= +github.com/mewkiz/flac v1.0.5/go.mod h1:EHZNU32dMF6alpurYyKHDLYpW1lYpBZ5WrXi/VuNIGs= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/profile v1.5.0 h1:042Buzk+NhDI+DeSAA62RwJL8VAuZUMQZUjCsRz1Mug= +github.com/pkg/profile v1.5.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/salviati/go-tmx v0.0.0-20180901011116-8dae25beffeb h1:xnT9hS4WTIDoD5dKlbDpBANISLHbW4JCEp4C5Mb4RLY= +github.com/salviati/go-tmx v0.0.0-20180901011116-8dae25beffeb/go.mod h1:MHMSHqNIP7nhf3dSczdtzy/1mKyYaQP7sHbXwmXvvaA= +github.com/sqweek/dialog v0.0.0-20200911184034-8a3d98e8211d h1:Chay1rwJnXxI27H+pzu7P81BKf647un9GOoRPTdXN18= +github.com/sqweek/dialog v0.0.0-20200911184034-8a3d98e8211d/go.mod h1:/qNPSY91qTz/8TgHEMioAUc6q7+3SOybeKczHMXFcXw= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/exp v0.0.0-20180710024300-14dda7b62fcd h1:nLIcFw7GiqKXUS7HiChg6OAYWgASB2H97dZKd1GhDSs= +golang.org/x/exp v0.0.0-20180710024300-14dda7b62fcd/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= +golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= +golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk= +golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/mobile v0.0.0-20180806140643-507816974b79 h1:t2JRgCWkY7Qaa1J2jal+wqC9OjbyHCHwIA9rVlRUSMo= +golang.org/x/mobile v0.0.0-20180806140643-507816974b79/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= +golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb h1:pf3XwC90UUdNPYWZdFjhGBE7DUFuK3Ct1zWmZ65QN30= +golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0/go.mod h1:OdE7CF6DbADk7lN8LIKRzRJTTZXIjtWgA5THM5lhBAw= diff --git a/shader/wavy/main.go b/shader/wavy/main.go index 8925b69..2950b43 100644 --- a/shader/wavy/main.go +++ b/shader/wavy/main.go @@ -67,6 +67,7 @@ func main() { var fragmentShader = ` #version 330 core +in vec2 vTexCoords; out vec4 fragColor; uniform sampler2D uTexture; @@ -77,7 +78,7 @@ uniform float uSpeed; uniform float uTime; void main() { - vec2 t = gl_FragCoord.xy / uTexBounds.zw; + vec2 t = vTexCoords / uTexBounds.zw; vec3 influence = texture(uTexture, t).rgb; if (influence.r + influence.g + influence.b > 0.3) {