shader之——单光源,法线,高光,+天空盒环境反射(HDR)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shader之——单光源,法线,高光,+天空盒环境反射(HDR)相关的知识,希望对你有一定的参考价值。

Shader "baicai/me02" {
    Properties {
        _MainTex ("MainTex", 2D) = "white" {}
        _Bump ("Bump", 2D) = "bump" {}
        _Intension ("强度", Range(0, 10)) = 10
        _Scale ("范围", Range(0, 1)) = 0.5969197
    _SpecularColor("SpecularColor",Color) = (1,1,1,1)

    }
    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        Pass {
            Tags {
                "LightMode"="ForwardBase"
            }
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define UNITY_PASS_FORWARDBASE
            #include "UnityCG.cginc"
            #include "AutoLight.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0

             float4 _LightColor0;
             sampler2D _MainTex;
         float4 _MainTex_ST;
             sampler2D _Bump; 
         float4 _Bump_ST;
             float _Intension;
             float _Scale;
         float4 _SpecularColor;


            struct VertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 tangent : TANGENT;
                float2 uv : TEXCOORD0;
            };
            struct v2f {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float4 posWorld : TEXCOORD1;
                float3 normal : TEXCOORD2;
                float3 tangentDir : TEXCOORD3;
                float3 bitangentDir : TEXCOORD4;
                LIGHTING_COORDS(5,6)

            };
            v2f vert (VertexInput v) {
                v2f o ;
        o.pos = UnityObjectToClipPos(v.vertex );
                o.uv =TRANSFORM_TEX(v.uv, _MainTex);
                o.normal =normalize( UnityObjectToWorldNormal(v.normal));
                o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );//切线方向
                o.bitangentDir = normalize(cross(o.normal, o.tangentDir) * v.tangent.w);//双切线
                o.posWorld = mul(unity_ObjectToWorld, v.vertex);
                TRANSFER_VERTEX_TO_FRAGMENT(o)
                return o;
            }
            float4 frag(v2f i) : COLOR {
                float3 nVert = i.normal;//顶点法线,不支持法线贴图的时候 ,是把这个当做最终法线
                float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, nVert);
                float3 BumpMap = UnpackNormal(tex2D(_Bump,i.uv));
                float3 N = normalize(mul( BumpMap.rgb, tangentTransform )); // 法线贴图 模型法线 切线 相结合,得到最终的法线
        float3 V = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz); //视向量
                float3 L = normalize(_WorldSpaceLightPos0.xyz);
                float3 speL = normalize(V+L); //视向量 + 光向量,得到一个高光的光向量
////// Lighting:
                float atten = LIGHT_ATTENUATION(i);
///////// Gloss:
                float Gloss = exp2( _Scale * 10.0+1.0);
////// Specular:
                float NdotSpeL = saturate(dot (speL,N)); //高光强度
                float4 col = tex2D(_MainTex,i.uv);
                float3 specular0 = atten * _LightColor0.xyz * pow(NdotSpeL,Gloss) * _SpecularColor * _Intension*col.a;
///////worldRefl
        half3 worldRefl = reflect (-V,N);/////
        half4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, worldRefl);/////
        half3 skyColor = DecodeHDR (skyData, unity_SpecCube0_HDR);/////
/////// Diffuse:
        float NdotL = saturate(dot( N, L )); //漫反射强            
                col.rgb= col.rgb * (NdotL *_LightColor0.xyz * atten + UNITY_LIGHTMODEL_AMBIENT.rgb);
                
/// Final Color:
        col.rgb = col.rgb * 0.5 + skyColor*col.a*0.5 + specular0 ;/////
            
                return col;
            }
            ENDCG
        }
   }
   FallBack "Diffuse"
}

 

以上是关于shader之——单光源,法线,高光,+天空盒环境反射(HDR)的主要内容,如果未能解决你的问题,请参考以下文章

Unity Shader用Cubemap实现天空盒和环境映射

ShaderLab学习小结漫反射+高光+点光源

Unity Shader ------ 纹理之法线纹理单张纹理及遮罩纹理的实现

带法线贴图的高光着色

OpenGL:天空盒放大太多

shader之——多光源漫反射以及阴影