Hi I have been looking for a snow / up vector shader for quite some time but so far the only shader I have found was this; http://unitygems.com/noobs-guide-shaders-2/ I tried to create this shader but it didnt seem to work in Unity 4 at all unfortunately. Do any of you good people have such a shader lying around or would you know how to get the linked shader working. Thanks in advance
Code (csharp): Shader "Custom/SnowShader" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _Bump ("Bump", 2D) = "bump" {} _Snow ("Snow Level", Range(0,1) ) = 0 _SnowColor ("Snow Color", Color) = (1.0,1.0,1.0,1.0) _SnowDirection ("Snow Direction", Vector) = (0,1,0) _SnowDepth ("Snow Depth", Range(0,0.2)) = 0.1 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Lambert vertex:vert sampler2D _MainTex; sampler2D _Bump; float _Snow; float4 _SnowColor; 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 coortinates float3 snormal = normalize(_SnowDirection.xyz); float3 sn = mul((float3x3)_World2Object, snormal).xyz; if(dot(v.normal, sn) >= lerp(1,-1, (_Snow*2)/3)) { v.vertex.xyz += normalize(sn + v.normal) * _SnowDepth * _Snow; } } void surf (Input IN, inout SurfaceOutput o) { half4 c = tex2D (_MainTex, IN.uv_MainTex); o.Normal = UnpackNormal (tex2D (_Bump, IN.uv_Bump)); if(dot(WorldNormalVector(IN, o.Normal), _SnowDirection.xyz)>=lerp(1,-1,_Snow)) { o.Albedo = _SnowColor.rgb; } else { o.Albedo = c.rgb; } o.Alpha = 1; } ENDCG } FallBack "Diffuse" }
James youre the man! Thanks a lot How much would it complicate matters if I instead of snow wanted another texture (w. normalmap) applied? IE pebbles/sand covering a rock mesh?
Not much. I did this a while back based on the same tutorial. Here's the shader with normalmapped texture blending instead of color blending: Code (csharp): Shader "Custom/BlendSamplersByDirection" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _MainBump ("MainBump", 2D) = "bump" {} _LayerTex ("Layer (RGB)", 2D) = "white" {} _LayerBump ("LayerBump", 2D) ="bump" {} _LayerStrength ("Layer Strength", Range(0, 1)) = 0 _LayerDirection ("Layer Direction", Vector) = (0, 1, 0) _LayerDepth ("Layer Depth", Range(0, 0.005)) = 0.0005 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma target 3.0 #pragma surface surf Lambert vertex:vert sampler2D _MainTex; sampler2D _MainBump; sampler2D _LayerTex; sampler2D _LayerBump; float _LayerStrength; float3 _LayerDirection; float _LayerDepth; struct Input { float2 uv_MainTex; float2 uv_MainBump; float2 uv_LayerTex; float2 uv_LayerBump; float3 worldNormal; INTERNAL_DATA }; void vert (inout appdata_full v) { // Convert the normal to world coordinates/world space float3 sn = mul((float3x3)_World2Object, _LayerDirection); if (dot(v.normal, sn.xyz) >= lerp(1, -1, (_LayerStrength * 2) / 3)) { v.vertex.xyz += (sn.xyz + v.normal) * _LayerDepth * _LayerStrength; } } void surf (Input IN, inout SurfaceOutput o) { // Diffuse color of pixel half4 mainDiffuse = tex2D(_MainTex, IN.uv_MainTex); half4 layerDiffuse = tex2D(_LayerTex, IN.uv_LayerTex); // Normal vector of pixel o.Normal = UnpackNormal(tex2D(_MainBump, IN.uv_MainBump)); half3 layerNormal = half3(0, 0, 0); // Snow mask half sm = dot(WorldNormalVector(IN, o.Normal), _LayerDirection); sm = pow(0.5 * sm + 0.5, 2.0); if (sm >= lerp(1, 0, _LayerStrength)) { o.Albedo = (layerDiffuse.rgb + 0.5 * mainDiffuse.rgb) * 0.75; layerNormal = UnpackNormal(tex2D(_LayerBump, IN.uv_LayerBump)); o.Normal = normalize(o.Normal + layerNormal); } else { o.Albedo = mainDiffuse.rgb; } o.Alpha = mainDiffuse.a; } ENDCG } FallBack "Diffuse" } Though you should note the difference in the target version. I think I was forced to use SM 3.0 because of the 2 additional texture samplers.
This is really cool, but could you blend this texture (snow/moss etc..) with a heightmap? That would make it look even nicer.
This is just the shader i was looking for. It is very fast on mobile. Because i'm a noob when it comes to shaders, I would like to ask if someone could modify this shader so it supports cutout, so even the trees would look good. That would be a lifesaver for me