crt.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2022 The Ebitengine 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. //go:build ignore
  15. //kage:unit pixels
  16. // Reference: a public domain CRT effect
  17. // https://github.com/libretro/glsl-shaders/blob/master/crt/shaders/crt-lottes.glsl
  18. package main
  19. func Warp(pos vec2) vec2 {
  20. const (
  21. warpX = 0.031
  22. warpY = 0.041
  23. )
  24. pos = pos*2 - 1
  25. pos *= vec2(1+(pos.y*pos.y)*warpX, 1+(pos.x*pos.x)*warpY)
  26. return pos/2 + 0.5
  27. }
  28. func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
  29. // Adjust the texture position to [0, 1].
  30. origin := imageSrc0Origin()
  31. size := imageSrc0Size()
  32. pos := srcPos
  33. pos -= origin
  34. pos /= size
  35. pos = Warp(pos)
  36. return imageSrc0At(pos*size + origin)
  37. }