首页 > 其他分享 >Shader入门精要笔记 - CH10.1_环境映射之折射

Shader入门精要笔记 - CH10.1_环境映射之折射

时间:2023-02-28 23:33:06浏览次数:47  
标签:CH10.1 Color Shader 折射 fixed3 fixed4 rgb worldNormal 环境映射

折射:光线从一种介质进入另一种介质,传播方向会发生改变

 

 

Cubemap是在Teapot_Refract的位置拍摄的

Shader "My/Tex2/RefractCubeMap"
{
    Properties
    {
        _MainTex("Main Tex", 2D) = "white" {}
        _Color("Color Tint", Color) = (1, 1, 1, 1)

        _Specular("Specular", Color) = (1, 1, 1, 1) //高光反射颜色
        _Gloss("Gloss", Range(8.0, 256)) = 20 //高光区域大小

        _RefractColor("Refraction Color", Color) = (1, 1, 1, 1) //折射颜色
        _RefractAmount("Refract Amount", Range(0, 1)) = 1 //折射程度
        _RefractRatio("Refract Ratio", Range(0.1, 1)) = 0.5 //入射介质与折射介质的折射率比值
        _Cubemap("Refraction Cubemap", Cube) = "_Skybox" {} //折射环境时从这个贴图采样环境
    }

    SubShader
    {
        Tags { "RenderType" = "Opaque" "Queue" = "Geometry"}

        Pass
        {
            Tags { "LightMode" = "ForwardBase" }

            CGPROGRAM

            #pragma multi_compile_fwdbase

            #pragma vertex vert
            #pragma fragment frag

            #include "Lighting.cginc"
            #include "AutoLight.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;

            fixed4 _Specular;
            float _Gloss;

            fixed4 _RefractColor;
            fixed _RefractAmount;
            fixed _RefractRatio;
            samplerCUBE _Cubemap;

            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL; //顶点法线
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 worldNormal : TEXCOORD1;
                float3 worldPos : TEXCOORD2;
                float3 worldViewDir : TEXCOORD3;
                float3 worldRefr : TEXCOORD4;
                SHADOW_COORDS(5)
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;

                o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos);
                o.worldRefr = refract(-normalize(o.worldViewDir), normalize(o.worldNormal), _RefractRatio); //折射方向

                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                TRANSFER_SHADOW(o);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed3 worldNormal = normalize(i.worldNormal);
                fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
                fixed3 worldViewDir = normalize(i.worldViewDir);

                fixed4 texColor = tex2D(_MainTex, i.uv);
                fixed3 albedo = texColor.rgb * _Color.rgb; //取贴图颜色作为漫反射颜色

                fixed3 lambert = max(0, dot(worldNormal, worldLightDir)); //表面法线和光线方向夹角的cos值成正比
                fixed3 diffuse = _LightColor0.rgb * albedo * lambert; //漫反射计算公式

                fixed3 halfDir = normalize(worldLightDir + worldViewDir); // blinn模型引入的h向量
                fixed3 specularColor = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(worldNormal, halfDir)), _Gloss); // blinn模型高光反射计算公式(更亮, 高光区域更大)

                fixed3 refraction = texCUBE(_Cubemap, i.worldRefr).rgb * _RefractColor.rgb; //根据折射光线采样颜色

                UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos); //光照衰减+接收阴影宏

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo; //环境光
                fixed3 color = ambient + (lerp(diffuse, refraction, _RefractAmount) + specularColor) * atten;

                return fixed4(color, 1.0);
            }

            ENDCG
        }
    }

    FallBack "Reflective/VertexLit" //引用它的ShadowCaster的Pass
}

 

标签:CH10.1,Color,Shader,折射,fixed3,fixed4,rgb,worldNormal,环境映射
From: https://www.cnblogs.com/sailJs/p/17085559.html

相关文章