Advertisement
Finnthahuman

Dynamic Snow

Jan 31st, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Dynamic_Snow" {
  2.     Properties {
  3.         _SnowTexture ("SnowTexture", 2D) = "white" {}
  4.         _SnowAmount ("Snow Amount", Range(0, 3)) = 0
  5.         _MainTexture ("Main Texture", 2D) = "white" {}
  6.         _NormalMap ("Normal Map", 2D) = "black" {}
  7.     }
  8.     SubShader {
  9.         Tags {
  10.             "Queue"="Geometry+1"
  11.             "RenderType"="Opaque"
  12.         }
  13.         Pass {
  14.             Name "FORWARD"
  15.             Tags {
  16.                 "LightMode"="ForwardBase"
  17.             }
  18.             ZTest Always
  19.            
  20.            
  21.             CGPROGRAM
  22.             #pragma vertex vert
  23.             #pragma fragment frag
  24.             #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
  25.             #define _GLOSSYENV 1
  26.             #include "UnityCG.cginc"
  27.             #include "AutoLight.cginc"
  28.             #include "Lighting.cginc"
  29.             #include "UnityPBSLighting.cginc"
  30.             #include "UnityStandardBRDF.cginc"
  31.             #pragma multi_compile_fwdbase_fullshadows
  32.             #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
  33.             #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
  34.             #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
  35.             #pragma only_renderers d3d9 d3d11 glcore gles
  36.             #pragma target 3.0
  37.             uniform sampler2D _SnowTexture; uniform float4 _SnowTexture_ST;
  38.             uniform float _SnowAmount;
  39.             uniform sampler2D _MainTexture; uniform float4 _MainTexture_ST;
  40.             uniform sampler2D _NormalMap; uniform float4 _NormalMap_ST;
  41.             struct VertexInput {
  42.                 float4 vertex : POSITION;
  43.                 float3 normal : NORMAL;
  44.                 float4 tangent : TANGENT;
  45.                 float2 texcoord0 : TEXCOORD0;
  46.                 float2 texcoord1 : TEXCOORD1;
  47.                 float2 texcoord2 : TEXCOORD2;
  48.             };
  49.             struct VertexOutput {
  50.                 float4 pos : SV_POSITION;
  51.                 float2 uv0 : TEXCOORD0;
  52.                 float2 uv1 : TEXCOORD1;
  53.                 float2 uv2 : TEXCOORD2;
  54.                 float4 posWorld : TEXCOORD3;
  55.                 float3 normalDir : TEXCOORD4;
  56.                 float3 tangentDir : TEXCOORD5;
  57.                 float3 bitangentDir : TEXCOORD6;
  58.                 LIGHTING_COORDS(7,8)
  59.                 #if defined(LIGHTMAP_ON) || defined(UNITY_SHOULD_SAMPLE_SH)
  60.                     float4 ambientOrLightmapUV : TEXCOORD9;
  61.                 #endif
  62.             };
  63.             VertexOutput vert (VertexInput v) {
  64.                 VertexOutput o = (VertexOutput)0;
  65.                 o.uv0 = v.texcoord0;
  66.                 o.uv1 = v.texcoord1;
  67.                 o.uv2 = v.texcoord2;
  68.                 #ifdef LIGHTMAP_ON
  69.                     o.ambientOrLightmapUV.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  70.                     o.ambientOrLightmapUV.zw = 0;
  71.                 #elif UNITY_SHOULD_SAMPLE_SH
  72.                 #endif
  73.                 #ifdef DYNAMICLIGHTMAP_ON
  74.                     o.ambientOrLightmapUV.zw = v.texcoord2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  75.                 #endif
  76.                 o.normalDir = UnityObjectToWorldNormal(v.normal);
  77.                 o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );
  78.                 o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
  79.                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
  80.                 float3 lightColor = _LightColor0.rgb;
  81.                 o.pos = UnityObjectToClipPos( v.vertex );
  82.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
  83.                 return o;
  84.             }
  85.             float4 frag(VertexOutput i) : COLOR {
  86.                 i.normalDir = normalize(i.normalDir);
  87.                 float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
  88.                 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  89.                 float4 _NormalMap_var = tex2D(_NormalMap,TRANSFORM_TEX(i.uv0, _NormalMap));
  90.                 float3 normalLocal = _NormalMap_var.rgb;
  91.                 float3 normalDirection = normalize(mul( normalLocal, tangentTransform ));
  92.                 float3 viewReflectDirection = reflect( -viewDirection, normalDirection );
  93.                 float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
  94.                 float3 lightColor = _LightColor0.rgb;
  95.                 float3 halfDirection = normalize(viewDirection+lightDirection);
  96.  
  97.                 UNITY_LIGHT_ATTENUATION(attenuation,i, i.posWorld.xyz);
  98.                 float3 attenColor = attenuation * _LightColor0.xyz;
  99.                 float Pi = 3.141592654;
  100.                 float InvPi = 0.31830988618;
  101.  
  102.                 float node_9886 = 0.0;
  103.                 float gloss = node_9886;
  104.                 float perceptualRoughness = 1.0 - node_9886;
  105.                 float roughness = perceptualRoughness * perceptualRoughness;
  106.                 float specPow = exp2( gloss * 10.0 + 1.0 );
  107.  
  108.                 UnityLight light;
  109.                 #ifdef LIGHTMAP_OFF
  110.                     light.color = lightColor;
  111.                     light.dir = lightDirection;
  112.                     light.ndotl = LambertTerm (normalDirection, light.dir);
  113.                 #else
  114.                     light.color = half3(0.f, 0.f, 0.f);
  115.                     light.ndotl = 0.0f;
  116.                     light.dir = half3(0.f, 0.f, 0.f);
  117.                 #endif
  118.                 UnityGIInput d;
  119.                 d.light = light;
  120.                 d.worldPos = i.posWorld.xyz;
  121.                 d.worldViewDir = viewDirection;
  122.                 d.atten = attenuation;
  123.                 #if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)
  124.                     d.ambient = 0;
  125.                     d.lightmapUV = i.ambientOrLightmapUV;
  126.                 #else
  127.                     d.ambient = i.ambientOrLightmapUV;
  128.                 #endif
  129.                 #if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTION
  130.                     d.boxMin[0] = unity_SpecCube0_BoxMin;
  131.                     d.boxMin[1] = unity_SpecCube1_BoxMin;
  132.                 #endif
  133.                 #if UNITY_SPECCUBE_BOX_PROJECTION
  134.                     d.boxMax[0] = unity_SpecCube0_BoxMax;
  135.                     d.boxMax[1] = unity_SpecCube1_BoxMax;
  136.                     d.probePosition[0] = unity_SpecCube0_ProbePosition;
  137.                     d.probePosition[1] = unity_SpecCube1_ProbePosition;
  138.                 #endif
  139.                 d.probeHDR[0] = unity_SpecCube0_HDR;
  140.                 d.probeHDR[1] = unity_SpecCube1_HDR;
  141.                 Unity_GlossyEnvironmentData ugls_en_data;
  142.                 ugls_en_data.roughness = 1.0 - gloss;
  143.                 ugls_en_data.reflUVW = viewReflectDirection;
  144.                 UnityGI gi = UnityGlobalIllumination(d, 1, normalDirection, ugls_en_data );
  145.                 lightDirection = gi.light.dir;
  146.                 lightColor = gi.light.color;
  147.  
  148.                 float NdotL = saturate(dot( normalDirection, lightDirection ));
  149.                 float LdotH = saturate(dot(lightDirection, halfDirection));
  150.                 float3 specularColor = node_9886;
  151.                 float specularMonochrome;
  152.                 float4 _MainTexture_var = tex2D(_MainTexture,TRANSFORM_TEX(i.uv0, _MainTexture));
  153.                 float3 node_9511 = normalDirection.rgb;
  154.                 float node_6886 = normalize(i.posWorld.g);
  155.                 float node_9376_if_leA = step(node_6886,0.0);
  156.                 float node_9376_if_leB = step(0.0,node_6886);
  157.                 float node_1759 = min(0,dot(node_9511.g,node_6886));
  158.                 float3 node_6249 = float3(node_9511.r,lerp((node_9376_if_leA*node_1759)+(node_9376_if_leB*max(0,dot(node_9511.g,node_6886))),node_1759,node_9376_if_leA*node_9376_if_leB),node_9511.b);
  159.                 float3 node_1351 = (node_6249*node_6249);
  160.                 float3 node_3784 = float3(0,0,0);
  161.                 float2 node_9531 = float2(i.posWorld.r,i.posWorld.b);
  162.                 float4 _SnowTexture_var = tex2D(_SnowTexture,TRANSFORM_TEX(node_9531, _SnowTexture));
  163.                 float3 diffuseColor = (_MainTexture_var.rgb+(node_1351.r*node_3784 + node_1351.g*(_SnowTexture_var.rgb*_SnowAmount) + node_1351.b*node_3784)); // Need this for specular when using metallic
  164.                 diffuseColor = DiffuseAndSpecularFromMetallic( diffuseColor, specularColor, specularColor, specularMonochrome );
  165.                 specularMonochrome = 1.0-specularMonochrome;
  166.                 float NdotV = abs(dot( normalDirection, viewDirection ));
  167.                 float NdotH = saturate(dot( normalDirection, halfDirection ));
  168.                 float VdotH = saturate(dot( viewDirection, halfDirection ));
  169.                 float visTerm = SmithJointGGXVisibilityTerm( NdotL, NdotV, roughness );
  170.                 float normTerm = GGXTerm(NdotH, roughness);
  171.                 float specularPBL = (visTerm*normTerm) * UNITY_PI;
  172.                 #ifdef UNITY_COLORSPACE_GAMMA
  173.                     specularPBL = sqrt(max(1e-4h, specularPBL));
  174.                 #endif
  175.                 specularPBL = max(0, specularPBL * NdotL);
  176.                 #if defined(_SPECULARHIGHLIGHTS_OFF)
  177.                     specularPBL = 0.0;
  178.                 #endif
  179.                 half surfaceReduction;
  180.                 #ifdef UNITY_COLORSPACE_GAMMA
  181.                     surfaceReduction = 1.0-0.28*roughness*perceptualRoughness;
  182.                 #else
  183.                     surfaceReduction = 1.0/(roughness*roughness + 1.0);
  184.                 #endif
  185.                 specularPBL *= any(specularColor) ? 1.0 : 0.0;
  186.                 float3 directSpecular = attenColor*specularPBL*FresnelTerm(specularColor, LdotH);
  187.                 half grazingTerm = saturate( gloss + specularMonochrome );
  188.                 float3 indirectSpecular = (gi.indirect.specular);
  189.                 indirectSpecular *= FresnelLerp (specularColor, grazingTerm, NdotV);
  190.                 indirectSpecular *= surfaceReduction;
  191.                 float3 specular = (directSpecular + indirectSpecular);
  192.  
  193.                 NdotL = max(0.0,dot( normalDirection, lightDirection ));
  194.                 half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);
  195.                 float nlPow5 = Pow5(1-NdotL);
  196.                 float nvPow5 = Pow5(1-NdotV);
  197.                 float3 directDiffuse = ((1 +(fd90 - 1)*nlPow5) * (1 + (fd90 - 1)*nvPow5) * NdotL) * attenColor;
  198.                 float3 indirectDiffuse = float3(0,0,0);
  199.                 indirectDiffuse += gi.indirect.diffuse;
  200.                 float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
  201.  
  202.                 float3 finalColor = diffuse + specular;
  203.                 return fixed4(finalColor,1);
  204.             }
  205.             ENDCG
  206.         }
  207.         Pass {
  208.             Name "FORWARD_DELTA"
  209.             Tags {
  210.                 "LightMode"="ForwardAdd"
  211.             }
  212.             Blend One One
  213.             ZTest Always
  214.            
  215.            
  216.             CGPROGRAM
  217.             #pragma vertex vert
  218.             #pragma fragment frag
  219.             #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
  220.             #define _GLOSSYENV 1
  221.             #include "UnityCG.cginc"
  222.             #include "AutoLight.cginc"
  223.             #include "Lighting.cginc"
  224.             #include "UnityPBSLighting.cginc"
  225.             #include "UnityStandardBRDF.cginc"
  226.             #pragma multi_compile_fwdadd_fullshadows
  227.             #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
  228.             #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
  229.             #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
  230.             #pragma only_renderers d3d9 d3d11 glcore gles
  231.             #pragma target 3.0
  232.             uniform sampler2D _SnowTexture; uniform float4 _SnowTexture_ST;
  233.             uniform float _SnowAmount;
  234.             uniform sampler2D _MainTexture; uniform float4 _MainTexture_ST;
  235.             uniform sampler2D _NormalMap; uniform float4 _NormalMap_ST;
  236.             struct VertexInput {
  237.                 float4 vertex : POSITION;
  238.                 float3 normal : NORMAL;
  239.                 float4 tangent : TANGENT;
  240.                 float2 texcoord0 : TEXCOORD0;
  241.                 float2 texcoord1 : TEXCOORD1;
  242.                 float2 texcoord2 : TEXCOORD2;
  243.             };
  244.             struct VertexOutput {
  245.                 float4 pos : SV_POSITION;
  246.                 float2 uv0 : TEXCOORD0;
  247.                 float2 uv1 : TEXCOORD1;
  248.                 float2 uv2 : TEXCOORD2;
  249.                 float4 posWorld : TEXCOORD3;
  250.                 float3 normalDir : TEXCOORD4;
  251.                 float3 tangentDir : TEXCOORD5;
  252.                 float3 bitangentDir : TEXCOORD6;
  253.                 LIGHTING_COORDS(7,8)
  254.             };
  255.             VertexOutput vert (VertexInput v) {
  256.                 VertexOutput o = (VertexOutput)0;
  257.                 o.uv0 = v.texcoord0;
  258.                 o.uv1 = v.texcoord1;
  259.                 o.uv2 = v.texcoord2;
  260.                 o.normalDir = UnityObjectToWorldNormal(v.normal);
  261.                 o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );
  262.                 o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
  263.                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
  264.                 float3 lightColor = _LightColor0.rgb;
  265.                 o.pos = UnityObjectToClipPos( v.vertex );
  266.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
  267.                 return o;
  268.             }
  269.             float4 frag(VertexOutput i) : COLOR {
  270.                 i.normalDir = normalize(i.normalDir);
  271.                 float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
  272.                 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  273.                 float4 _NormalMap_var = tex2D(_NormalMap,TRANSFORM_TEX(i.uv0, _NormalMap));
  274.                 float3 normalLocal = _NormalMap_var.rgb;
  275.                 float3 normalDirection = normalize(mul( normalLocal, tangentTransform ));
  276.                 float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));
  277.                 float3 lightColor = _LightColor0.rgb;
  278.                 float3 halfDirection = normalize(viewDirection+lightDirection);
  279.  
  280.                 UNITY_LIGHT_ATTENUATION(attenuation,i, i.posWorld.xyz);
  281.                 float3 attenColor = attenuation * _LightColor0.xyz;
  282.                 float Pi = 3.141592654;
  283.                 float InvPi = 0.31830988618;
  284.  
  285.                 float node_9886 = 0.0;
  286.                 float gloss = node_9886;
  287.                 float perceptualRoughness = 1.0 - node_9886;
  288.                 float roughness = perceptualRoughness * perceptualRoughness;
  289.                 float specPow = exp2( gloss * 10.0 + 1.0 );
  290.  
  291.                 float NdotL = saturate(dot( normalDirection, lightDirection ));
  292.                 float LdotH = saturate(dot(lightDirection, halfDirection));
  293.                 float3 specularColor = node_9886;
  294.                 float specularMonochrome;
  295.                 float4 _MainTexture_var = tex2D(_MainTexture,TRANSFORM_TEX(i.uv0, _MainTexture));
  296.                 float3 node_9511 = normalDirection.rgb;
  297.                 float node_6886 = normalize(i.posWorld.g);
  298.                 float node_9376_if_leA = step(node_6886,0.0);
  299.                 float node_9376_if_leB = step(0.0,node_6886);
  300.                 float node_1759 = min(0,dot(node_9511.g,node_6886));
  301.                 float3 node_6249 = float3(node_9511.r,lerp((node_9376_if_leA*node_1759)+(node_9376_if_leB*max(0,dot(node_9511.g,node_6886))),node_1759,node_9376_if_leA*node_9376_if_leB),node_9511.b);
  302.                 float3 node_1351 = (node_6249*node_6249);
  303.                 float3 node_3784 = float3(0,0,0);
  304.                 float2 node_9531 = float2(i.posWorld.r,i.posWorld.b);
  305.                 float4 _SnowTexture_var = tex2D(_SnowTexture,TRANSFORM_TEX(node_9531, _SnowTexture));
  306.                 float3 diffuseColor = (_MainTexture_var.rgb+(node_1351.r*node_3784 + node_1351.g*(_SnowTexture_var.rgb*_SnowAmount) + node_1351.b*node_3784));
  307.                 diffuseColor = DiffuseAndSpecularFromMetallic( diffuseColor, specularColor, specularColor, specularMonochrome );
  308.                 specularMonochrome = 1.0-specularMonochrome;
  309.                 float NdotV = abs(dot( normalDirection, viewDirection ));
  310.                 float NdotH = saturate(dot( normalDirection, halfDirection ));
  311.                 float VdotH = saturate(dot( viewDirection, halfDirection ));
  312.                 float visTerm = SmithJointGGXVisibilityTerm( NdotL, NdotV, roughness );
  313.                 float normTerm = GGXTerm(NdotH, roughness);
  314.                 float specularPBL = (visTerm*normTerm) * UNITY_PI;
  315.                 #ifdef UNITY_COLORSPACE_GAMMA
  316.                     specularPBL = sqrt(max(1e-4h, specularPBL));
  317.                 #endif
  318.                 specularPBL = max(0, specularPBL * NdotL);
  319.                 #if defined(_SPECULARHIGHLIGHTS_OFF)
  320.                     specularPBL = 0.0;
  321.                 #endif
  322.                 specularPBL *= any(specularColor) ? 1.0 : 0.0;
  323.                 float3 directSpecular = attenColor*specularPBL*FresnelTerm(specularColor, LdotH);
  324.                 float3 specular = directSpecular;
  325.  
  326.                 NdotL = max(0.0,dot( normalDirection, lightDirection ));
  327.                 half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);
  328.                 float nlPow5 = Pow5(1-NdotL);
  329.                 float nvPow5 = Pow5(1-NdotV);
  330.                 float3 directDiffuse = ((1 +(fd90 - 1)*nlPow5) * (1 + (fd90 - 1)*nvPow5) * NdotL) * attenColor;
  331.                 float3 diffuse = directDiffuse * diffuseColor;
  332.  
  333.                 float3 finalColor = diffuse + specular;
  334.                 return fixed4(finalColor,0);
  335.             }
  336.             ENDCG
  337.         }
  338.     }
  339.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement