Advertisement
Guest User

shader with world-space air dust for light shafts etc

a guest
Jan 21st, 2017
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/AdditiveScreenTexture"{
  2. Properties {
  3.     _TintColor ("Tint Color", Color) = (1,1,1,1)
  4.     _EmissionGain ("Emission Gain", Range(0, 1)) = 0.1
  5.     _Mul ("Cloudy", Range (0, 4)) = 1
  6.     _Params ("Size (Min Max) Wind (Min Max)", Vector) = (32, 48, 1, .5)
  7.     _ParamsB ("Windangle (Min Max)", Vector) = (4.05, 5.77, 0, 0)
  8.     _Wind ("Wind speed", Range (0, 2)) = 0.18
  9.     _Rotation ("Rotation", Range (0, 1)) = 0
  10.     _Samples ("Samples", Range (1, 10)) = 2
  11.     _MainTex ("Texture", 2D) = "white" {}
  12.    
  13. }
  14.  
  15. Category {
  16.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
  17.     Blend SrcAlpha One
  18.     ZWrite Off
  19.  
  20.     SubShader {
  21.         Pass {
  22.        
  23.             CGPROGRAM
  24. // Upgrade NOTE: excluded shader from DX11; has structs without semantics (struct v2f members worldPos)
  25. #pragma exclude_renderers d3d11
  26.             #pragma vertex vert
  27.             #pragma fragment frag
  28.  
  29.             fixed4 _TintColor;
  30.             float _EmissionGain;
  31.             half _Mul;
  32.             half _Rotation;
  33.             half _Wind;
  34.             half4 _Params;
  35.             half4 _ParamsB;
  36.             int _Samples;
  37.             sampler2D _MainTex;
  38.             float4 _MainTex_ST;
  39.  
  40.             struct appdata_t {
  41.                 float4 vertex : POSITION;
  42.                 fixed4 color : COLOR;
  43.             };
  44.  
  45.             struct v2f {
  46.                 float4 vertex : SV_POSITION;
  47.                 fixed4 color : COLOR;
  48.                 float2 worldPosition: TEXCOORD0;
  49.             };
  50.  
  51.  
  52.             v2f vert (appdata_t v)
  53.             {
  54.                 v2f o;
  55.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  56.                 o.color = v.color ;
  57.                 o.worldPosition = mul(unity_ObjectToWorld, v.vertex);
  58.                 return o;
  59.             }
  60.  
  61.             float pingpong(float value, float freq){
  62.                 return sin( (value*3.1415926536) * freq);
  63.             }
  64.  
  65.             float semiRandom(float2 uv){
  66.                 return frac(sin(dot(uv, float2(12.9898, 78.233))) * 43758.5453);
  67.             }
  68.  
  69.             fixed noise(v2f i, float size, float2 speed){
  70.                 float2 uv = speed * _Time.x + i.worldPosition.xy * size;
  71.                 return tex2D(_MainTex, uv).x;
  72.             }
  73.  
  74.             // rotated 45 degrees              
  75.             fixed noise45(v2f i, float size, float2 speed){
  76.                 float2 uv = speed * _Time.x + i.worldPosition.xy * size;
  77.  
  78.                 float y = uv.y * 0.7071067812;
  79.                 float x = uv.x * 0.7071067812;
  80.                 uv = half2(x+y,-x+y);
  81.  
  82.                 return tex2D(_MainTex, uv).x;
  83.             }
  84.  
  85.             float2 rotate(float2 original, float angle ){
  86.                 float sinX = sin ( angle );
  87.                 float cosX = cos ( angle );
  88.                 float sinY = sin ( angle );
  89.                 float2x2 rotationMatrix = float2x2( cosX, -sinX, sinY, cosX);
  90.                 return mul(original, rotationMatrix );
  91.             }
  92.  
  93.             fixed4 frag (v2f i) : SV_Target
  94.             {
  95.                 float clouds = 1;
  96.                
  97.                 for(int s=0;s<_Samples;s++){
  98.                     float s01 = s/(float)(_Samples-1);
  99.                     float2 direction = rotate(float2(_Wind,0), lerp(_ParamsB.x, _ParamsB.y, s01)  );
  100.                     clouds *= .3+( noise45(i, 1/lerp(_Params.x, _Params.y, s01), direction * (lerp(_Params.z, _Params.w, s01))) + .5);
  101.                 }
  102.  
  103.                 float4 shaftColor = i.color*i.color;
  104.  
  105.                 return lerp(shaftColor, shaftColor*clouds, _Mul) * _TintColor * (exp(_EmissionGain * 5));
  106.             }
  107.            
  108.             ENDCG
  109.         }
  110.     }  
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement