Unity坐标变换笔记
屏幕uv坐标计算方法顶点着色器float4 positionCS : SV_POSITION; //表示裁剪空间下的坐标float4 ndc input.positionCS * 0.5f; input.positionNDC.xy float2(ndc.x, ndc.y * _ProjectionParams.x) ndc.w; input.positionNDC.zw input.positionCS.zw;包含w分量在片元着色器需要除以w分量片元着色器float4 positionCS : SV_POSITION; //表示屏幕像素位置原点在左下角output.NDCPosition input.positionCS.xy / _ScaledScreenParams.xy;屏幕像素除以屏幕宽高由深度纹理重建世界坐标获取视角空间下的深度值透视相机投影矩阵视角空间下zview为负数取反位于Near和Far之间归一化正交相机投影矩阵zview d *(Far - Near) Near;重建世界坐标worldPos _WorldSpaceCameraPos linearDepth * interplateRay;Urp教程的重建世界坐标https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal14.0/manual/writing-shaders-urp-reconstruct-world-position.html// 由纹理获取ndc下的z值 #if UNITY_REVERSED_Z real depth SampleSceneDepth(UV); #else // Adjust z to match NDC for OpenGL real depth lerp(UNITY_NEAR_CLIP_VALUE, 1, SampleSceneDepth(UV)); #endif float2 UV IN.positionHCS.xy / _ScaledScreenParams.xy; // Reconstruct the world space positions. float3 worldPos ComputeWorldSpacePosition(UV, depth, UNITY_MATRIX_I_VP);https://docs.unity3d.com/Manual/SL-PlatformDifferences.htmlfloat3 ComputeWorldSpacePosition(float2 positionNDC, float deviceDepth, float4x4 invViewProjMatrix) { float4 positionCS ComputeClipSpacePosition(positionNDC, deviceDepth); float4 hpositionWS mul(invViewProjMatrix, positionCS); return hpositionWS.xyz / hpositionWS.w; } float4 ComputeClipSpacePosition(float2 positionNDC, float deviceDepth) { float4 positionCS float4(positionNDC * 2.0 - 1.0, deviceDepth, 1.0); #if UNITY_UV_STARTS_AT_TOP // Our world space, view space, screen space and NDC space are Y-up. // Our clip space is flipped upside-down due to poor legacy Unity design. // The flip is baked into the projection matrix, so we only have to flip // manually when going from CS to NDC and back. positionCS.y -positionCS.y; #endif return positionCS; }
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2482475.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!