Guest User

TrippyBackgrounds

a guest
Jun 12th, 2016
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Background/CrazyBackground"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex("Primary Texture", 2D) = "white" {}
  6.         _BlendTex("Secondary Texture", 2D) = "white" {}
  7.  
  8.         [Space(10)]
  9.  
  10.         _MainTintColor("Blend: Primary Tint", Color) = (1,1,1,1)
  11.         _BlendTintColor("Blend: Secondary Tint", Color) = (1,1,1,1)
  12.  
  13.         [Space(10)]
  14.         [Header(Blending Options)]
  15.         _BlendMode("Blend: Mode", int) = 0
  16.         _BlendWarp("Blend: Warp Factor", float) = 0.25
  17.         _BlendStay("Blend: Stay Factor", float) = 1
  18.         _BlendTimeScaling("Blend: Time Scaling", float) = 20
  19.            
  20.         [Space(10)]
  21.  
  22.         _TextureSize("Texture Size", int) = 256
  23.  
  24.         [Space(10)]
  25.  
  26.         _DistortX("X Mode", int) = 0
  27.         _DistortY("Y Mode", int) = 0
  28.  
  29.         [Space(10)]
  30.  
  31.         _AmplitudeX("X: Amplitude", float) = 16.0
  32.         _FrequencyX("X: Frequency", float) = 0.1
  33.         _CompressionX("X: Compression", float) = 1.0
  34.         _TimeScalingX("X: Time Scaling", float) = 32
  35.  
  36.         [Space(10)]
  37.  
  38.         _AmplitudeY("Y: Amplitude", float) = 16.0
  39.         _FrequencyY("Y: Frequency", float) = 0.1
  40.         _CompressionY("Y: Compression", float) = 1.0
  41.         _TimeScalingY("Y: Time Scaling", float) = 32
  42.     }
  43.     SubShader
  44.     {
  45.         // No culling or depth
  46.         Cull Off ZWrite Off ZTest Always
  47.  
  48.         Pass
  49.         {
  50.             CGPROGRAM
  51.             #pragma vertex vert
  52.             #pragma fragment frag
  53.            
  54.             #include "UnityCG.cginc"
  55.  
  56.             // Generic stuff
  57.             struct appdata
  58.             {
  59.                 float4 vertex : POSITION;
  60.                 float2 uv : TEXCOORD0;
  61.             };
  62.  
  63.             struct v2f
  64.             {
  65.                 float2 uv : TEXCOORD0;
  66.                 float4 vertex : SV_POSITION;
  67.             };
  68.  
  69.             v2f vert (appdata v)
  70.             {
  71.                 v2f o;
  72.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  73.                 o.uv = v.uv;
  74.                 return o;
  75.             }
  76.            
  77.             // Important stuff
  78.             sampler2D _MainTex;
  79.             sampler2D _BlendTex;
  80.  
  81.             fixed4 _MainTintColor;
  82.             fixed4 _BlendTintColor;
  83.  
  84.             float _BlendTimeScaling;
  85.  
  86.             int _TextureSize;
  87.  
  88.             float _AmplitudeX;
  89.             float _FrequencyX;
  90.             float _CompressionX;
  91.             float _TimeScalingX;
  92.  
  93.             float _AmplitudeY;
  94.             float _FrequencyY;
  95.             float _CompressionY;
  96.             float _TimeScalingY;
  97.  
  98.             int _DistortX;
  99.             int _DistortY;
  100.  
  101.             int _BlendMode;
  102.             float _BlendStay;
  103.             float _BlendWarp;
  104.  
  105.             fixed4 frag(v2f i) : SV_Target
  106.             {
  107.                 // position values are [0,1], so we scale it to texture size, and will later scale it back down.
  108.                 float x = i.uv.x * _TextureSize;
  109.                 float y = i.uv.y * _TextureSize;
  110.  
  111.                 // ================================= Distortion =================================
  112.  
  113.                 float new_x = x;
  114.                 float new_y = y;
  115.  
  116.                 // =========== DISTORT X ===========
  117.                 if (_DistortX == 1)
  118.                 {
  119.                     float offset_x = _AmplitudeX * sin(_FrequencyX * x + _TimeScalingX * _Time.x);
  120.                     new_x = x * _CompressionX + offset_x;
  121.                 }
  122.                 else if (_DistortX == 2)
  123.                 {
  124.                     float offset_x = _AmplitudeX * cos(_FrequencyX * x + _TimeScalingX * _Time.x);
  125.                     new_x = x * _CompressionX + offset_x;
  126.                 }
  127.                 else if (_DistortX == 3)
  128.                 {
  129.                     float offset_x = _AmplitudeX * sin(_FrequencyX * x + _TimeScalingX * _Time.x);
  130.                     new_x = x + _CompressionX * offset_x;
  131.                 }
  132.                 else if (_DistortX == 4)
  133.                 {
  134.                     float offset_x = _AmplitudeX * cos(_FrequencyX * x + _TimeScalingX * _Time.x);
  135.                     new_x = x + _CompressionX * offset_x;
  136.                 }
  137.  
  138.                 // =========== DISTORY Y ===========
  139.                 if (_DistortY == 1)
  140.                 {
  141.                     float offset_y = _AmplitudeY * sin(_FrequencyY * y + _TimeScalingY * _Time.x);
  142.                     new_y = y * _CompressionY + offset_y;
  143.                 }
  144.                 else if (_DistortY == 2)
  145.                 {
  146.                     float offset_y = _AmplitudeY * cos(_FrequencyY * y + _TimeScalingY * _Time.x);
  147.                     new_y = y * _CompressionY + offset_y;
  148.                 }
  149.                 else if (_DistortY == 3)
  150.                 {
  151.                     float offset_y = _AmplitudeY * sin(_FrequencyY * y + _TimeScalingY * _Time.x);
  152.                     new_y = y + _CompressionY * offset_y;
  153.                 }
  154.                 else if (_DistortY == 4)
  155.                 {
  156.                     float offset_y = _AmplitudeY * cos(_FrequencyY * y + _TimeScalingY * _Time.x);
  157.                     new_y = y + _CompressionY * offset_y;
  158.                 }
  159.  
  160.                 // =========== FINISH ===========
  161.  
  162.                 new_x = fmod(new_x / _TextureSize + 1.0, 1.0);
  163.                 new_y = fmod(new_y / _TextureSize + 1.0, 1.0);
  164.  
  165.                 float2 pos = float2(new_x, new_y);
  166.  
  167.                 // ================================= Blend =================================
  168.  
  169.                 float4 MainColor = tex2D(_MainTex, pos).rgba * _MainTintColor;
  170.                 float4 SecondaryColor = tex2D(_BlendTex, pos).rgba * _BlendTintColor;
  171.  
  172.                 float4 result = MainColor;
  173.  
  174.                 if (_BlendMode == 1)
  175.                 {
  176.                     float blendTime = (cos(_Time.x * _BlendTimeScaling) + 1) / 2;
  177.                     result = lerp(MainColor, SecondaryColor, blendTime);
  178.                 }
  179.                 else if (_BlendMode == 2)
  180.                 {
  181.                     float blendTime = sin(_Time.x * _BlendTimeScaling) / 2 * (_BlendStay * 2 + 1) + 0.5;
  182.                     if (blendTime <= 0)
  183.                     {
  184.                         result = MainColor;
  185.                     }
  186.                     else if (blendTime >= 1)
  187.                     {
  188.                         result = SecondaryColor;
  189.                     }
  190.                     else
  191.                     {
  192.                         result = lerp(MainColor, SecondaryColor, blendTime);
  193.                     }
  194.                 }
  195.                 else if (_BlendMode >= 3 && _BlendMode <= 4)
  196.                 {
  197.                     float d;
  198.                     if (_BlendMode == 3)
  199.                     {
  200.                         d = fmod(sin(_Time.x * _BlendTimeScaling) * sqrt((new_x - 0.5)*(new_x - 0.5) + (new_y - 0.5)*(new_y - 0.5)) / sqrt(_BlendWarp) + 1, 1);
  201.                     }
  202.                     else if (_BlendMode == 4)
  203.                     {
  204.                         d = fmod(tan(_Time.x * _BlendTimeScaling) * sqrt((new_x - 0.5)*(new_x - 0.5) + (new_y - 0.5)*(new_y - 0.5)) / sqrt(_BlendWarp) + 1, 1);
  205.                     }
  206.  
  207.                     if ((d > 0 && d < 0.1) ||
  208.                         (d > 0.2 && d < 0.3) ||
  209.                         (d > 0.4 && d < 0.5) ||
  210.                         (d > 0.6 && d < 0.7) ||
  211.                         (d > 0.8 && d < 0.9))
  212.                     {
  213.                         result = SecondaryColor;
  214.                     }
  215.                     else
  216.                     {
  217.                         result = MainColor;
  218.                     }
  219.                 }
  220.  
  221.                 return result;
  222.             }
  223.  
  224.             ENDCG
  225.         }
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment