Guest User

VertexColorAnim.shader

a guest
Oct 18th, 2021
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Shader "Particles/VertexColorAnim" {
  3.     Properties {
  4.         [Header(Base)]
  5.         _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
  6.         _MainTex ("Particle Texture", 2D) = "white" {}
  7.         _Alpha ("Alpha", Range(0,1)) =1
  8.  
  9.         [Space]
  10.         [Header(Rim)]
  11.         [Toggle(RIM)] _RIM("Rim Light", Float) = 1
  12.         _RimColor ("Rim Color", Color) = (1,0,0,0.5)
  13.         _RimPower("Rimpower", Range(0,10)) = 2
  14.  
  15.         [Space]
  16.         [Header(Animation)]      
  17.         _Rigidness("Rigidness", Range(0.01,10)) = 0.5
  18.         _Speed("Speed", Range(0,10)) = 0.5
  19.         _Length("Length", Range(0,10)) = 2
  20.  
  21.         [Space]
  22.         [Header(Rotation Fix)]
  23.         _Rotation("Rotation", Range(0,360.0)) = 0
  24.         _Axis("Axis", Vector) = (0,1,0)
  25.        
  26.     }
  27.    
  28.     Category {
  29.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"   }
  30.         Blend One OneMinusSrcAlpha
  31.         ZWrite On  
  32.        
  33.         CGINCLUDE
  34.        
  35.         sampler2D _MainTex;
  36.         fixed4 _TintColor,_RimColor;
  37.        
  38.         struct appdata_t {
  39.             float4 vertex : POSITION;
  40.             fixed4 color : COLOR;
  41.             float4 texcoord : TEXCOORD0;
  42.             float4 velocity : TEXCOORD1;
  43.             float4 uv : TEXCOORD2;
  44.             float3 normal : NORMAL;
  45.             float3 viewDir : TEXCOORD4;
  46.             float4 rotation : TEXCOORD3;
  47.         };
  48.        
  49.         float4 _MainTex_ST;
  50.         float _Rotation;
  51.         float _Speed, _Rigidness,_Length;
  52.         float _RimPower;
  53.         float _Alpha;
  54.         float3 _Axis;
  55.        
  56.         float3 Unity_RotateAboutAxis_Degrees_float(float3 In, float3 Axis, float Rotation)
  57.         {
  58.             Rotation = radians(Rotation);
  59.             float s = sin(Rotation);
  60.             float c = cos(Rotation);
  61.             float one_minus_c = 1.0 - c;
  62.            
  63.             Axis = normalize(Axis);
  64.             float3x3 rot_mat =
  65.             {   one_minus_c * Axis.x * Axis.x + c, one_minus_c * Axis.x * Axis.y - Axis.z * s, one_minus_c * Axis.z * Axis.x + Axis.y * s,
  66.                 one_minus_c * Axis.x * Axis.y + Axis.z * s, one_minus_c * Axis.y * Axis.y + c, one_minus_c * Axis.y * Axis.z - Axis.x * s,
  67.                 one_minus_c * Axis.z * Axis.x - Axis.y * s, one_minus_c * Axis.y * Axis.z + Axis.x * s, one_minus_c * Axis.z * Axis.z + c
  68.             };
  69.             return mul(rot_mat,  In);
  70.         }
  71.         ENDCG
  72.        
  73.         SubShader {
  74.             Pass {
  75.                
  76.                 CGPROGRAM
  77.                 #pragma vertex vert
  78.                 #pragma fragment frag
  79.                 #pragma target 2.0
  80.                 #pragma multi_compile_particles
  81.                 #pragma shader_feature RIM
  82.                 #include "UnityCG.cginc"
  83.                
  84.                 struct v2f {
  85.                     float4 vertex : POSITION;                  
  86.                     float2 texcoord : TEXCOORD2;
  87.                     float3 normal : NORMAL;
  88.                     float3 viewDir : TEXCOORD4;                
  89.                 };
  90.                
  91.                
  92.                 v2f vert (appdata_t v)
  93.                 {
  94.                     // particle scale
  95.                     float scale = v.texcoord.w;
  96.  
  97.                     //randomisation
  98.                     float random =  v.uv.w;
  99.  
  100.                     float4 pos = v.vertex;
  101.                     // center
  102.                     float3 pivot = v.texcoord.xyz;
  103.  
  104.                     //Move to root
  105.                     pos.xyz -= pivot;
  106.                     //Rotate on velocity
  107.                     float3 crossVel = cross(normalize(v.velocity),_Axis);
  108.                     pos.xyz = Unity_RotateAboutAxis_Degrees_float(pos.xyz,crossVel, _Rotation);
  109.  
  110.                     // add sine wave
  111.                     float y = sin( _Rigidness + (_Time.y * (_Speed + random)))*( _Length * scale);// z axis movements
  112.                     float x = sin( pos.z  / _Rigidness + (_Time.y * (_Speed + random )))*( _Length * scale);// z axis movements
  113.                     float y2 = sin( 2/ _Rigidness + ((_Time.y)  * (_Speed+ random)))*( _Length * scale);// z axis movements
  114.  
  115.                     // move vertices              
  116.                     pos.y += y * v.color.r;
  117.                     pos.y += y2 * v.color.g;
  118.                     pos.x += x * v.color.b;
  119.  
  120.                     //Rotate back
  121.                     pos.xyz = Unity_RotateAboutAxis_Degrees_float(pos.xyz,crossVel,- _Rotation);
  122.                     //Move it back
  123.                     pos.xyz += pivot;
  124.                    
  125.                     v2f o;
  126.                     o.vertex = UnityObjectToClipPos(pos);                
  127.                     o.texcoord = v.uv.yz;
  128.                     o.normal = v.normal;
  129.                     o.viewDir = ObjSpaceViewDir(pos);
  130.                     return o;
  131.                 }
  132.                
  133.                
  134.                 fixed4 frag (v2f i) : SV_Target
  135.                 {
  136.                     float rim = 1.0 - saturate(dot (normalize(i.viewDir), i.normal));
  137.                     float4 tex = tex2D(_MainTex, i.texcoord);
  138.                     #if RIM
  139.                         float  fresnel =  pow(rim, _RimPower);
  140.                         float4 result = lerp(tex * _TintColor,_RimColor, fresnel);
  141.                     #else
  142.                         float4 result = tex * _TintColor;
  143.                     #endif
  144.                     return float4(result.rgb, _Alpha);
  145.                 }
  146.                 ENDCG
  147.             }
  148.  
  149.             Pass
  150.             {
  151.                 Tags {"LightMode"="ShadowCaster"}
  152.  
  153.                 CGPROGRAM
  154.                 #pragma vertex vert
  155.                 #pragma fragment frag
  156.                 #pragma multi_compile_shadowcaster
  157.                 #include "UnityCG.cginc"
  158.  
  159.                 struct v2f {
  160.                     V2F_SHADOW_CASTER;
  161.                 };
  162.  
  163.                
  164.  
  165.                 v2f vert(appdata_t v)
  166.                 {
  167.                     // particle scale
  168.                     float scale = v.texcoord.w;
  169.  
  170.                     //randomisation
  171.                     float random =  v.uv.w;
  172.  
  173.                     float4 pos = v.vertex;
  174.                     // center
  175.                     float3 pivot = v.texcoord.xyz;
  176.  
  177.                     //Move to root
  178.                     pos.xyz -= pivot;
  179.                     //Rotate on velocity
  180.                     float3 crossVel = cross(normalize(v.velocity),_Axis);
  181.                     pos.xyz = Unity_RotateAboutAxis_Degrees_float(pos.xyz,crossVel, _Rotation);
  182.  
  183.                     // add sine wave
  184.                     float y = sin( _Rigidness + (_Time.y * (_Speed + random)))*( _Length * scale);// z axis movements
  185.                     float x = sin( pos.z  / _Rigidness + (_Time.y * (_Speed + random )))*( _Length * scale);// z axis movements
  186.                     float y2 = sin( 2/ _Rigidness + ((_Time.y)  * (_Speed+ random)))*( _Length * scale);// z axis movements
  187.                    
  188.                     // move vertices              
  189.                     pos.y += y * v.color.r;
  190.                     pos.y += y2 * v.color.g;
  191.                     pos.x += x * v.color.b;
  192.  
  193.                     //Rotate back
  194.                     pos.xyz = Unity_RotateAboutAxis_Degrees_float(pos.xyz,crossVel,- _Rotation);
  195.                     //Move it back
  196.                     pos.xyz += pivot;
  197.  
  198.                     v.vertex.xyz = pos.xyz;
  199.                     v2f o;
  200.                     TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
  201.                     return o;
  202.                 }
  203.  
  204.                 float4 frag(v2f i) : SV_Target
  205.                 {
  206.                     SHADOW_CASTER_FRAGMENT(i)
  207.                 }
  208.                 ENDCG
  209.             }
  210.         }
  211.        
  212.     }
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment