Advertisement
Guest User

LavaUnlit.shader

a guest
Jan 17th, 2020
1,958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Unlit/SimpleLava"
  2. {
  3.     Properties
  4.     {
  5.         [Header(Main)]
  6.         _Color("Main Tint Start", Color) = (0.5, 0.1,0.1, 1)
  7.         _Color2("Main Tint End", Color) = (0.6, 0.4,0.1, 1)
  8.         _Offset("Start/End Tint Offset", Range(0,10)) = 1
  9.         _MainTex("Main Texture", 2D) = "white" {}
  10.         _Color3("Top Layer Tint", Color) = (1, 1,0, 1)
  11.         _Scale("Scale Main", Range(0,1)) = 0.3
  12.         _SpeedMainX("Speed Main X", Range(-10,10)) = 0.4
  13.         _SpeedMainY("Speed Main Y", Range(-10,10)) = 0.4       
  14.         _Strength("Brightness Under Lava", Range(0,10)) = 2
  15.         _StrengthTop("Brightness Top Lava", Range(0,10)) = 3
  16.         _Cutoff("Cutoff Top", Range(0,1)) = 0.9
  17.         _TopBlur("Top Blur", Range(0,1)) = 0.1
  18.  
  19.         [Space(10)]
  20.         [Header(Edge)]
  21.         _EdgeC("Edge Color", Color) = (1, 0.5, 0.2, 1)
  22.         _EdgeBlur("Edge Blur", Range(0,1)) = 0.5
  23.         _Edge("Edge Thickness", Range(0,20)) = 8
  24.  
  25.         [Space(10)]
  26.         [Header(Distortion)]
  27.         _DistortTex("Distort Texture", 2D) = "white" {}
  28.         _ScaleDist("Scale Distortion", Range(0,1)) = 0.5
  29.         _SpeedDistortX("Speed Distort X", Range(-10,10)) = 0.2
  30.         _SpeedDistortY("Speed Distort Y", Range(-10,10)) = 0.2
  31.         _Distortion("Distort Strength", Range(0,1)) = 0.2
  32.         _VertexDistortion("Extra Vertex Color Distortion", Range(0,1)) = 0.3
  33.        
  34.         [Space(10)]
  35.         [Header(Vertex Movement)]
  36.         _Speed("Wave Speed", Range(0,1)) = 0.5
  37.         _Amount("Wave Amount", Range(0,1)) = 0.6
  38.         _Height("Wave Height", Range(0,1)) = 0.1
  39.        
  40.     }
  41.         SubShader
  42.     {
  43.         Tags{ "RenderType" = "Opaque"  "Queue" = "Transparent" }
  44.         LOD 100
  45.  
  46.         Pass
  47.     {
  48.         CGPROGRAM
  49. #pragma vertex vert
  50. #pragma fragment frag
  51.         // make fog work
  52. #pragma multi_compile_fog
  53. #include "UnityCG.cginc"
  54.  
  55.         struct appdata
  56.     {
  57.         float4 vertex : POSITION;
  58.         float2 uv : TEXCOORD0;
  59.         float4 color: COLOR;
  60.     };
  61.  
  62.     struct v2f
  63.     {
  64.         float2 uv : TEXCOORD3;
  65.         UNITY_FOG_COORDS(1)
  66.         float4 vertex : SV_POSITION;
  67.         float4 scrPos : TEXCOORD2;//
  68.         float4 worldPos : TEXCOORD4;//
  69.         float4 color :COLOR;
  70.     };
  71.  
  72.     sampler2D _CameraDepthTexture;
  73.  
  74.     float4 _Color, _Color2, _Color3, _EdgeC;
  75.     sampler2D _MainTex, _DistortTex;
  76.     float _Speed, _Amount, _Height, _Edge, _Scale, _ScaleDist;
  77.     float  _Offset, _Strength, _Distortion, _Cutoff, _StrengthTop;
  78.     float _EdgeBlur, _VertexDistortion;
  79.     float _SpeedDistortX, _SpeedDistortY;
  80.     float _SpeedMainX, _SpeedMainY;
  81.     float _TopBlur;
  82.  
  83.     v2f vert(appdata v)
  84.     {
  85.         v2f o;
  86.         UNITY_INITIALIZE_OUTPUT(v2f, o);
  87.        
  88.         //wave movement multiplied by vertex color
  89.         v.vertex.y += (sin(_Time.z * _Speed + (v.vertex.x * v.vertex.z * _Amount)) * _Height) *v.color.r;
  90.  
  91.         o.vertex = UnityObjectToClipPos(v.vertex);
  92.  
  93.         // world position for textures
  94.         o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  95.  
  96.         // vertex colors
  97.         o.color = v.color;
  98.  
  99.         // screen position for depth
  100.         o.scrPos = ComputeScreenPos(o.vertex);
  101.  
  102.         UNITY_TRANSFER_FOG(o,o.vertex);
  103.         return o;
  104.     }
  105.  
  106.     fixed4 frag(v2f i) : SV_Target
  107.     {
  108.        
  109.         // uv distortion scaled
  110.         float2 uvDistort = i.worldPos.xz * _ScaleDist;
  111.         // moving over time
  112.         float speedDistortX = _Time.x * _SpeedDistortX;
  113.         float speedDistortY = _Time.x * _SpeedDistortY;
  114.         float2 speedDistortCombined = float2(speedDistortX, speedDistortY);
  115.  
  116.         // distortion textures at different scales
  117.         float d = tex2D(_DistortTex, uvDistort + speedDistortCombined).r;
  118.         float d2 = tex2D(_DistortTex, (i.worldPos.xz * (_ScaleDist * 0.5)) + speedDistortCombined).r;
  119.         // combined
  120.         float layereddist = saturate((d + d2)*0.5 ) ;
  121.        
  122.         // uv main scaled
  123.         float2 uvMain = i.worldPos.xz * _Scale;
  124.         // plus distortion
  125.         uvMain += layereddist * _Distortion;
  126.  
  127.         // moving over time
  128.         float speedMainX = _Time.x * _SpeedMainX;
  129.         float speedMainY = _Time.x * _SpeedMainY;
  130.         float2 speedMainCombined = float2(speedMainX, speedMainY);
  131.  
  132.         // main uv moving with extra distortion based on vertex colors
  133.         uvMain += speedMainCombined +(i.color.r * _VertexDistortion);
  134.  
  135.         // main texture fading with vertex color
  136.         half4 col = tex2D(_MainTex, uvMain)*i.color.r;
  137.  
  138.         // add layered distortion
  139.         col += layereddist;
  140.    
  141.         // top layer
  142.         float top = smoothstep(_Cutoff, _Cutoff + _TopBlur, col) * i.color.r;
  143.    
  144.         // depth edge detection
  145.         half depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos)));
  146.         half4 edgeLine = 1 - saturate(_Edge* (depth - i.scrPos.w));
  147.  
  148.         // cutoff edge based on main texture
  149.         float edge = smoothstep(1- col , 1- col+ _EdgeBlur, edgeLine);
  150.        
  151.         // lerp start and end color over main texture, multiply for brightness
  152.         float4 color = lerp(_Color, _Color2, col * _Offset) * _Strength;
  153.        
  154.         // take edge out of main color
  155.         color *= (1 - edge);
  156.         // take the top out of main color
  157.         color *= (1 - top);
  158.         // fade using the vertex color
  159.         color *= i.color.r;
  160.         // add edge back in colored, multiply for brightness
  161.         color += (edge *_EdgeC) * _StrengthTop;
  162.  
  163.         // add top back in colored, multiply for brightness
  164.         color += top * _Color3 *_StrengthTop;
  165.  
  166.         return color;
  167.         }
  168.         ENDCG
  169.     }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement