Advertisement
Guest User

ParticleFire.shader

a guest
May 24th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Shader "Particles/Fire"
  3. {
  4.     Properties
  5.     {
  6.         _MainTex("Texture", 2D) = "white" {}
  7.         _Noise("Noise Texture", 2D) = "white" {}
  8.         _Distort("Second Noise Texture", 2D) = "white" {}
  9.         _Scale("Noise Scale", Range(0,2)) = 0.5
  10.         _DistortScale("Distort Scale", Range(0,2)) = 0.5
  11.         _Tint("Tint", Color) = (1,1,0,0) // Color of the dissolve Line
  12.         _EdgeColor("Edge", Color) = (1,0.5,0,0) // Color of the dissolve Line)
  13.         _Cutoff("Cutoff Smoothness", Range(0,1)) = 0.2
  14.         _Speed("Speed", Range(-10,10)) = 2
  15.         _Brightness("Brightness", Range(0,2)) = 0.6
  16.         _Stretch("Stretch", Range(0,2)) = 1
  17.         _EdgeWidth("EdgeWidth", Range(0,2)) = 0.4
  18.         _Particle("Density", Range(-2,2)) = 0
  19.         [Toggle(MULTIPLY)] _MULTIPLY("Multiply Noise?", Float) = 1
  20.  
  21.     }
  22.         SubShader
  23.         {
  24.             Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "PreviewType" = "Plane" }
  25.         Blend One OneMinusSrcAlpha
  26.         ColorMask RGB
  27.          Lighting Off ZWrite Off
  28.  
  29.             Pass
  30.             {
  31.                 CGPROGRAM
  32.                 #pragma vertex vert
  33.                 #pragma fragment frag
  34.                 // make fog work
  35.                 #pragma multi_compile_fog
  36.                 #pragma shader_feature MULTIPLY
  37.  
  38.                 #include "UnityCG.cginc"
  39.  
  40.                 struct appdata
  41.                 {
  42.                     float4 vertex : POSITION;
  43.                     float3 uv : TEXCOORD0;// .z has particle age
  44.                     float4 color : COLOR;
  45.                     float4 normal :NORMAL;
  46.                 };
  47.  
  48.                 struct v2f
  49.                 {
  50.                     float3 uv : TEXCOORD0; // .z has particle age
  51.                     UNITY_FOG_COORDS(1)
  52.                     float4 vertex : SV_POSITION;
  53.                     float4 color: COLOR;
  54.                     float3 worldPos : TEXCOORD2;
  55.                     float3 worldNormal : TEXCOORD3;
  56.  
  57.                 };
  58.  
  59.                 sampler2D _MainTex,_Noise, _Distort;
  60.                 float4 _MainTex_ST, _Noise_ST, _Tint, _EdgeColor;
  61.                 float _Scale, _DistortScale, _Cutoff, _Speed, _Brightness, _Stretch, _EdgeWidth, _Particle;
  62.                 v2f vert(appdata v)
  63.                 {
  64.                     v2f o;
  65.                     o.worldNormal = mul(unity_ObjectToWorld,v.normal);
  66.                     o.vertex = UnityObjectToClipPos(v.vertex);
  67.                     o.uv.xy = TRANSFORM_TEX(v.uv.xy, _MainTex);
  68.                     o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  69.                     UNITY_TRANSFER_FOG(o,o.vertex);
  70.                     o.uv.z = v.uv.z;
  71.                     o.color = v.color;
  72.                     return o;
  73.                 }
  74.  
  75.                 fixed4 frag(v2f i) : SV_Target
  76.                 {
  77.                     // sample the texture
  78.  
  79.                     float3 blendNormal = saturate(pow(i.worldNormal * 1.4,4));
  80.  
  81.                     float flowspeed = _Time.y * _Speed;
  82.  
  83.                     i.worldPos.y -= flowspeed;
  84.                     i.worldPos.y *= _Stretch;
  85.  
  86.                     // normal distort triplanar for x, y, z sides
  87.                     float3 xd = tex2D(_Distort, i.worldPos.zy * _DistortScale);
  88.                     float3 zd = tex2D(_Distort, i.worldPos.xy * _DistortScale);
  89.  
  90.                     // lerped together all sides for distort texture
  91.                     float3 distorttexture = zd;
  92.                     distorttexture = lerp(distorttexture, xd, blendNormal.x);
  93.  
  94.  
  95.                     // normal noise triplanar for x, y, z sides
  96.                     float3 xn = tex2D(_Noise, (i.worldPos.zy * _Scale));
  97.                     float3 zn = tex2D(_Noise, (i.worldPos.xy  * _Scale));
  98.  
  99.                     // lerped together all sides for noise texture
  100.                     float3 noisetexture = zn;
  101.                     noisetexture = lerp(noisetexture, xn, blendNormal.x);
  102.  
  103.                     float particleAgePercent = i.uv.z;
  104.                     particleAgePercent -= _Particle;
  105.  
  106.                     float3 finalNoise;
  107.     #if MULTIPLY
  108.                     finalNoise = noisetexture * distorttexture * 2;
  109.     #else
  110.                     finalNoise = (noisetexture + distorttexture) * 2;
  111.     #endif
  112.                     // particle shape
  113.                     float4 col = tex2D(_MainTex, i.uv);
  114.  
  115.                     float4 result = smoothstep(particleAgePercent - _Cutoff,particleAgePercent, finalNoise.r* col.a);
  116.                     float edge = result * step(finalNoise.r * col.a, particleAgePercent + _EdgeWidth) * col.a;// edge multiplied by particle shape  alpha
  117.  
  118.                     result *= col.a * result;// use particle texture
  119.                     result += (result * _Brightness);// extra brightness
  120.  
  121.                     result *= _Tint;// tint
  122.                     result *= i.color.a; // particle alpha
  123.                     result += (edge * _EdgeColor);// add colored edge
  124.                     // apply fog
  125.                     UNITY_APPLY_FOG(i.fogCoord, result);
  126.                     return result;
  127.  
  128.                 }
  129.                 ENDCG
  130.             }
  131.         }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement