antonkudin

Sprite additive distortion shader

Oct 31st, 2017
1,505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.67 KB | None | 0 0
  1.  
  2. //
  3. // shader starts here, c# companion script below
  4. //
  5.  
  6.  
  7. Shader "Custom/AdditiveScreen Distorted Noise+Main"{
  8. Properties {
  9.    
  10.     _MainTex ("Texture", 2D) = "white" {}
  11.     _NoiseTex ("Noise", 2D) = "white" {}
  12.     _AlphaClip ("Alpha clip", Range(0,1)) = .01
  13.     [Space]
  14.     _TintColor ("Tint Color", Color) = (1,1,1,1)
  15.     _EmissionGain ("Emission", Range(0, 1)) = 0.052
  16.     _OffsetBrightness ("Offset Emission", Range(0, 2)) = 0.052
  17.    
  18.     [Header(Distortion)]
  19.     _DistortionMult ("Scale", Float) = .05
  20.     _DistortionX ("Horisontal", Range (0, 3)) = .1
  21.     _DistortionY ("Vertical", Range (0, 3)) = .1
  22.     [Space]
  23.     [MaterialToggle] _LocalSpace("Local space", Float) = 0
  24.     [MaterialToggle] _ClipToSprite("Clip to sprite", Float) = 0
  25.  
  26.     [Header(Padding)]
  27.     _PaddingX ("Horisontal", Range (0, 3)) = .1
  28.     _PaddingY ("Vertical", Range (0, 3)) = .1
  29.    
  30.     [Header(Distortion Layers)]
  31.     _Params ("Size (MinMax) Move (MinMax)", Vector) = (32, 64, 1, 1)
  32.     _ParamsB ("Angle (DEG) Variat", Vector) = (-1.87, -1.27, 0, 0)
  33.     _Wind ("Move speed", Range (0, 2)) = .2
  34.  
  35.     [Header(Color Oscilation)]
  36.     _OscFreq ("Frequency", Range(0,120)) = 1
  37.     _OscAmpl ("Amplitude", Range(0,5)) = 0
  38.    
  39.     [Header(Sprite MetaData)]
  40.     _SpriteUV ("Sprite Rect", Vector) = (1,1,0,0)
  41.     _SpritePivot ("Sprite Pivot", Vector) = (1,1,0,0)
  42. }
  43.  
  44. Category {
  45.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
  46.     Lighting Off
  47.     Blend SrcAlpha One
  48.     ZWrite Off
  49.  
  50.     SubShader {
  51.         Pass {
  52.        
  53.             CGPROGRAM
  54. // Upgrade NOTE: excluded shader from DX11; has structs without semantics (struct v2f members worldPos)
  55. #pragma exclude_renderers d3d11
  56.             #pragma vertex vert
  57.             #pragma fragment frag
  58.  
  59.             fixed4 _TintColor;
  60.             float _EmissionGain, _OffsetBrightness;
  61.             half _DistortionX, _DistortionY, _DistortionMult;
  62.             // half _Rotation;
  63.             half  _LocalSpace, _ClipToSprite;
  64.             half _Wind, _AlphaClip;
  65.             half _PaddingX, _PaddingY;
  66.             half4 _Params;
  67.             half4 _ParamsB;
  68.             half4 _ParamsD, _DistortionDir;
  69.             static const int _Samples = 3;
  70.             sampler2D _MainTex;
  71.             float4 _MainTex_ST;
  72.             sampler2D _NoiseTex;
  73.             float4 _NoiseTex_ST;
  74.             half _OscAmpl, _OscFreq;
  75.             half4 _SpriteUV, _SpritePivot;
  76.  
  77.             struct appdata_t {
  78.                 float4 vertex : POSITION;
  79.                 float2 uv : TEXCOORD0;
  80.                 fixed4 color : COLOR;
  81.             };
  82.  
  83.             struct v2f {
  84.                 float4 vertex : SV_POSITION;
  85.                 fixed4 color : COLOR;
  86.                 float2 uv: TEXCOORD0;
  87.                 float2 worldPosition: TEXCOORD1;
  88.                 float2 angle: TEXCOORD2;
  89.             };
  90.  
  91.             float TriangleWave( float x ) { return abs( frac(x+0.5)*2.0 - 1.0 ); }  
  92.            
  93.             v2f vert (appdata_t v)
  94.             {
  95.                 v2f o;
  96.                
  97.                 half4 _Padding = half4(1+_PaddingX, 1+_PaddingY,0,0);
  98.                
  99.                 float2 luv = (v.uv-_SpriteUV.zw)/_SpriteUV.xy; // convert UV to 0..1 space
  100.                 o.uv = (luv-_SpritePivot.xy) * _Padding.xy + _SpritePivot.xy; //scale the uv
  101.                 o.uv = o.uv*_SpriteUV.xy + _SpriteUV.zw; // convert it back to texture space
  102.                
  103.                 o.vertex = UnityObjectToClipPos( v.vertex * _Padding);
  104.                
  105.                 o.color = fixed4( v.color.rgb, v.color.a *  (1 + TriangleWave(_Time.y*_OscFreq) * _OscAmpl));
  106.                 o.worldPosition = lerp(mul(unity_ObjectToWorld, v.vertex), v.vertex.xy,  _LocalSpace );
  107.                 o.angle = _LocalSpace;
  108.  
  109.                 return o;
  110.             }
  111.  
  112.             float2 rotate(float2 original, float angle ){
  113.                 angle = angle * 0.01745329252;
  114.                 float sinX = sin ( angle );
  115.                 float cosX = cos ( angle );
  116.                 float2x2 rotationMatrix = float2x2( cosX, -sinX, sinX, cosX);
  117.                 return mul(original, rotationMatrix );
  118.             }
  119.            
  120.             // rotated 45 degrees              
  121.             half2 noise45(half2 uv, float size, float2 speed){
  122.                 float2 xy = speed * _Time.x + uv * size;
  123.                 return tex2D(_NoiseTex, xy*_NoiseTex_ST.xy+_NoiseTex_ST.zw).rg-0.215;
  124.             }
  125.  
  126.             float tri(float speed){
  127.                 return abs(frac(_Time.y*speed)-0.5);
  128.             }
  129.  
  130.            
  131.             fixed4 frag (v2f i) : SV_Target
  132.             {
  133.                
  134.                 half2 distortUV = i.worldPosition.xy * half2(1+_PaddingX, 1+_PaddingY);
  135.                
  136.                 half2 offset = noise45(distortUV, 1/_Params.x, rotate(float2(_Wind,0), _ParamsB.x )*_Params.z);
  137.                
  138.                 for(int s=1;s<_Samples;s++){
  139.                     float s01 = s/(float)(_Samples-1);
  140.                     float2 direction = rotate(float2(_Wind,0), lerp(_ParamsB.x, _ParamsB.y, s01) );
  141.                     offset += (noise45(distortUV, 1/lerp(_Params.x, _Params.y, s01),
  142.                         direction * (lerp(_Params.z, _Params.w, s01)))) * lerp(.3, 1, s01);
  143.                 }
  144.                
  145.                 float2 uv = i.uv + half2(_DistortionX, _DistortionY) * offset * _DistortionMult;
  146.                
  147.                 half2 luv = (uv-_SpriteUV.zw) / _SpriteUV.xy;
  148.                
  149.                 if(_ClipToSprite>0)
  150.                     clip( min(min(luv.y,(1-luv.y)),min(luv.x,(1-luv.x))) );
  151.                
  152.                 fixed4 main = tex2D(_MainTex, uv);
  153.                 clip(main.a-_AlphaClip);
  154.                
  155.                 half offbri = max( abs(offset.x) , abs(offset.y)) * _OffsetBrightness;
  156.                
  157.                 return i.color * main * _TintColor * (exp((_EmissionGain + offbri) * 5));
  158.             }
  159.            
  160.             ENDCG
  161.         }
  162.     }  
  163. }
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. // c# companion script
  174. // SpriteUVToShader.cs -------------------------------------------------------------------------------------------------------------------------------- //
  175. // Save you your project, add to your SpriteRenderer gameObject
  176.  
  177. using UnityEngine;
  178. using System.Collections;
  179. using System.Collections.Generic;
  180.  
  181. [ExecuteInEditMode]
  182. [RequireComponent(typeof(SpriteRenderer))]
  183. public class SpriteUVToShader : MonoBehaviour {
  184.  
  185.     public string UV_property="_SpriteUV";
  186.     public string Pivot_property="_SpritePivot";
  187.     SpriteRenderer sr;
  188.     Sprite sprite;
  189.     MaterialPropertyBlock mpb;
  190.    
  191.     void OnValidate()
  192.     {
  193.         update();
  194.     }
  195.    
  196.     void OnWillRenderObject(){
  197.         update();
  198.     }
  199.    
  200.     void Start(){
  201.         update();
  202.     }
  203.    
  204.     void update(){
  205.         if(sr==null)
  206.             sr = GetComponent<SpriteRenderer>();
  207.        
  208.         if(sprite != sr.sprite){
  209.             sprite = sr.sprite;
  210.             applySprite(sr, sprite, ref mpb, UV_property, Pivot_property);
  211.         }
  212.     }
  213.    
  214.     public static void applySprite(Renderer renderer, Sprite toSprite, ref MaterialPropertyBlock mpb,
  215.         string uvProp=null, string pivotProp=null){
  216.        
  217.         if(toSprite==null) return;
  218.        
  219.         var scale = new Vector2(
  220.             toSprite.textureRect.width/ toSprite.texture.width,
  221.             toSprite.textureRect.height/toSprite.texture.height);
  222.            
  223.         var offset = new Vector2(
  224.             toSprite.rect.x/toSprite.texture.width,
  225.             toSprite.rect.y/toSprite.texture.height);
  226.        
  227.         Vector4 uvVector = new Vector4(scale.x,scale.y,offset.x,offset.y);
  228.         Vector4 pivotVector = new Vector4(toSprite.pivot.x/toSprite.rect.width,toSprite.pivot.y/toSprite.rect.height);
  229.        
  230.         if(string.IsNullOrEmpty(uvProp))
  231.             uvProp = "_MainTex_ST";
  232.  
  233.         if(mpb==null)
  234.             mpb = new MaterialPropertyBlock();
  235.  
  236.         renderer.GetPropertyBlock(mpb);
  237.        
  238.         mpb.SetVector(uvProp, uvVector);
  239.         if(!string.IsNullOrEmpty(pivotProp))
  240.             mpb.SetVector(pivotProp, pivotVector);
  241.        
  242.         renderer.SetPropertyBlock(mpb);
  243.     }
  244. }
Add Comment
Please, Sign In to add comment