我创建了一个渲染纹理,可以使用来自UE编辑器的视图菜单来对其进行可视化。当前,纹理是单一纯色。我想使用虚幻着色器以像素深度填充纹理。我知道深度缓冲区已经存在,但是此练习将帮助我了解UE中的整个着色器管道。有没有一种方法可以将着色器中的深度值转储到纹理上?我有以下顶点着色器。
struct FTriangleIDPassVSToPS
{
float4 Position : SV_POSITION;
};
#define FTriangleIDPassVSOutput FTriangleIDPassVSToPS
void MainVS(FVertexFactoryInput Input,
out FTriangleIDPassVSOutput Output
#if USE_GLOBAL_CLIP_PLANE
, out float OutGlobalClipPlaneDistance : SV_ClipDistance
#endif
)
{
ResolvedView = ResolveView();
FVertexFactoryIntermediates VFIntermediates = GetVertexFactoryIntermediates(Input);
float4 WorldPos = VertexFactoryGetWorldPosition(Input, VFIntermediates);
float3x3 TangentToLocal = VertexFactoryGetTangentToLocal(Input, VFIntermediates);
FMaterialVertexParameters VertexParameters = GetMaterialVertexParameters(Input, VFIntermediates, WorldPos.xyz, TangentToLocal);
// Isolate instructions used for world position offset
// As these cause the optimizer to generate different position calculating instructions in each pass, resulting in self-z-fighting.
// This is only necessary for shaders used in passes that have depth testing enabled.
ISOLATE
{
WorldPos.xyz += GetMaterialWorldPositionOffset(VertexParameters);
}
ISOLATE
{
float4 RasterizedWorldPosition = VertexFactoryGetRasterizedWorldPosition(Input, VFIntermediates, WorldPos);
#if ODS_CAPTURE
float3 ODS = OffsetODS(RasterizedWorldPosition.xyz, ResolvedView.TranslatedWorldCameraOrigin.xyz, ResolvedView.StereoIPD);
Output.Position = INVARIANT(mul(float4(RasterizedWorldPosition.xyz + ODS, 1.0), ResolvedView.TranslatedWorldToClip));
#else
Output.Position = INVARIANT(mul(RasterizedWorldPosition, ResolvedView.TranslatedWorldToClip));
#endif
}
}
了解“输出”给出顶点的裁剪空间位置。在像素着色器中,我应该这样做吗?
void MainPS(FTriangleIDPassVSToPS Output, out uint OutColor : SV_Target0)
{
OutColor = Output.Position.z / Output.Position.w;
}
当我可视化渲染纹理时,它没有给出预期的结果。您能指出正确的做法吗?谢谢