包含此页的版本:
不含此页的版本:
团结着色器在 GPU 上运行的程序。更多信息
请参阅术语表在此示例中,将 基础颜色(Base Color) 属性添加到材质。您可以使用该属性选择颜色,着色器会填充meshUnity 的主要图形原语。网格体构成了 3D 世界的很大一部分。Unity 支持三角或四边形多边形网格。Nurbs、Nurms、Subdiv 曲面必须转换为多边形。更多信息
请参阅术语表形状与颜色。
使用URP unlit basic shader部分中的Unity着色器源文件,并对ShaderLabUnity 用于定义 Shader 对象结构的语言。更多信息
请参阅术语表法典:
添加_BaseColor属性定义添加到 Properties 块:
Properties
{
[MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
}
此声明将_BaseColor属性,并带有标签 Base Color:
当您使用[MainColor]属性时,Unity 使用此属性作为材质的主色。
注意:出于兼容性原因,该
_Color属性名称是保留名称。Unity 使用名称为_Color作为主色,即使它没有[MainColor]属性。
在 Properties 块中声明属性时,还需要在 HLSL 代码中声明它。
注意:为确保 Unity 着色器与 SRP Batcher 兼容,请在单个
CBUFFER块,名称为UnityPerMaterial.有关SRP批处理程序的更多信息,请参阅有关可编写脚本的渲染管线(SRP)批处理程序的文档。
在顶点着色器 渲染模型时在 3D 模型的每个顶点上运行的程序。更多信息
请参阅术语表:
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
CBUFFER_END
更改片段着色器中的代码,使其返回_BaseColor财产。
half4 frag() : SV_Target
{
return _BaseColor;
}
现在,您可以在检查器一个 Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑值。更多信息
请参阅术语表窗。片段着色器使用您选择的颜色填充网格。
以下是此示例的完整 ShaderLab 代码。
// This shader fills the mesh shape with a color that a user can change using the
// Inspector window on a Material.
Shader "Example/URPUnlitShaderColor"
{
// The _BaseColor variable is visible in the Material's Inspector, as a field
// called Base Color. You can use it to select a custom color. This variable
// has the default value (1, 1, 1, 1).
Properties
{
[MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
}
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;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
};
// To make the Unity shader SRP Batcher compatible, declare all
// properties related to a Material in a a single CBUFFER block with
// the name UnityPerMaterial.
CBUFFER_START(UnityPerMaterial)
// The following line declares the _BaseColor variable, so that you
// can use it in the fragment shader.
half4 _BaseColor;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
return OUT;
}
half4 frag() : SV_Target
{
// Returning the _BaseColor value.
return _BaseColor;
}
ENDHLSL
}
}
}
部分 绘制纹理 展示了如何在网格上绘制纹理。