美文网首页
冰雪效果

冰雪效果

作者: 星易乾川 | 来源:发表于2018-08-21 09:59 被阅读0次
    冰雪.jpg
    雪.jpg
    冰雪材质面板.jpg

    增加了实时光影


    阴影.jpg

    冰城效果,冰没有做半透,考虑到移动端的渲染效率,直接用贴图+法线+反射球来模拟,反射球采样的时候用了另一张法线来偏移
    雪就是用了正常的光照模型+法线来偏移,加了一张高光贴图控制雪上星星点点的闪光,加上菲涅耳计算
    最后用了法线的y轴做了下冰和雪差值进行混合

    Shader "Artist/Scene/IceMobile" {
        Properties{
                _SnowColor("Snow Main Color", Color) = (0.78,0.78,0.78,1)
                _SnowBumpTex("SnowBumpTex",2D) = "white" {}
                _Snow_specular("Snow_specular",2D) = "white" {}
                _SnowDirection ("Snow Direction", Vector) = (0,1,0)
                _Specular ("Specular Color", Color) = (1, 1, 1, 1)
                _Gloss ("Gloss", Range(8.0, 256)) = 20
                _RimColor("RimColor", Color) = (1,1,1,1)
                _RimPower("RimPower", Range(0.000001, 3.0)) = 0.1
                [Space(40)]
                _Color("Ice Main Color", Color) = (0.78,0.78,0.78,1)
                _MainTex("Ice MainTex ", 2D) = "white" {}
                _BumpMap("Ice NormalMap", 2D) = "bump" {}
                _Cube("Reflection Cubemap", Cube) = "" {}
                [HDR]_ReflectColor("Reflection Color", Color) = (0.78,0.78,0.78,1)
                _LightColor ("LightColor", Color) = (1, 1, 1, 1) 
                _ambientColor ("AmbientColor", Color) = (1, 1, 1, 1) 
                _ShadowStrength("ShadowStrength", float) = 0.5
        }
     
           SubShader{
                    Tags{ "RenderType"="Opaque" "Queue"="Geometry"  }
                    Pass{
                    
                        Cull Back
                        Tags { "LightMode"="ForwardBase" }
     
                    CGPROGRAM
     
                    #pragma vertex vert
                    #pragma fragment frag
                    #include "UnityCG.cginc"
                    #include "Lighting.cginc"
                    #include "AutoLight.cginc"
                    #pragma multi_compile_fwdbase 
                    #pragma multi_compile_fog
                    struct appdata_t {
                    float4 vertex : POSITION;
                    float2 texcoord: TEXCOORD0;
                    half4 color : COLOR;
                    float3 normal : NORMAL;
                    
                };
                struct appdata
                {
                    float4 vertex : POSITION;           
                    float4 texcoord : TEXCOORD0;
                    float3 normal : NORMAL;
                    float4 tangent : TANGENT;
                
                };
                struct v2f {
                    float4 vertex : SV_POSITION;
                    float4 uv_Main : TEXCOORD0;
                    float3 refl : TEXCOORD1;
                    float3 lightDir : TEXCOORD2;
                    float4 TtoW0 : TEXCOORD3;  
                    float4 TtoW1 : TEXCOORD4;  
                    float4 TtoW2 : TEXCOORD5;
                    float4 uv_Snow : TEXCOORD6;
                    UNITY_FOG_COORDS(8)
                    SHADOW_COORDS(7)
                };
                    float4 _SnowDirection;
                    sampler2D _MainTex;
                    sampler2D _BumpMap;
                    float4 _MainTex_ST;
                    float4 _BumpMap_ST;
                    samplerCUBE _Cube;
                    half4 _Color;
                    half4 _ReflectColor;
                    half4 _SnowColor;
                    fixed4 _Specular;
                    float _Gloss;
                    sampler2D _SnowBumpTex;
                    float4 _SnowBumpTex_ST;
                    sampler2D _Snow_specular;
                    float4 _Snow_specular_ST;
                    fixed4 _RimColor;
                    float _RimPower;
                    fixed3 _ambientColor;
                    fixed3 _LightColor;
                    float _ShadowStrength;
                v2f vert(appdata v)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    fixed3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                    o.uv_Main.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
                    o.uv_Main.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);
                    o.uv_Snow.xy = TRANSFORM_TEX(v.texcoord, _Snow_specular);
                    o.uv_Snow.zw = TRANSFORM_TEX(v.texcoord, _SnowBumpTex);
                    float3 viewDir = mul(unity_ObjectToWorld, v.vertex).xyz - _WorldSpaceCameraPos;
                    fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);  
                    o.refl = reflect(viewDir, worldNormal);
                    
                    fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);  
                    fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w; 
                    o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);  
                    o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);  
                    o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);  
                    UNITY_TRANSFER_FOG(o,o.vertex);
                    TRANSFER_SHADOW(o);
                    return o;
                }
     
                half4 frag(v2f i) : SV_Target
                {
                    fixed3 lightDir = normalize(UnityWorldSpaceLightDir(half3(i.TtoW0.w,i.TtoW1.w,i.TtoW2.w)));
                    fixed3 viewDir = normalize(UnityWorldSpaceViewDir(half3(i.TtoW0.w,i.TtoW1.w,i.TtoW2.w)));
                    float rim = 1 - max(0, dot(viewDir, half3(i.TtoW0.z, i.TtoW1.z, i.TtoW2.z)));
                    fixed3 rimColor = _RimColor * pow(rim, 1 / _RimPower);
                    half3 bump = UnpackNormal(tex2D(_BumpMap, i.uv_Main.zw));
                    fixed3 Snowbump = UnpackNormal(tex2D(_SnowBumpTex, i.uv_Snow.zw));
                    Snowbump = normalize(half3(dot(i.TtoW0.xyz, Snowbump), dot(i.TtoW1.xyz, Snowbump), dot(i.TtoW2.xyz, Snowbump)));
                    half4 col = tex2D(_MainTex, i.uv_Main.xy );
                    fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * col.rgb * _ambientColor;
                    UNITY_LIGHT_ATTENUATION(atten, i, half3(i.TtoW0.w,i.TtoW1.w,i.TtoW2.w));
                    fixed3 diffuseL = _LightColor0.rgb * max(0, dot(half3(i.TtoW0.z, i.TtoW1.z, i.TtoW2.z), lightDir))*_LightColor*col.rgb;
                    half reflcol = dot(texCUBE(_Cube, i.refl*(bump*2-1)), 0.3);
                    col.rgb = col.rgb * _Color + diffuseL * atten + reflcol * _ReflectColor.rgb * col.rgb ;
                    half4 Snow_specular = tex2D(_Snow_specular, i.uv_Snow.xy);
                    fixed3 diffuse = _LightColor0.rgb * max(0, dot(Snowbump, lightDir)) * _SnowColor * (1 - (1 - atten) *  _ShadowStrength );
                    fixed3 halfDir = normalize(lightDir + viewDir);
                    fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(Snowbump, halfDir)), _Gloss)* Snow_specular;
                    
                    
                    col.rgb = lerp(col.rgb + ambient, diffuse + specular + rimColor + ambient, clamp(dot(half3(i.TtoW0.z, i.TtoW1.z, i.TtoW2.z), _SnowDirection.xyz),_SnowDirection.w,1));
                    col.a = 1;
                    UNITY_APPLY_FOG(i.fogCoord, col);
                    return col;
                    //return half4(col.rgb, 1);
                    
                }
                ENDCG
                
            }
        }
     FallBack "Specular"
    }
    
    

    相关文章

      网友评论

          本文标题:冰雪效果

          本文链接:https://www.haomeiwen.com/subject/ajrdiftx.html