rainbow.swsl 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. uniform sampler2D SCREEN_TEXTURE;
  2. uniform highp float effectScale;
  3. const highp float TimeScale = 0.15;
  4. const highp float DistortionScale = 0.02; // how strongly to warp the screen
  5. const highp float NoiseScale = 4.0; // scale of the random noise
  6. const highp float MaxColorMix = 0.05; // rainbow effect strength. at 1.0, you wont be able to see the screen anymore
  7. const highp float BaseColorMult = 8.0; // multiplier of the underlying screen texture for the rainbow effect
  8. const highp float BaseColorPow = 0.8; // exponent for the rainbow effect's
  9. const highp float CenterRadius = 200.0; // radius of the gradient used to tone down the distortion effect
  10. const highp float CenterMinDist = 0.4; // minimum distance from the center of the screen for the distortion to appear at all
  11. const highp float CenterPow = 3.0; // the exponent used for the distortion center
  12. const highp float CenterColorRadius = 250.0; // radius of the gradient used to tone down the color effect
  13. const highp float CenterColorMinDist = 0.2; // minimum distance from the center of the screen for the color to appear at all
  14. const highp float CenterColorPow = 1.25; // the exponent used for the color's center
  15. // noise() and hsv2rgb_smooth() were converted from shadertoy examples, which were released under the MIT License:
  16. // The MIT License
  17. // Copyright © 2013 Inigo Quilez
  18. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. // https://www.youtube.com/c/InigoQuilez
  20. // https://iquilezles.org
  21. highp float noise( highp vec2 p ) {
  22. const highp float K1 = 0.366025404;
  23. const highp float K2 = 0.211324865;
  24. highp vec2 i = floor( p + (p.x+p.y)*K1 );
  25. highp vec2 a = p - i + (i.x+i.y)*K2;
  26. highp float m = step(a.y,a.x);
  27. highp vec2 o = vec2(m,1.0-m);
  28. highp vec2 b = a - o + K2;
  29. highp vec2 c = a - 1.0 + 2.0*K2;
  30. highp vec3 h = max( 0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 );
  31. highp vec3 n = h*h*h*h*vec3( dot(a,zRandom(i+0.0)), dot(b,zRandom(i+o)), dot(c,zRandom(i+1.0)));
  32. return dot( n, vec3(70.0) );
  33. }
  34. highp vec3 hsv2rgb_smooth( highp vec3 c ) {
  35. highp vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
  36. rgb = rgb*rgb*(3.0-2.0*rgb);
  37. return c.z * mix( vec3(1.0), rgb, c.y);
  38. }
  39. highp float mixNoise(highp vec2 point, highp float phase) {
  40. highp float time = TIME * TimeScale + phase;
  41. highp float a = noise( NoiseScale * point - time);
  42. highp float b = noise( NoiseScale * point + time );
  43. return mix(a,b,0.5);
  44. }
  45. highp float genGradient(highp vec2 center, highp vec2 coord, highp float radius, highp float dist, highp float power) {
  46. return pow(clamp((length(center - coord) / radius) - dist, 0.0, 1.0), power);
  47. }
  48. void fragment() {
  49. highp vec2 coord = FRAGCOORD.xy * SCREEN_PIXEL_SIZE.xy;
  50. highp vec2 aspect = vec2(1.0/SCREEN_PIXEL_SIZE.x, 1.0/SCREEN_PIXEL_SIZE.y);
  51. highp vec2 center = aspect * 0.5;
  52. // warp the screen.
  53. highp vec2 offset = vec2(mixNoise(coord, 0.), mixNoise(coord, 5.));
  54. highp float centergradient = genGradient(center, FRAGCOORD.xy, CenterRadius, CenterMinDist, CenterPow);
  55. COLOR = zTextureSpec(SCREEN_TEXTURE, coord + effectScale * (DistortionScale * centergradient) * offset);
  56. // apply rainbow effect.
  57. highp float hue = 1. + mixNoise(coord, 10.);
  58. highp vec3 color = hsv2rgb_smooth(vec3(hue,1.0,1.0));
  59. highp float centercolor = genGradient(center, FRAGCOORD.xy, CenterColorRadius, CenterColorMinDist, CenterColorPow);
  60. highp float coloration = pow((COLOR.x + COLOR.y + COLOR.z) * BaseColorMult, BaseColorPow) * centercolor;
  61. COLOR.xyz = mix(COLOR.xyz, color, MaxColorMix * effectScale * effectScale * coloration);
  62. }