一段根据片元深度值重建对应的深度坐标值的代码及其解释
请尊重原作者的工作,转载时请务必注明转载自:www.xionggf.com
// 从深度信息重建世界空间坐标 // 参数: // screenPos - 屏幕空间坐标(通常来自顶点着色器的SV_Position) // viewDir - 视图空间方向向量 // sceneDepth - 包含原始深度和线性深度值的结构体 float3 ReconstructWorldPosition(float4 screenPos, float3 viewDir, SceneDepth sceneDepth) { // 平台深度缓冲处理 ---------------------------------------- #if UNITY_REVERSED_Z // 处理Reversed-Z平台(如DirectX) real rawDepth = sceneDepth.raw; // 直接使用原始深度值 #else // OpenGL等常规深度缓冲平台 // 将深度值从[0,1]映射到[Near,1],适配OpenGL的NDC范围 real rawDepth = lerp(UNITY_NEAR_CLIP_VALUE, 1, sceneDepth.raw); #endif // 正交投影处理 -------------------------------------------- #if defined(ORTHOGRAPHIC_SUPPORT) // 将屏幕坐标转换为NDC空间(-1到1范围) float4 viewPos = float4( (screenPos.xy / screenPos.w) * 2.0 - 1.0, // 透视除法后映射到NDC rawDepth, // 深度值 1.