Advertisement
Guest User

ParticleFire.shader

a guest
May 26th, 2019
2,408
1
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(-2,2)) = 0.4
  18.         _Particle("Density", Range(-2,2)) = 0
  19.         [Toggle(MULTIPLY)] _MULTIPLY("Multiply Noise?", Float) = 1
  20.         [Enum(UnityEngine.Rendering.BlendOp)] _BlendOp("Blend Op", Int) = 0// 0 = add, 4 = max, other ones probably won't look good
  21.        
  22.     }
  23.     SubShader
  24.     {
  25.         Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "PreviewType" = "Plane" }
  26.     Blend One OneMinusSrcAlpha
  27.     ColorMask RGB
  28.     Cull Off Lighting Off ZWrite Off
  29.     BlendOp [_BlendOp]
  30.        
  31.         Pass
  32.         {
  33.             CGPROGRAM
  34.             #pragma vertex vert
  35.             #pragma fragment frag
  36.             // make fog work
  37.             #pragma multi_compile_fog
  38.             #pragma shader_feature MULTIPLY
  39.  
  40.             #include "UnityCG.cginc"
  41.  
  42.             struct appdata
  43.             {
  44.                 float4 vertex : POSITION;
  45.                 float3 uv : TEXCOORD0;// .z has particle age
  46.                 float4 color : COLOR;
  47.                 float4 normal :NORMAL;
  48.             };
  49.  
  50.             struct v2f
  51.             {
  52.                 float3 uv : TEXCOORD0; // .z has particle age
  53.                 UNITY_FOG_COORDS(1)
  54.                 float4 vertex : SV_POSITION;
  55.                 float4 color: COLOR;
  56.                 float3 worldPos : TEXCOORD2;
  57.                 float3 worldNormal : TEXCOORD3;
  58.  
  59.             };
  60.  
  61.             sampler2D _MainTex,_Noise, _Distort;
  62.             float4 _MainTex_ST, _Noise_ST, _Tint, _EdgeColor;
  63.             float _Scale, _DistortScale, _Cutoff, _Speed, _Brightness, _Stretch, _EdgeWidth, _Particle;
  64.             v2f vert (appdata v)
  65.             {
  66.                 v2f o;
  67.                 o.worldNormal = mul(unity_ObjectToWorld,v.normal);
  68.                 o.vertex = UnityObjectToClipPos(v.vertex);
  69.                 o.uv.xy = TRANSFORM_TEX(v.uv.xy, _MainTex);
  70.                 o.worldPos = mul (unity_ObjectToWorld, v.vertex);
  71.                 UNITY_TRANSFER_FOG(o,o.vertex);
  72.                 o.uv.z = v.uv.z;
  73.                 o.color = v.color;
  74.                 return o;
  75.             }
  76.  
  77.             fixed4 frag (v2f i) : SV_Target
  78.             {
  79.                 // sample the texture
  80.  
  81.                 float3 blendNormal = saturate(pow(i.worldNormal * 1.4,4));
  82.  
  83.                 float flowspeed = _Time.y * _Speed;
  84.  
  85.                 i.worldPos.y -= flowspeed;
  86.                 i.worldPos.y *= _Stretch;
  87.  
  88.                 // normal distort triplanar for x, y, z sides
  89.                 float xd = tex2D(_Distort, i.worldPos.zy * _DistortScale);
  90.                 float zd = tex2D(_Distort, i.worldPos.xy * _DistortScale);
  91.  
  92.                 // lerped together all sides for distort texture
  93.                 float distorttexture = zd;
  94.                 distorttexture = lerp(distorttexture, xd, blendNormal.x);
  95.  
  96.  
  97.                 // normal noise triplanar for x, y, z sides
  98.                 float xn = tex2D(_Noise, (i.worldPos.zy * _Scale));
  99.                 float zn = tex2D(_Noise, (i.worldPos.xy  * _Scale) );
  100.  
  101.                 // lerped together all sides for noise texture
  102.                 float noisetexture = zn;
  103.                 noisetexture = lerp(noisetexture, xn, blendNormal.x);
  104.  
  105.                 float particleAgePercent = i.uv.z;
  106.                 particleAgePercent -= _Particle;
  107.    
  108.                 float finalNoise;
  109. #if MULTIPLY
  110.                 finalNoise = noisetexture * distorttexture * 2;
  111. #else
  112.                 finalNoise = (noisetexture + distorttexture)*2 ;
  113. #endif
  114.  
  115.                 // particle shape
  116.                 float4 shape = tex2D(_MainTex, i.uv);
  117.  
  118.                
  119.                
  120.                 float4 result = smoothstep(particleAgePercent - _Cutoff,particleAgePercent, finalNoise* shape.a); // step through the noise * shape based on the particle age
  121.                 float edge = result * step(finalNoise * shape.a, particleAgePercent + _EdgeWidth) * shape.a;// edge multiplied by particle shape  alpha
  122.                 result -= edge;// remove the edge so it can be colored seperately
  123.                 result *= shape.a;// use particle texture
  124.                 result += (result * _Brightness);// extra brightness
  125.  
  126.                 result *=  _Tint;// tint
  127.                 result *= i.color.a; // particle color alpha
  128.                 result += (edge * _EdgeColor);// add colored edge
  129.                 result *= i.color;// particle color (over lifetime)
  130.                 // apply fog
  131.                 UNITY_APPLY_FOG(i.fogCoord, result);
  132.                 return result;
  133.  
  134.             }
  135.             ENDCG
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement