渲染纹理的模板元素。
用于从渲染纹理访问底层表面的模板数据,并将其绑定在着色器中。
在创建渲染纹理前,确保将渲染纹理的 stencilFormat 设置为目标平台上支持的格式。
要访问着色器中的模板信息,则需要从正确通道读取它。
除 DirectX 11 外,对于支持模板数据的所有图形 API,模板数据存储在红色通道中,在 DirectX 11 的情况下,模板数据存储在绿色通道中。
如果将 stencilFormat 设置为 /R8_UInt/,则需要使用函数 Load 从纹理读取。
否则,如果格式为 /R8_UNorm/,可使用采样。
当使用 MSAA 渲染纹理时,需要使用等效的 MS 纹理类型定义模板纹理。
不能绑定作为 RWTexture 的模板缓冲区。
以下是一个无光照着色器示例,该着色器使用 stencilFormat R8_UInt
从渲染纹理读取模板信息,并在绿色通道上将其作为颜色信息写入。
另请参阅:GraphicsFormat。
Shader "Unlit/ExampleShader" { Properties { _ExampleTex("Texture", 2D) = "" {} } SubShader { Tags { "RenderType" = "Opaque" } LOD 100
Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma target 4.0
#include "UnityCG.cginc"
struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; };
struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; };
#if SHADER_API_D3D11 #define STENCIL_CHANNEL g #else #define STENCIL_CHANNEL r #endif
Texture2D<uint2> _ExampleTex;
v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; }
fixed4 frag(v2f i) : SV_Target { int xRes = 1024; int yRes = 768;
uint stencil = _ExampleTex.Load(int3(floor(i.uv.x * xRes), floor(i.uv.y * yRes), 0)).STENCIL_CHANNEL; fixed4 col = float4(0.0, float(stencil) / 255.0f, 0.0, 1.0);
return col; }
ENDCG } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.