RT+曲面细分 雪材质

作者: 万里_aa3f | 来源:发表于2019-02-22 23:50 被阅读0次

    1.RT+曲面细分的设置
    2.雪下瓷砖的细节表现

    1.RT+曲面细分

    将一个正交摄像机设置在地面以下,将远裁剪面调小(节省深度RT的精度)
    正裁剪面在小球或着脚踝以下,能完整的更新RT 避免bug
    设置曲面细分tessellate:tessFixed #pragma target 4.6
    在vertex shader中变形顶点 (测试,不能进行额外数据的插值)
    我这里做的省事了,把RT与平面完全对齐,直接用模型的uv来读取 RT的UV
    不然需要在shader中做矩阵变换 读取辅助摄像机对应的RT数据

            float4 tessFixed(){
                return _Tess;
            }
    
            void vert(inout appdata v)
            {
                float d=tex2Dlod(_DispTex,float4(1-v.texcoord.x , v.texcoord.y , 0 , 0)).r * _Displacement;
                v.vertex.xyz -= normalize(v.normal)*d;
    
            }
    

    2.雪下瓷砖的细节表现

    1.区分哪里表现雪 哪里表现瓷砖
    modelPos是雪最高点的世界坐标高度,建模时,确保模型坐标轴点在平面上


    float4 modelPos = float4(0,0,0,1);
    float snowHeight=mul(unity_ObjectToWorld,modelPos).y;
    float low = snowHeight - IN.worldPos.y; 
    float mask = low/_Displacement;
    mask = smoothstep(0.85,0.95,mask);
    fixed3 finalCol = lerp(snowCol,tileCol,mask);
    

    2.对比度调节,模拟表面的薄雪

    fixed3 tileCol = tex2D(_MainTex, IN.uv_MainTex).xyz * _Color;
    tileCol = lerp(float3(0.5,0.5,0.5),tileCol,_Saturation);
    

    3.模拟砖缝中的雪
    在切线空间下,dot(法线,float3(0,0,1); 得到砖缝
    再用一个noise贴图在消除所有砖缝都藏积雪的感觉

    //color
    fixed3 snowCol = fixed3(0.8,0.8,0.8);
    //砖的颜色及调节饱和度
    fixed3 tileCol = tex2D(_MainTex, IN.uv_MainTex).xyz * _Color;
    tileCol = lerp(float3(0.5,0.5,0.5),tileCol,_Saturation);
    
    float3 normal = UnpackScaleNormal(tex2D(_NormalMap , IN.uv_MainTex),_BumpScale);
    //在切线空间计算 法线与z轴夹角
    float tileSnow = dot(normal,float3(0,0,1));
    //噪声图使得砖缝中的雪不全
    float detailMask=tex2D(_MaskMap,IN.uv_MaskMap).x;
    tileSnow = clamp(tileSnow + detailMask , 0 , 1);
    tileSnow = smoothstep(0.2,0.4,tileSnow);
    tileCol = lerp(snowCol,tileCol,tileSnow);   //得到非雪的color
    

    shader

    
    Shader "Strees/Snowtile"
    {
        Properties
        {
            
            _Color ("Color", Color) = (1,1,1,1)
            _MainTex ("Albedo (RGB)", 2D) = "white" {}
            _Saturation("Saturation",Range(0,2))=1.0
    
            _DispTex("Disp Texture",2D)="gray"{}
            _Tess("Tessellation",Range(1,32))=4
            _Displacement("Displacement",Range(0,1))=0.3
    
            _NormalMap("NormalMap",2D)="bump"{}
            _BumpScale("BumpScale",float)=1
            _MaskMap("MaskMap",2D)="white"{}
    
            _Glossiness ("Smoothness", Range(0,1)) = 0.5
            _Metallic ("Metallic", Range(0,1)) = 0.0
        }
        SubShader
        {
            Tags { "RenderType"="Opaque" }
            LOD 200
    
            CGPROGRAM
            #pragma surface surf Standard addshadow fullforwardshadows vertex:vert tessellate:tessFixed nolightmap
    
            #pragma target 4.6
    
            struct appdata{
                float4 vertex:POSITION;
                float4 tangent:TANGENT;
                float3 normal:NORMAL;
                float2 texcoord:TEXCOORD;
            };
    
            sampler2D _MainTex;
            sampler2D _DispTex;
            sampler2D _NormalMap;
            sampler2D _MaskMap;
    
            struct Input
            {
                float2 uv_MainTex;
                float3 worldPos;
                float2 uv_MaskMap;
            };
    
            float _Saturation;
            half _Glossiness;
            half _Metallic;
            fixed4 _Color;
            float _Tess;
            float _Displacement;
            float _BumpScale;
    
            //曲面细分
            float4 tessFixed(){
                return _Tess;
            }
    
            void vert(inout appdata v)
            {
                float d=tex2Dlod(_DispTex,float4(1-v.texcoord.x , v.texcoord.y , 0 , 0)).r * _Displacement;
                v.vertex.xyz -= normalize(v.normal)*d;
    
            }
    
            UNITY_INSTANCING_BUFFER_START(Props)
            UNITY_INSTANCING_BUFFER_END(Props)
    
            void surf (Input IN, inout SurfaceOutputStandard o)
            {
                //color
                fixed3 snowCol = fixed3(0.8,0.8,0.8);
                //砖的颜色及调节饱和度
                fixed3 tileCol = tex2D(_MainTex, IN.uv_MainTex).xyz * _Color;
                tileCol = lerp(float3(0.5,0.5,0.5),tileCol,_Saturation);
                //读取法线
                float3 normal = UnpackScaleNormal(tex2D(_NormalMap , IN.uv_MainTex),_BumpScale);
                //在切线空间计算 法线与z轴夹角
                float tileSnow = dot(normal,float3(0,0,1));
                //噪声图使得砖缝中的雪不全
                float detailMask=tex2D(_MaskMap,IN.uv_MaskMap).x;
                tileSnow = clamp(tileSnow + detailMask , 0 , 1);
                tileSnow = smoothstep(0.2,0.4,tileSnow);
                tileCol = lerp(snowCol,tileCol,tileSnow);   //得到非雪的color
                
                //data 
                //得到模型z轴零点的世界坐标
                float4 modelPos = float4(0,0,0,1);
                float snowHeight=mul(unity_ObjectToWorld,modelPos).y;
                float low = snowHeight - IN.worldPos.y; 
                float mask = low/_Displacement;
                mask = smoothstep(0.85,0.95,mask);
                fixed3 finalCol = lerp(snowCol,tileCol,mask);
                
                //fianlNormal
                float3 normal2 = float3(0,0,1);
                float3 finalNormal=lerp(normal2,normal,mask);
    
                o.Albedo = finalCol;
                o.Normal = finalNormal;
                o.Metallic = _Metallic;
                o.Smoothness = _Glossiness;
                o.Alpha = 1.0;
            }
            ENDCG
        }
        FallBack "Diffuse"
    }
    

    相关文章

      网友评论

        本文标题:RT+曲面细分 雪材质

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