Skip to content

Instantly share code, notes, and snippets.

@smokelore
Last active July 21, 2016 13:14
Show Gist options
  • Select an option

  • Save smokelore/26042b146258bd5da12671a4d667cb8b to your computer and use it in GitHub Desktop.

Select an option

Save smokelore/26042b146258bd5da12671a4d667cb8b to your computer and use it in GitHub Desktop.
Shader "CookbookShaders/Chapter05/SnowShader"
{
Properties
{
_MainColor("Main Color", Color) = (1.0,1.0,1.0,1.0)
_MainTex("Base (RGB)", 2D) = "white" {}
_Bump("Bump", 2D) = "bump" {}
_SnowLevel("Level of snow", Range(1, -1)) = 1
_SnowColor("Color of snow", Color) = (1.0,1.0,1.0,1.0)
_SnowDirection("Direction of snow", Vector) = (0,1,0)
_SnowDepth("Depth of snow", Range(0,1)) = 0
}
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard vertex:vert
sampler2D _MainTex;
sampler2D _Bump;
float _SnowLevel;
float4 _SnowColor;
float4 _MainColor;
float4 _SnowDirection;
float _SnowDepth;
struct Input
{
float2 uv_MainTex;
float2 uv_Bump;
float3 worldNormal;
INTERNAL_DATA
};
void vert(inout appdata_full v)
{
// Convert the normal to world coordinates
float4 sn = mul(UNITY_MATRIX_IT_MV, _SnowDirection);
if (dot(v.normal, sn.xyz) >= _SnowLevel)
{
v.vertex.xyz += (sn.xyz + v.normal) * _SnowDepth * _SnowLevel;
}
}
void surf(Input IN, inout SurfaceOutputStandard o)
{
half4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Normal = UnpackNormal(tex2D(_Bump, IN.uv_Bump));
if (dot(WorldNormalVector(IN, o.Normal), _SnowDirection.xyz) >= _SnowLevel)
{
o.Albedo = _SnowColor.rgb;
}
else
{
o.Albedo = c.rgb * _MainColor;
}
o.Alpha = 1;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment