Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // shader starts here, c# companion script below
- //
- Shader "Custom/AdditiveScreen Distorted Noise+Main"{
- Properties {
- _MainTex ("Texture", 2D) = "white" {}
- _NoiseTex ("Noise", 2D) = "white" {}
- _AlphaClip ("Alpha clip", Range(0,1)) = .01
- [Space]
- _TintColor ("Tint Color", Color) = (1,1,1,1)
- _EmissionGain ("Emission", Range(0, 1)) = 0.052
- _OffsetBrightness ("Offset Emission", Range(0, 2)) = 0.052
- [Header(Distortion)]
- _DistortionMult ("Scale", Float) = .05
- _DistortionX ("Horisontal", Range (0, 3)) = .1
- _DistortionY ("Vertical", Range (0, 3)) = .1
- [Space]
- [MaterialToggle] _LocalSpace("Local space", Float) = 0
- [MaterialToggle] _ClipToSprite("Clip to sprite", Float) = 0
- [Header(Padding)]
- _PaddingX ("Horisontal", Range (0, 3)) = .1
- _PaddingY ("Vertical", Range (0, 3)) = .1
- [Header(Distortion Layers)]
- _Params ("Size (MinMax) Move (MinMax)", Vector) = (32, 64, 1, 1)
- _ParamsB ("Angle (DEG) Variat", Vector) = (-1.87, -1.27, 0, 0)
- _Wind ("Move speed", Range (0, 2)) = .2
- [Header(Color Oscilation)]
- _OscFreq ("Frequency", Range(0,120)) = 1
- _OscAmpl ("Amplitude", Range(0,5)) = 0
- [Header(Sprite MetaData)]
- _SpriteUV ("Sprite Rect", Vector) = (1,1,0,0)
- _SpritePivot ("Sprite Pivot", Vector) = (1,1,0,0)
- }
- Category {
- Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
- Lighting Off
- Blend SrcAlpha One
- ZWrite Off
- SubShader {
- Pass {
- CGPROGRAM
- // Upgrade NOTE: excluded shader from DX11; has structs without semantics (struct v2f members worldPos)
- #pragma exclude_renderers d3d11
- #pragma vertex vert
- #pragma fragment frag
- fixed4 _TintColor;
- float _EmissionGain, _OffsetBrightness;
- half _DistortionX, _DistortionY, _DistortionMult;
- // half _Rotation;
- half _LocalSpace, _ClipToSprite;
- half _Wind, _AlphaClip;
- half _PaddingX, _PaddingY;
- half4 _Params;
- half4 _ParamsB;
- half4 _ParamsD, _DistortionDir;
- static const int _Samples = 3;
- sampler2D _MainTex;
- float4 _MainTex_ST;
- sampler2D _NoiseTex;
- float4 _NoiseTex_ST;
- half _OscAmpl, _OscFreq;
- half4 _SpriteUV, _SpritePivot;
- struct appdata_t {
- float4 vertex : POSITION;
- float2 uv : TEXCOORD0;
- fixed4 color : COLOR;
- };
- struct v2f {
- float4 vertex : SV_POSITION;
- fixed4 color : COLOR;
- float2 uv: TEXCOORD0;
- float2 worldPosition: TEXCOORD1;
- float2 angle: TEXCOORD2;
- };
- float TriangleWave( float x ) { return abs( frac(x+0.5)*2.0 - 1.0 ); }
- v2f vert (appdata_t v)
- {
- v2f o;
- half4 _Padding = half4(1+_PaddingX, 1+_PaddingY,0,0);
- float2 luv = (v.uv-_SpriteUV.zw)/_SpriteUV.xy; // convert UV to 0..1 space
- o.uv = (luv-_SpritePivot.xy) * _Padding.xy + _SpritePivot.xy; //scale the uv
- o.uv = o.uv*_SpriteUV.xy + _SpriteUV.zw; // convert it back to texture space
- o.vertex = UnityObjectToClipPos( v.vertex * _Padding);
- o.color = fixed4( v.color.rgb, v.color.a * (1 + TriangleWave(_Time.y*_OscFreq) * _OscAmpl));
- o.worldPosition = lerp(mul(unity_ObjectToWorld, v.vertex), v.vertex.xy, _LocalSpace );
- o.angle = _LocalSpace;
- return o;
- }
- float2 rotate(float2 original, float angle ){
- angle = angle * 0.01745329252;
- float sinX = sin ( angle );
- float cosX = cos ( angle );
- float2x2 rotationMatrix = float2x2( cosX, -sinX, sinX, cosX);
- return mul(original, rotationMatrix );
- }
- // rotated 45 degrees
- half2 noise45(half2 uv, float size, float2 speed){
- float2 xy = speed * _Time.x + uv * size;
- return tex2D(_NoiseTex, xy*_NoiseTex_ST.xy+_NoiseTex_ST.zw).rg-0.215;
- }
- float tri(float speed){
- return abs(frac(_Time.y*speed)-0.5);
- }
- fixed4 frag (v2f i) : SV_Target
- {
- half2 distortUV = i.worldPosition.xy * half2(1+_PaddingX, 1+_PaddingY);
- half2 offset = noise45(distortUV, 1/_Params.x, rotate(float2(_Wind,0), _ParamsB.x )*_Params.z);
- for(int s=1;s<_Samples;s++){
- float s01 = s/(float)(_Samples-1);
- float2 direction = rotate(float2(_Wind,0), lerp(_ParamsB.x, _ParamsB.y, s01) );
- offset += (noise45(distortUV, 1/lerp(_Params.x, _Params.y, s01),
- direction * (lerp(_Params.z, _Params.w, s01)))) * lerp(.3, 1, s01);
- }
- float2 uv = i.uv + half2(_DistortionX, _DistortionY) * offset * _DistortionMult;
- half2 luv = (uv-_SpriteUV.zw) / _SpriteUV.xy;
- if(_ClipToSprite>0)
- clip( min(min(luv.y,(1-luv.y)),min(luv.x,(1-luv.x))) );
- fixed4 main = tex2D(_MainTex, uv);
- clip(main.a-_AlphaClip);
- half offbri = max( abs(offset.x) , abs(offset.y)) * _OffsetBrightness;
- return i.color * main * _TintColor * (exp((_EmissionGain + offbri) * 5));
- }
- ENDCG
- }
- }
- }
- }
- // c# companion script
- // SpriteUVToShader.cs -------------------------------------------------------------------------------------------------------------------------------- //
- // Save you your project, add to your SpriteRenderer gameObject
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- [ExecuteInEditMode]
- [RequireComponent(typeof(SpriteRenderer))]
- public class SpriteUVToShader : MonoBehaviour {
- public string UV_property="_SpriteUV";
- public string Pivot_property="_SpritePivot";
- SpriteRenderer sr;
- Sprite sprite;
- MaterialPropertyBlock mpb;
- void OnValidate()
- {
- update();
- }
- void OnWillRenderObject(){
- update();
- }
- void Start(){
- update();
- }
- void update(){
- if(sr==null)
- sr = GetComponent<SpriteRenderer>();
- if(sprite != sr.sprite){
- sprite = sr.sprite;
- applySprite(sr, sprite, ref mpb, UV_property, Pivot_property);
- }
- }
- public static void applySprite(Renderer renderer, Sprite toSprite, ref MaterialPropertyBlock mpb,
- string uvProp=null, string pivotProp=null){
- if(toSprite==null) return;
- var scale = new Vector2(
- toSprite.textureRect.width/ toSprite.texture.width,
- toSprite.textureRect.height/toSprite.texture.height);
- var offset = new Vector2(
- toSprite.rect.x/toSprite.texture.width,
- toSprite.rect.y/toSprite.texture.height);
- Vector4 uvVector = new Vector4(scale.x,scale.y,offset.x,offset.y);
- Vector4 pivotVector = new Vector4(toSprite.pivot.x/toSprite.rect.width,toSprite.pivot.y/toSprite.rect.height);
- if(string.IsNullOrEmpty(uvProp))
- uvProp = "_MainTex_ST";
- if(mpb==null)
- mpb = new MaterialPropertyBlock();
- renderer.GetPropertyBlock(mpb);
- mpb.SetVector(uvProp, uvVector);
- if(!string.IsNullOrEmpty(pivotProp))
- mpb.SetVector(pivotProp, pivotVector);
- renderer.SetPropertyBlock(mpb);
- }
- }
Add Comment
Please, Sign In to add comment