Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "image"
  5. "image/color"
  6. "math"
  7. "time"
  8.  
  9. "github.com/faiface/pixel"
  10. "github.com/faiface/pixel/pixelgl"
  11. "github.com/peterhellberg/plasma"
  12. "github.com/peterhellberg/plasma/palette"
  13. )
  14.  
  15. const (
  16. size = 256
  17. fsize = float64(size)
  18. w = 640
  19. fw = float64(w)
  20. h = 480
  21. fh = float64(h)
  22. )
  23.  
  24. func run() {
  25. win, err := pixelgl.NewWindow(pixelgl.WindowConfig{
  26. Bounds: pixel.R(0, 0, float64(w), float64(h)),
  27. VSync: true,
  28. Undecorated: true,
  29. Resizable: false,
  30. })
  31. if err != nil {
  32. panic(err)
  33. }
  34.  
  35. win.SetSmooth(true)
  36.  
  37. texture1 := xorImage(size, size)
  38. texture2 := plasmaImage(size, size)
  39.  
  40. buffer := image.NewRGBA(image.Rect(0, 0, w, h))
  41. bufferPicture := pixel.PictureDataFromImage(buffer)
  42.  
  43. distanceTable := [h][w]int{}
  44. angleTable := [h][w]int{}
  45.  
  46. ratio := 32.0
  47.  
  48. for y := 0; y < h; y++ {
  49. for x := 0; x < w; x++ {
  50. fx, fy := float64(x), float64(y)
  51.  
  52. distance := int(ratio*size/math.Sqrt((fx-fw/2.0)*(fx-fw/2.0)+(fy-fh/2.0)*(fy-fh/2.0))) % size
  53. angle := int(0.5 * size * math.Atan2(fy-fh/2.0, fx-fw/2.0) / math.Pi)
  54.  
  55. distanceTable[y][x] = distance
  56. angleTable[y][x] = angle
  57. }
  58. }
  59.  
  60. centerMatrix := pixel.IM.Moved(win.Bounds().Center()).Scaled(
  61. win.Bounds().Center(), 1,
  62. )
  63.  
  64. start := time.Now()
  65.  
  66. go func() {
  67. for range time.Tick(32 * time.Millisecond) {
  68. animation := time.Since(start).Seconds()
  69.  
  70. shiftX := int(fsize * 1.2 * animation)
  71. shiftY := int(fsize * 0.10 * animation)
  72.  
  73. for y := 0; y < h; y++ {
  74. for x := 0; x < w; x++ {
  75. texture := texture1
  76.  
  77. if int(animation)%2 == 0 {
  78. texture = texture2
  79. }
  80.  
  81. buffer.Set(x, y, texture.At(
  82. int(uint(distanceTable[y][x]+shiftX)%size),
  83. int(uint(angleTable[y][x]+shiftY)%size),
  84. ))
  85. }
  86. }
  87.  
  88. bufferPicture = pixel.PictureDataFromImage(buffer)
  89. }
  90. }()
  91.  
  92. for !win.Closed() {
  93. win.SetClosed(win.JustPressed(pixelgl.KeyEscape) || win.JustPressed(pixelgl.KeyQ))
  94.  
  95. bs := pixel.NewSprite(bufferPicture, bufferPicture.Bounds())
  96.  
  97. bs.SetMatrix(centerMatrix)
  98. bs.Draw(win)
  99.  
  100. win.Update()
  101. }
  102. }
  103. func plasmaImage(w, h int) image.Image {
  104. return plasma.New(w, h, 64).Image(w, h, 12, palette.DefaultGradient)
  105. }
  106.  
  107. func xorImage(w, h int) image.Image {
  108. m := image.NewRGBA(image.Rect(0, 0, w, h))
  109.  
  110. for x := 0; x < w; x++ {
  111. for y := 0; y < h; y++ {
  112. u := uint8(x ^ y)
  113.  
  114. c := color.RGBA{
  115. (u) % 192,
  116. 24,
  117. (u & uint8(y)) % 128,
  118. 200,
  119. }
  120.  
  121. m.Set(x, y, c)
  122. }
  123. }
  124.  
  125. return m
  126. }
  127.  
  128. func main() {
  129. pixelgl.Run(run)
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement