欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity Shader 雪效果

程序员文章站 2024-02-12 22:42:10
...

原理:采集雪贴图和雪的法线贴图,然后求得世界下的法线方向,并用主纹理的法线贴图和雪的法线贴图进行插值,插值系数是世界空间和雪方向的点积,并用一个雪量变量进行控制,得到插值后的法线,实现如下:

Shader "Snow"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
		_SnowTex("SnowTex",2D) = "white"{}
		_NormalTex("Normal",2D) = "bump"{}
		_SnowNormal("SnowNormal",2D) = "bump"{}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
		_SnowDir("SnowDir",Vector) = (0,1,0)
		_SnowAmount("SnowAmount",Range(0,10)) = 1

    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200
		Cull off


        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf StandardSpecular fullforwardshadows
		#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
		sampler2D _NormalTex;
		sampler2D _SnowTex;
		sampler2D _SnowNormal;

        struct Input
        {
            float2 uv_MainTex;
			float2 uv_NormalTex;
			float2 uv_SnowTex;
			float2 uv_SnowNormal;
			float3 worldNormal;
			INTERNAL_DATA
        };

        half _Glossiness;
        half _Metallic;
		half _SnowAmount;
        fixed4 _Color;
		fixed4 _SnowDir;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandardSpecular o)
        {
			float3 normalTex = UnpackNormal(tex2D(_NormalTex,IN.uv_NormalTex));
			float3 snowNormalTex =UnpackNormal(tex2D(_SnowNormal,IN.uv_SnowNormal));

			fixed3 WorldNormal = WorldNormalVector(IN,float3(0,0,1));
			fixed3 finalNormal =lerp(normalTex,snowNormalTex, saturate(dot(WorldNormal,_SnowDir.xyz)*_SnowAmount));
			o.Normal = finalNormal;

			fixed3 finalworldNormal = WorldNormalVector(IN,finalNormal);
			float lerpVal = saturate( dot(finalworldNormal,_SnowDir.xyz));

			
            // Albedo comes from a texture tinted by color
            fixed4 c = lerp(tex2D (_MainTex, IN.uv_MainTex) * _Color,tex2D(_SnowTex,IN.uv_SnowTex),lerpVal *_SnowAmount);

            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

效果如下:
Unity Shader 雪效果