要在自定义通用 渲染管线一系列操作,将场景内容显示在屏幕上。Unity 允许您选择预制的渲染管线,或编写自己的渲染管线。 更多信息
参见 术语表 (URP) 着色器在 GPU 上运行的程序。 更多信息
参见 术语表 中使用 相机一个组件,用于创建场景中特定视点的图像。输出要么绘制到屏幕上,要么捕获为纹理。 更多信息
参见 术语表,请按照以下步骤操作
HLSLPROGRAM 内添加 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"。Core.hlsl 文件导入 ShaderVariablesFunction.hlsl 文件。ShaderVariablesFunction.hlsl 文件中的以下方法之一。| 方法 | 语法 | 描述 | 
|---|---|---|
| GetCameraPositionWS | float3 GetCameraPositionWS() | 返回相机的世界空间位置。 | 
| GetScaledScreenParams | float4 GetScaledScreenParams() | 返回屏幕的宽度和高度,以 像素计算机图像中最小的单位。像素大小取决于您的屏幕分辨率。每个屏幕像素都会计算像素光照。 更多信息 参见 术语表 为单位。 | 
| GetViewForwardDir | float3 GetViewForwardDir() | 返回视图在世界空间中的前进方向。 | 
| IsPerspectiveProjection | bool IsPerspectiveProjection() | 如果相机的投影设置为透视,则返回 true。 | 
| LinearDepthToEyeDepth | half LinearDepthToEyeDepth(half linearDepth) | 将线性 深度缓冲区一个内存存储,用于保存图像中每个像素的 z 值深度,其中 z 值是每个渲染像素从投影平面到相机的深度。 更多信息 参见 术语表 值转换为视图深度。有关更多信息,请参阅 相机和深度纹理。 | 
| TransformScreenUV | void TransformScreenUV(inout float2 screenSpaceUV) | 如果 Unity 使用颠倒的坐标空间,则翻转屏幕空间位置的 y 坐标。您也可以输入 uv和屏幕高度作为float,以便该方法输出按像素缩放的屏幕大小位置。 | 
以下 URP 着色器使用代表从表面到相机的方向的颜色绘制对象表面。
Shader "Custom/DirectionToCamera"
{
    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            struct Attributes
            {
                float4 positionOS : POSITION;
                float2 uv: TEXCOORD0;
            };
            struct Varyings
            {
                float4 positionCS  : SV_POSITION;
                float2 uv: TEXCOORD0;
                float3 viewDirection : TEXCOORD2;
            };
            Varyings vert(Attributes IN)
            {
                Varyings OUT;
                // Get the positions of the vertex in different coordinate spaces
                VertexPositionInputs positions = GetVertexPositionInputs(IN.positionOS);
                OUT.positionCS = positions.positionCS;
                // Get the direction from the vertex to the camera, in world space
                OUT.viewDirection = GetCameraPositionWS() - positions.positionWS.xyz;
                return OUT;
            }
            half4 frag(Varyings IN) : SV_Target
            {
                // Set the fragment color to the direction vector
                return float4(IN.viewDirection, 1);
            }
            ENDHLSL
        }
    }
}