UnityShader:IBL

效果:

实现:

Shader "MyShader/IBL"
{
    Properties
    {
        _CubeMap ("环境贴图", Cube) = "white" {}
          _Exposure("曝光",float)=1.0
        _Color("颜色",color)=(1,1,1,1)
        _NormalMap("法线贴图",2d)="bump"{}
        _AOMap("环境遮蔽",2d)="white"{}    
        _RoughnessMap("粗糙度贴图",2d)="black"{}
        _RoughnessContrast("粗糙度对比度",Range(0,10))=1
        _RoughnessLight("粗糙度亮度",float)=1
        _Roughness("粗糙度",Range(0,1))=1

    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

           inline float3 ACES_ToneMapping(float3 v)
            {
                v *= 0.6f;
                float a = 2.51f;
                float b = 0.03f;
                float c = 2.43f;
                float d = 0.59f;
                float e = 0.14f;
                return saturate((v*(a*v+b))/(v*(c*v+d)+e));
            }   

            struct appdata
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                float3 normal : NORMAL;
                float3 tangent : TANGENT;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 normal_world : TEXCOORD1;
                float3 tangent_dir : TEXCOORD2;
                float3 binormal_dir : TEXCOORD3;
                float3 pos_world : TEXCOORD4;
            };

            samplerCUBE _CubeMap;
            float4 _CubeMap_HDR;
            sampler2D _NormalMap;
            float4 _NormalMap_ST;
            sampler2D _AOMap;
            float _Exposure;
            float4 _Color;
            float _RoughnessContrast;
            sampler2D _RoughnessMap;
            float _RoughnessLight;
            float _Roughness;

            v2f vert (appdata v)
            {
                v2f o = (v2f)0;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord,_NormalMap);
                o.normal_world = normalize(mul(v.vertex,unity_WorldToObject).xyz);
                o.tangent_dir = normalize(mul(unity_ObjectToWorld,v.tangent).xyz);
                o.binormal_dir = normalize(cross(o.normal_world,o.tangent_dir));
                o.pos_world = mul(unity_ObjectToWorld,v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
               half3 normal_world = normalize(i.normal_world);
               half3 tangent_dir = normalize(i.tangent_dir);
               half3 binormal_dir = normalize(i.binormal_dir);
               half3 normal_data = UnpackNormal(tex2D(_NormalMap,i.uv));
               float3x3 TBN = float3x3(tangent_dir,binormal_dir,normal_world);
               normal_world = normalize(mul(normal_data,TBN));

               half3 view_dir = normalize(_WorldSpaceCameraPos.xyz-i.pos_world);
               half3 hdr_reflect_dir = normalize(reflect(-view_dir,normal_world));

               half roughness = tex2D(_RoughnessMap,i.uv);
               roughness = saturate(pow(roughness,_RoughnessContrast)*_RoughnessLight+_Roughness);
               roughness = roughness*(1.7-0.7*roughness);
               float mip_level = roughness*6;
               half4 cube_color = texCUBElod(_CubeMap,float4(hdr_reflect_dir,mip_level));
               half3 env_color = DecodeHDR(cube_color,_CubeMap_HDR);
               half ao_color = tex2D(_AOMap,i.uv);
               half3 final_color = env_color*ao_color*_Color.rgb*_Exposure;
               half3 final_color_liner = pow(final_color,2.2);
               half3 final_color_gamma = pow(ACES_ToneMapping(final_color_liner),1.0/2.2);
               return fixed4(final_color_gamma,1.0);
            }
            ENDCG
        }
    }
}
相关推荐
神码编程5 小时前
【Unity功能集】TextureShop纹理工坊(五)选区
unity·游戏引擎·shader·ps选区
LuH11246 小时前
【论文阅读笔记】Learning to sample
论文阅读·笔记·图形渲染·点云
m0_7482517211 小时前
Android webview 打开本地H5项目(Cocos游戏以及Unity游戏)
android·游戏·unity
benben04411 小时前
Unity3D仿星露谷物语开发7之事件创建动画
unity·游戏引擎
林枫依依14 小时前
Unity2021.3.16f1可以正常打开,但是Unity2017.3.0f3却常常打开闪退或者Unity2017编辑器运行起来就闪退掉
unity
虾球xz14 小时前
游戏引擎学习第57天
学习·游戏引擎
异次元的归来1 天前
Unity DOTS中的share component
unity·游戏引擎
向宇it1 天前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
_oP_i1 天前
unity webgl部署到iis报错
unity
Go_Accepted1 天前
Unity全局雾效
unity