此示例展示了一个基本的与 URP 兼容的着色器。此着色器用在着色器代码中预定义的颜色填充网格Unity 中的主要图形基元。网格构成 3D 世界的很大一部分。Unity 支持三角形或四边形多边形网格。NURBS、NURMS、细分曲面必须转换为多边形。 更多信息
查看 术语表形状。
要自己尝试着色器,请将以下 ShaderLab 代码复制并粘贴到着色器资源中。
// This shader fills the mesh shape with a color predefined in the code.
Shader "Example/URPUnlitShaderBasic"
{
    // The properties block of the Unity shader. In this example this block is empty
    // because the output color is predefined in the fragment shader code.
    Properties
    { }
    // The SubShader block containing the Shader code.
    SubShader
    {
        // SubShader Tags define when and under which conditions a SubShader block or
        // a pass is executed.
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
        Pass
        {
            // The HLSL code block. Unity SRP uses the HLSL language.
            HLSLPROGRAM
            // This line defines the name of the vertex shader.
            #pragma vertex vert
            // This line defines the name of the fragment shader.
            #pragma fragment frag
            // The Core.hlsl file contains definitions of frequently used HLSL
            // macros and functions, and also contains #include references to other
            // HLSL files (for example, Common.hlsl, SpaceTransforms.hlsl, etc.).
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            // The structure definition defines which variables it contains.
            // This example uses the Attributes structure as an input structure in
            // the vertex shader.
            struct Attributes
            {
                // The positionOS variable contains the vertex positions in object
                // space.
                float4 positionOS   : POSITION;
            };
            struct Varyings
            {
                // The positions in this struct must have the SV_POSITION semantic.
                float4 positionHCS  : SV_POSITION;
            };
            // The vertex shader definition with properties defined in the Varyings
            // structure. The type of the vert function must match the type (struct)
            // that it returns.
            Varyings vert(Attributes IN)
            {
                // Declaring the output object (OUT) with the Varyings struct.
                Varyings OUT;
                // The TransformObjectToHClip function transforms vertex positions
                // from object space to homogenous clip space.
                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                // Returning the output.
                return OUT;
            }
            // The fragment shader definition.
            half4 frag() : SV_Target
            {
                // Defining the color variable and returning it.
                half4 customColor = half4(0.5, 0, 0, 1);
                return customColor;
            }
            ENDHLSL
        }
    }
}
片段着色器将游戏对象Unity 场景中的基本对象,可以代表角色、道具、场景、相机、航点等。游戏对象的功能由附加到它的组件定义。 更多信息
查看 术语表颜色设置为深红色(RGB 值为 (0.5, 0, 0))。
 
以下部分介绍了此基本 Unity 着色器的结构。
Unity 着色器是用 Unity 特定的语言编写的,称为ShaderLabUnity 用于定义着色器对象结构的语言。 更多信息
查看 术语表。
此示例中的 Unity 着色器包含以下块
ShaderLab 代码以 Shader 声明开头。
Shader "Example/URPUnlitShaderBasic"
此声明中的路径决定了 Unity 着色器在材质的“着色器”菜单中的显示名称和位置。Shader.Find 方法也使用此路径。
 
Properties 块包含用户可以在材质的InspectorUnity 窗口,显示有关当前选定游戏对象、资源或项目设置的信息,允许你检查和编辑值。 更多信息
查看 术语表窗口中设置的属性声明。
在此示例中,“属性”块为空,因为此 Unity 着色器没有公开用户可以定义的任何材质属性。
一个 Unity 着色器源文件包含一个或多个子着色器块。渲染网格时,Unity 会选择第一个与目标设备上的 GPU 兼容的子着色器。
子着色器块可以包含一个可选的子着色器标记块。使用 Tags 关键字声明子着色器标记块。
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
名称为 RenderPipeline 的子着色器标记告诉 Unity 使用哪个渲染管线一系列操作,获取场景的内容并将其显示在屏幕上。Unity 允许你从预构建的渲染管线中选择,或者编写自己的渲染管线。 更多信息
查看 术语表来使用此子着色器,UniversalPipeline 的值表示 Unity 应该将此子着色器与 URP 一起使用。
要在不同的渲染管线中执行相同的着色器,请创建多个具有不同 RenderPipeline 标记值的子着色器块。要在 HDRP 中执行子着色器块,请将 RenderPipeline 标记设置为 HDRenderPipeline,要在内置渲染管线中执行子着色器块,请将 RenderPipeline 设置为空值。
有关子着色器标记的更多信息,请参阅ShaderLab:子着色器标记。
在此示例中,有一个通道块,它包含 HLSL 程序代码。有关通道块的更多信息,请参阅ShaderLab:通道。
通道块可以包含一个可选的通道标记块。有关更多信息,请参阅URP ShaderLab 通道标记。
此块包含 HLSL 程序代码。
注意:HLSL 语言是 URP 着色器的首选语言。
注意:URP 支持 CG 语言。如果在着色器中添加了 CGPROGRAM/ENDCGPROGRAM 块,Unity 会自动包含来自内置渲染管线库的着色器。如果包含来自 SRP 着色器库的着色器,某些 SRP 着色器宏和函数可能会与内置渲染管线着色器函数冲突。包含 CGPROGRAM 块的着色器与 SRP 批量渲染器不兼容。
此块包含 #include 声明,其中引用了 Core.hlsl 文件。
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
Core.hlsl 文件包含常用 HLSL 宏和函数的定义,还包含对其他 HLSL 文件(例如 Common.hlsl 和 SpaceTransforms.hlsl)的 #include 引用。
例如,HLSL 代码中的顶点着色器在渲染模型时,对模型的每个顶点运行的程序。 更多信息
查看 术语表使用了 SpaceTransforms.hlsl 文件中的 TransformObjectToHClip 函数。该函数将顶点位置从对象空间转换为齐次空间
Varyings vert(Attributes IN)
{
    Varyings OUT;
    OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
    return OUT;
}
此基本 HLSL 代码中的片段着色器输出在代码中预定义的单一颜色
half4 frag() : SV_Target
{
    half4 customColor;
    customColor = half4(0.5, 0, 0, 1);
    return customColor;
}
部分 URP 带有颜色输入的无光照着色器 显示了如何在材质的 Inspector 窗口中添加可编辑的颜色属性。