main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2020 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bytes"
  17. _ "embed"
  18. "image"
  19. _ "image/png"
  20. "log"
  21. "github.com/hajimehoshi/ebiten/v2"
  22. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  23. resources "github.com/hajimehoshi/ebiten/v2/examples/resources/images/shader"
  24. "github.com/hajimehoshi/ebiten/v2/inpututil"
  25. )
  26. var (
  27. //go:embed default.go
  28. default_go []byte
  29. //go:embed texel.go
  30. texel_go []byte
  31. //go:embed lighting.go
  32. lighting_go []byte
  33. //go:embed radialblur.go
  34. radialblur_go []byte
  35. //go:embed chromaticaberration.go
  36. chromaticaberration_go []byte
  37. //go:embed dissolve.go
  38. dissolve_go []byte
  39. //go:embed water.go
  40. water_go []byte
  41. )
  42. const (
  43. screenWidth = 640
  44. screenHeight = 480
  45. )
  46. var (
  47. gopherImage *ebiten.Image
  48. gopherBgImage *ebiten.Image
  49. normalImage *ebiten.Image
  50. noiseImage *ebiten.Image
  51. )
  52. func init() {
  53. // Decode an image from the image file's byte slice.
  54. img, _, err := image.Decode(bytes.NewReader(resources.Gopher_png))
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. gopherImage = ebiten.NewImageFromImage(img)
  59. }
  60. func init() {
  61. img, _, err := image.Decode(bytes.NewReader(resources.GopherBg_png))
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. gopherBgImage = ebiten.NewImageFromImage(img)
  66. }
  67. func init() {
  68. img, _, err := image.Decode(bytes.NewReader(resources.Normal_png))
  69. if err != nil {
  70. log.Fatal(err)
  71. }
  72. normalImage = ebiten.NewImageFromImage(img)
  73. }
  74. func init() {
  75. img, _, err := image.Decode(bytes.NewReader(resources.Noise_png))
  76. if err != nil {
  77. log.Fatal(err)
  78. }
  79. noiseImage = ebiten.NewImageFromImage(img)
  80. }
  81. var shaderSrcs = [][]byte{
  82. default_go,
  83. texel_go,
  84. lighting_go,
  85. radialblur_go,
  86. chromaticaberration_go,
  87. dissolve_go,
  88. water_go,
  89. }
  90. type Game struct {
  91. shaders map[int]*ebiten.Shader
  92. idx int
  93. time int
  94. }
  95. func (g *Game) Update() error {
  96. g.time++
  97. if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) {
  98. g.idx++
  99. g.idx %= len(shaderSrcs)
  100. }
  101. if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) {
  102. g.idx += len(shaderSrcs) - 1
  103. g.idx %= len(shaderSrcs)
  104. }
  105. if g.shaders == nil {
  106. g.shaders = map[int]*ebiten.Shader{}
  107. }
  108. if _, ok := g.shaders[g.idx]; !ok {
  109. s, err := ebiten.NewShader([]byte(shaderSrcs[g.idx]))
  110. if err != nil {
  111. return err
  112. }
  113. g.shaders[g.idx] = s
  114. }
  115. return nil
  116. }
  117. func (g *Game) Draw(screen *ebiten.Image) {
  118. s, ok := g.shaders[g.idx]
  119. if !ok {
  120. return
  121. }
  122. w, h := screen.Bounds().Dx(), screen.Bounds().Dy()
  123. cx, cy := ebiten.CursorPosition()
  124. op := &ebiten.DrawRectShaderOptions{}
  125. op.Uniforms = map[string]any{
  126. "Time": float32(g.time) / 60,
  127. "Cursor": []float32{float32(cx), float32(cy)},
  128. }
  129. op.Images[0] = gopherImage
  130. op.Images[1] = normalImage
  131. op.Images[2] = gopherBgImage
  132. op.Images[3] = noiseImage
  133. screen.DrawRectShader(w, h, s, op)
  134. msg := "Press Up/Down to switch the shader."
  135. ebitenutil.DebugPrint(screen, msg)
  136. }
  137. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  138. return screenWidth, screenHeight
  139. }
  140. func main() {
  141. ebiten.SetWindowSize(screenWidth, screenHeight)
  142. ebiten.SetWindowTitle("Shader (Ebitengine Demo)")
  143. if err := ebiten.RunGame(&Game{}); err != nil {
  144. log.Fatal(err)
  145. }
  146. }