Unity URP Shader之环境贴图,反射探针
首先弄明白以下几个知识点:
1. 环境贴图,unity中即CubeMap,采样时需求出视角方向在法线方向的反射方向,用此反射方向来采样CubeMap,拿到颜色后还需解码hdr,公式如下:
half3 reflectDir = reflect(-viewDir, worldNormal);
hlaf4 cubeCol = texCube(_CubeMap, reflectDir);
half3 envCol = DecodeHDREnvironment(cubeCol, _CubeMap_HDR);
2. 反射探针(Reflection Probe),在这里创建:
它的作用是将设为Reflection Probe Static的物体烘焙成一张CubeMap贴图,以供shader采样,从而模拟现实中的环境反射。
采样方法如下:
half4 envCol = SAMPLE_TEXTURECUBE(unity_SpecCube0, samplerunity_SpecCube0, reflectDir);
half3 envHDRCol = DecodeHDREnvironment(envCol, unity_SpecCube0_HDR);
3. 有时我们需要用一张图片来模拟CubeMap,那么如果采样呢,公式如下:
float latitude = acos(reflectDir.y);
float longitude = atan2(reflectDir.z, reflectDir.x);
float2 sphereCoords = float2(longitude, latitude) * float2(0.5 / PI, 1 / PI);
float2 uv = float2(0.5, 1) - sphereCoords;
half4 cubeCol = SAMPLE_TEXTURE2D(_PanoramaMap, samper_PanoramaMap, uv);
half3 envCol = DecodeHDREnvironment(cubeCol, _PanoramaMap_HDR);
4. 有时我们需要对reflectDir做旋转,公式如下:
float3 RotateAround(float degree, float3 target)
{
float rad = PI * degree / 180;
float2x2 matrixRot = float2x2(cos(rad), -sin(rad), sin(rad), cos(rad));
float2 rotDir = mul(matrixRot, target.xz);
return float3(rotDir.x, target.y, rotDir.y);
}
效果如下:
转载请注明出处:https://www.cnblogs.com/jietian331/p/17027019.html