包含此页的版本:
不含此页的版本:
此示例展示了一个基本的 URP 兼容着色器。此着色器填充meshUnity 的主要图形原语。网格体构成了 3D 世界的很大一部分。Unity 支持三角或四边形多边形网格。Nurbs、Nurms、Subdiv 曲面必须转换为多边形。更多信息
请参阅术语表形状,其中包含着色器代码中预定义的颜色。
要亲自试用着色器,请将以下 ShaderLab 代码复制并粘贴到着色器资源中。
注意:如果在URP资源中启用 深度启动模式(Depth Priming Mode),则此着色器会将不透明对象渲染为不可见。有关详细信息,请参阅仅在着色器中写入深度。
// 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 的 Unity 特定语言编写的ShaderLabUnity 用于定义 Shader 对象结构的语言。更多信息
请参阅术语表.
此示例中的 Unity 着色器具有以下块:
ShaderLab 代码以Shader声明。
Shader "Example/URPUnlitShaderBasic"
此声明中的路径确定材质上着色器菜单中Unity着色器的显示名称和位置。Shader.Find 方法也使用此路径。
Properties 块包含用户可以在检查器一个 Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑值。更多信息
请参阅术语表窗口。
在此示例中,Properties 块为空,因为此 Unity 着色器不会公开用户可以定义的任何材质属性。
Unity 着色器源文件包含一个或多个 SubShader 块。渲染网格时,Unity 会选择与目标设备上的 GPU 兼容的第一个子着色器。
SubShader 块可以选择包含 SubShader Tags 块。使用Tags关键字来声明 SubShader Tags 块。
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
名称为RenderPipeline告诉 Unity 哪个渲染管线 获取场景内容并将其显示在屏幕上的一系列作。Unity 允许您从预构建的渲染管道中进行选择,或编写自己的渲染管道。更多信息
请参阅术语表将此 SubShader 与UniversalPipeline表示 Unity 应将此 SubShader 与 URP 一起使用。
要在不同的渲染管道中执行相同的着色器,请创建多个具有不同RenderPipeline标记值。要在 HDRP 中执行 SubShader 块,请将RenderPipeline标签设置为HDRenderPipeline,要在内置渲染管线中执行它,请将RenderPipeline设置为空值。
有关子着色器标签的更多信息,请参阅 ShaderLab:子着色器标签。
在此示例中,有一个 Pass 块包含 HLSL 程序代码。有关 Pass 块的更多信息,请参阅 ShaderLab:Pass。
Pass 块可以选择包含 Pass tags 块。有关更多信息,请参阅URP ShaderLab Pass标签。
此块包含 HLSL 程序代码。
注意:HLSL 语言是 URP 着色器的首选语言。
此块包含#include声明,并引用Core.hlsl文件。
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
这Core.hlslfile 包含常用 HLSL 宏和函数的定义,还包含对其他 HLSL 文件的 #include 引用(例如Common.hlsl和SpaceTransforms.hlsl).
例如,顶点着色器 渲染模型时在 3D 模型的每个顶点上运行的程序。更多信息
请参阅术语表在 HLSL 代码中使用TransformObjectToHClip函数从SpaceTransforms.hlsl文件。该函数将顶点位置从对象空间转换为同质空间:
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
return OUT;
}
注意:不要从同一着色器中的可脚本渲染管线(SRP)和内置渲染管线导入着色器库文件。某些SRP着色器宏和函数可能与内置渲染管线着色器函数冲突。有关更多信息,请参阅URP中的着色器方法和从内置渲染管线中的着色器库导入文件。
此基本 HLSL 代码中的片段着色器输出代码中预定义的单一颜色:
half4 frag() : SV_Target
{
half4 customColor;
customColor = half4(0.5, 0, 0, 1);
return customColor;
}
带颜色输入的URP无光照着色器部分展示了如何在材质的 检查器(Inspector) 窗口中添加可编辑颜色属性。