Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

snow shader: adding texture in place of color

Discussion in 'Shaders' started by roach_779, Feb 2, 2014.

  1. roach_779

    roach_779

    Joined:
    Aug 2, 2012
    Posts:
    37
    Hey Guys,

    I got an awesome snow shader that mimics snow on objects from the net (sorry I can't find source) and the shader is using color, but I want to use texture. I been playing around with the code and can't seem to get it going, so I need help in getting shader working. Here the code:

    Code (csharp):
    1.  
    2.  
    3. Shader "Custom/SnowShader" {
    4.     Properties {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _Bump ("Bump", 2D) = "bump" {}
    7.         _Snow ("Snow Level", Range(0,1) ) = 0
    8.         _SnowColor ("Snow Color", Color) = (1.0,1.0,1.0,1.0)
    9.         _SnowDirection ("Snow Direction", Vector) = (0,1,0)
    10.         _SnowDepth ("Snow Depth", Range(0,0.2)) = 0.1
    11.         _Wetness ("Wetness", Range(0, 0.5)) = 0.3
    12.     }
    13.     SubShader {
    14.         Tags { "RenderType"="Opaque" }
    15.         LOD 200
    16.  
    17.         CGPROGRAM
    18.         #pragma surface surf Lambert vertex:vert
    19.  
    20.         sampler2D _MainTex;
    21.         sampler2D _Bump;
    22.         float _Snow;
    23.         float4 _SnowColor;
    24.         float4 _SnowDirection;
    25.         float _SnowDepth;
    26.         float _Wetness;
    27.  
    28.         struct Input {
    29.             float2 uv_MainTex;
    30.             float2 uv_Bump;
    31.             float3 worldNormal;
    32.             INTERNAL_DATA
    33.         };
    34.  
    35.          void vert (inout appdata_full v) {
    36.                    //Convert the normal to world coortinates
    37.                    float4 sn = mul(UNITY_MATRIX_IT_MV, _SnowDirection);
    38.  
    39.                   if(dot(v.normal, sn.xyz) >= lerp(1,-1, (_Snow*2)/3))
    40.                   {
    41.                       v.vertex.xyz += (sn.xyz + v.normal) * _SnowDepth * _Snow;
    42.                   }
    43.                 }
    44.  
    45.         void surf (Input IN, inout SurfaceOutput o) {
    46.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    47.             o.Normal = UnpackNormal (tex2D (_Bump, IN.uv_Bump));
    48.             half difference = dot(WorldNormalVector(IN, o.Normal), _SnowDirection.xyz) - lerp(1,-1,_Snow);;
    49.             difference = saturate(difference / _Wetness);
    50.             o.Albedo = difference*_SnowColor.rgb + (1-difference) *c;
    51.             o.Alpha = c.a;
    52.         }
    53.         ENDCG
    54.     }
    55.     FallBack "Diffuse"
    56. }
    57.  
    Thanks!

    Roach
     
  2. smd863

    smd863

    Joined:
    Jan 26, 2014
    Posts:
    292
    You would a new property
    Code (csharp):
    1.  
    2. _SnowTex ("Snow Texture", 2D) = "white" {}
    3.  
    A new sampler2D
    Code (csharp):
    1.  
    2. sampler2D _SnowTex;
    3.  
    And then do a texture lookup in the surface shader
    Code (csharp):
    1.  
    2. half3 snow =_SnowColor * tex2D (_SnowTex, IN.uv_MainTex);
    3. o.Albedo = lerp(c, snow, difference);
    4.  
    In this case, I'm just piggy-backing the snow texture on the UV's of the main texture if that works for your model. You may want to use the same UV's for the main texture and bump, and then use the second set of UVs more suited to tiling for the snow texture (and maybe a snow bump map).

    That's completely untested so no guarantee that will work as-is, but it shouldn't be any more complicated than that.
     
  3. roach_779

    roach_779

    Joined:
    Aug 2, 2012
    Posts:
    37
    Thanks it worked!

    Question:
    _SnowTex is using _MainTex's UV, is it also connected to _MainTex's tiling? Numbers I put in _MainTex UV is also effecting _Snowtex tiling..._Snowtex tiling has no effect. Anyway of _SnowTex having its own tiling?

    If I want to get rid of _Bump, do I delete any line with _Bump?

    Thanks again!
     
  4. roach_779

    roach_779

    Joined:
    Aug 2, 2012
    Posts:
    37
    Looks like I'm just getting the following errors:

    Shader warning in 'Custom/SnowShader01_WithTex': Not enough temporary registers, needs 9 (compiling for flash) at line 16

    Shader warning in 'Custom/SnowShader': Not enough temporary registers, needs 9 (compiling for flash) at line 15

    You know the cause?

    Code (csharp):
    1.  
    2. Shader "Custom/SnowShader01_WithTex" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}  
    5.         _SnowTex ("Snow Texture", 2D) = "white" {}
    6.         _Bump ("Bump", 2D) = "bump" {}
    7.         _Snow ("Snow Level", Range(0,1) ) = 0
    8.         _SnowColor ("Snow Color", Color) = (1.0,1.0,1.0,1.0)
    9.         _SnowDirection ("Snow Direction", Vector) = (0,1,0)
    10.         _SnowDepth ("Snow Depth", Range(0,0.2)) = 0.1
    11.         _Wetness ("Wetness", Range(0, 0.5)) = 0.3
    12.     }
    13.     SubShader {
    14.         Tags { "RenderType"="Opaque" }
    15.         LOD 200
    16.  
    17.         CGPROGRAM
    18.         #pragma surface surf Lambert vertex:vert
    19.  
    20.         sampler2D _MainTex;
    21.         sampler2D _SnowTex;
    22.         sampler2D _Bump;
    23.         float _Snow;
    24.         float4 _SnowColor;
    25.         float4 _SnowDirection;
    26.         float _SnowDepth;
    27.         float _Wetness;
    28.  
    29.         struct Input {
    30.             float2 uv_MainTex;
    31.             float2 uv_Bump;
    32.             float3 worldNormal;
    33.             INTERNAL_DATA
    34.         };
    35.  
    36.          void vert (inout appdata_full v) {
    37.                    //Convert the normal to world coortinates
    38.                    float4 sn = mul(UNITY_MATRIX_IT_MV, _SnowDirection);
    39.  
    40.                   if(dot(v.normal, sn.xyz) >= lerp(1,-1, (_Snow*2)/3))
    41.                   {
    42.                       v.vertex.xyz += (sn.xyz + v.normal) * _SnowDepth * _Snow;
    43.                   }
    44.                 }
    45.  
    46.         void surf (Input IN, inout SurfaceOutput o) {
    47.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    48.             o.Normal = UnpackNormal (tex2D (_Bump, IN.uv_Bump));
    49.             half difference = dot(WorldNormalVector(IN, o.Normal), _SnowDirection.xyz) - lerp(1,-1,_Snow);;
    50.             difference = saturate(difference / _Wetness);
    51.             half3 snow =_SnowColor * tex2D (_SnowTex, IN.uv_MainTex);
    52.             o.Albedo = lerp(c, snow, difference);        
    53.             o.Alpha = c.a;
    54.         }
    55.         ENDCG
    56.     }
    57.     FallBack "Diffuse"
    58. }
    59.  
    Thanks
     
  5. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    This is because the shader is trying to be compiled for Flash aswell. You can use#pragma target 3.0 to set the shader model to 3.0 and it should get rid of the errors. Also, you will need uv_SnowTex if you are to use the inspector values to change the tiling of the snow texture to be different to that of the diffuse.
     
  6. roach_779

    roach_779

    Joined:
    Aug 2, 2012
    Posts:
    37
    Cool...it worked! Error gone and SnowTex has its own tile control.

    Question. I have another question, but for another shader. How would can insert a detail channel in following shader:

    Code (csharp):
    1.  
    2.  
    3.     Shader "VertexLighting/DiffuseVtxCol" {
    4.     Properties {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 400
    11.        
    12.     CGPROGRAM
    13.     #pragma surface surf BlinnPhong
    14.      
    15.      
    16.     sampler2D _MainTex;
    17.     fixed4 _Color;
    18.      
    19.     struct Input {
    20.         float2 uv_MainTex;
    21.         half4 color : COLOR0;
    22.     };
    23.      
    24.     void surf (Input IN, inout SurfaceOutput o) {
    25.         fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    26.         o.Albedo = tex.rgb * _Color.rgb * IN.color.rgb;
    27.         o.Alpha = tex.a * _Color.a;
    28.     }
    29.     ENDCG
    30.     }
    31.      
    32.     FallBack "Diffuse"
    33.     }
    34.  
    Thanks!