效果:
实现:
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
}
}
}