包含此页的版本:
不含此页的版本:
团结着色器在 GPU 上运行的程序。更多信息
请参阅术语表在此示例中,在meshUnity 的主要图形原语。网格体构成了 3D 世界的很大一部分。Unity 支持三角或四边形多边形网格。Nurbs、Nurms、Subdiv 曲面必须转换为多边形。更多信息
请参阅术语表.
使用带有颜色输入的 URP unlit 着色器部分中的 Unity 着色器源文件,并对ShaderLabUnity 用于定义 Shader 对象结构的语言。更多信息
请参阅术语表法典:
在 Properties 块中,将现有代码替换为_BaseMap属性定义。
Properties
{
[MainTexture] _BaseMap("Base Map", 2D) = "white" {}
}
当您在 Properties 块中声明纹理属性时,Unity 会添加_BaseMap属性,并将标签 Base Map 添加到材质中,并将 Tiling 和 Offset 控件添加。
当您使用[MainTexture]属性时,Unity 使用此属性作为材质的主要纹理。
注意:出于兼容性原因,该
_MainTex属性名称是保留名称。Unity 使用名称为_MainTex作为主要纹理,即使它没有[MainTexture]属性。
在struct Attributes和struct Varyings,将uv纹理上的UV坐标的变量:
float2 uv : TEXCOORD0;
将纹理定义为 2D 纹理,并为其指定采样器。在 CBUFFER 块之前添加以下行:
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
TEXTURE2D和 SAMPLER 宏在Core.hlsl.
要使平铺和偏移工作,必须使用_ST后缀。这_ST后缀是必需的,因为某些宏(例如,TRANSFORM_TEX) 使用它。
注意:为确保 Unity 着色器与 SRP Batcher 兼容,请在单个
CBUFFER块,名称为UnityPerMaterial.有关SRP批处理程序的更多信息,请参阅有关可编写脚本的渲染管线(SRP)批处理程序的文档。
CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;
CBUFFER_END
要应用平铺和偏移变换,请在顶点着色器 渲染模型时在 3D 模型的每个顶点上运行的程序。更多信息
请参阅术语表:
OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);
这TRANSFORM_TEX宏定义在Macros.hlsl文件。这#include声明包含对该文件的引用。
在片段着色器中,使用SAMPLE_TEXTURE2D宏对纹理进行采样:
half4 frag(Varyings IN) : SV_Target
{
half4 color = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
return color;
}
现在,您可以在检查器一个 Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑值。更多信息
请参阅术语表窗。着色器在网格上绘制该纹理。
以下是此示例的完整 ShaderLab 代码。
// This shader draws a texture on the mesh.
Shader "Example/URPUnlitShaderTexture"
{
// The _BaseMap variable is visible in the Material's Inspector, as a field
// called Base Map.
Properties
{
[MainTexture] _BaseMap("Base Map", 2D) = "white" {}
}
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;
// The uv variable contains the UV coordinate on the texture for the
// given vertex.
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
// The uv variable contains the UV coordinate on the texture for the
// given vertex.
float2 uv : TEXCOORD0;
};
// This macro declares _BaseMap as a Texture2D object.
TEXTURE2D(_BaseMap);
// This macro declares the sampler for the _BaseMap texture.
SAMPLER(sampler_BaseMap);
CBUFFER_START(UnityPerMaterial)
// The following line declares the _BaseMap_ST variable, so that you
// can use the _BaseMap variable in the fragment shader. The _ST
// suffix is necessary for the tiling and offset function to work.
float4 _BaseMap_ST;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
// The TRANSFORM_TEX macro performs the tiling and offset
// transformation.
OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
// The SAMPLE_TEXTURE2D marco samples the texture with the given
// sampler.
half4 color = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
return color;
}
ENDHLSL
}
}
}
可视化法线向量一节展示了如何在网格体上可视化法线向量。