Version: 6000.3
语言: 中文
从 URP 着色器库导入文件
在自定义 URP 着色器中使用摄像机

在自定义 URP 着色器中变换位置

在自定义通用中转换位置渲染管线(Render Pipeline) 获取场景内容并将其显示在屏幕上的一系列作。Unity 允许您从预构建的渲染管道中进行选择,或编写自己的渲染管道。更多信息
请参阅术语表
(URP)着色器在 GPU 上运行的程序。更多信息
请参阅术语表
,请按照下列步骤作:

  1. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"HLSLPROGRAM在着色器文件中。这Core.hlsl文件导入ShaderVariablesFunction.hlsl文件。
  2. 使用以下方法之一ShaderVariablesFunction.hlsl文件。
方法 语法 描述
GetNormalizedScreenSpaceUV float2 GetNormalizedScreenSpaceUV(float2 positionInClipSpace) 将剪辑空间中的位置转换为屏幕空间。
GetObjectSpaceNormalizeViewDir half3 GetObjectSpaceNormalizeViewDir(float3 positionInObjectSpace) 将对象空间中的位置转换为朝向查看器的规范化方向。
GetVertexNormalInputs VertexNormalInputs GetVertexNormalInputs(float3 normalInObjectSpace) 将对象空间中顶点的法线转换为世界空间中的切线、双角形和法线。您还可以输入法线和float4对象空间中的切线。
GetVertexPositionInputs VertexPositionInputs GetVertexPositionInputs(float3 positionInObjectSpace) 将对象空间中顶点的位置转换为世界空间、视图空间、裁剪空间和规范化设备坐标中的位置。
GetWorldSpaceNormalizeViewDir half3 GetWorldSpaceNormalizeViewDir(float3 positionInWorldSpace) 将方向从世界空间中的某个位置返回给查看器,并规范化方向。
GetWorldSpaceViewDir float3 GetWorldSpaceViewDir(float3 positionInWorldSpace) 将从世界空间中的某个位置返回到查看器的方向。

结构体

顶点位置输入

使用 GetVertexPositionInputs 方法获取此结构。

描述
float3 positionWS 在世界空间中的位置。
float3 positionVS 视图空间中的位置。
float4 positionCS 剪辑空间中的位置。
float4 positionNDC 位置为规范化设备坐标 (NDC)。

顶点法线输入(VertexNormalInputs)

使用GetVertexNormalInputs方法来获取此结构体。

描述
real3 tangentWS 世界空间中的切线。
real3 bitangentWS 世界空间中的双胞胎。
float3 normalWS 世界空间中的常态。

以下 URP 着色器使用表示其在屏幕空间中的位置的颜色绘制对象表面。

Shader "Custom/ScreenSpacePosition"
{
    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 positionWS : TEXCOORD2;
            };

            Varyings vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);

                // Get the position of the vertex in different spaces
                VertexPositionInputs positions = GetVertexPositionInputs(IN.positionOS);

                // Set positionWS to the screen space position of the vertex
                OUT.positionWS = positions.positionWS.xyz;

                return OUT;
            }

            half4 frag(Varyings IN) : SV_Target
            {
                // Set the fragment color to the screen space position vector
                return float4(IN.positionWS.xy, 0, 1);
            }
            ENDHLSL
        }
    }
}

其他资源

从 URP 着色器库导入文件
在自定义 URP 着色器中使用摄像机