Sample a texture in HLSL
Declare and sample a texture in HLSL using a sampler.
阅读时间3 分钟最后更新于 24 天前
To sample a texture in High-Level Shader Language (HLSL), declare a texture and its sampler. The sampler contains the properties the GPU uses to sample the texture, for example the wrap mode and filter mode. The properties usually come from the texture import settings.
Follow these steps:
-
In theblock, use the
HLSLPROGRAMmacro to declare the texture and its sampler. For example:UNITY_DECLARE_TEX2DUNITY_DECLARE_TEX2D(_MainTex);Unity creates a standard HLSLobject for the texture, and a standard HLSLTexture2Dobject for its sampler.SamplerState -
To sample the texture, use themacro. For example:
UNITY_SAMPLE_TEX2Dhalf4 color = UNITY_SAMPLE_TEX2D(_MainTex, uv);
If the graphics API doesn't support separate textures and samplers, Unity does the following:
- The macro falls back to creating a standard HLSL
UNITY_DECLARE_TEX2Dobject instead, which combines the texture and its sampler.sampler2D - The macro samples the texture using the combined texture and sampler.
UNITY_SAMPLE_TEX2D_SAMPLER
Sample other textures with the same sampler
To sample a second texture with the sampler from a texture, follow these steps:
-
In theblock, declare the second texture using the
HLSLPROGRAMmacro. For example:UNITY_DECLARE_TEX2D_NOSAMPLERUNITY_DECLARE_TEX2D_NOSAMPLER(_SecondTex); -
To sample the texture, use themacro, passing in the first texture as the sampler source. For example:
UNITY_SAMPLE_TEX2D_SAMPLERhalf4 color = UNITY_SAMPLE_TEX2D_SAMPLER(_SecondTex, _FirstTex, uv);
Create a custom sampler
To create and use a custom sampler directly in a shader, also known as an inline sampler, follow these steps:
-
Declare aobject and use its name to set its properties. For example, the following code creates a custom sampler that uses point filtering and the clamp wrapping mode:
SamplerStateSamplerState my_point_clamp_sampler; -
Sample the texture using themethod of a
Sampleobject, and pass in the custom sampler. For example:Texture2D_MainTex.Sample(my_point_clamp_sampler, uv);
For more information, refer to Built-in shader variables reference.
Example
The following example reuses the same sampler for three textures:
Shader "Custom/SeparateSamplerExample"{ Properties { _MainTex ("Main Texture", 2D) = "white" {} _SecondTex ("Second Texture", 2D) = "white" {} _ThirdTex ("Third Texture", 2D) = "white" {} } SubShader { Pass { HLSLPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" // Declare the first texture and its sampler. UNITY_DECLARE_TEX2D(_MainTex); // Declare the second and third textures without samplers. UNITY_DECLARE_TEX2D_NOSAMPLER(_SecondTex); UNITY_DECLARE_TEX2D_NOSAMPLER(_ThirdTex); struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert (appdata_base v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord; return o; } half4 frag (v2f i) : SV_Target { // Sample the first texture using its own sampler. half4 color1 = UNITY_SAMPLE_TEX2D(_MainTex, i.uv); // Sample the second and third textures using the sampler from the first texture. half4 color2 = UNITY_SAMPLE_TEX2D_SAMPLER(_SecondTex, _MainTex, i.uv); half4 color3 = UNITY_SAMPLE_TEX2D_SAMPLER(_ThirdTex, _MainTex, i.uv); // Return the combined color from all three textures. return (color1 + color2 + color3) / 3.0; } ENDHLSL } }}
Additional resources
- Use built-in shader functions
- HLSL documentation on the Microsoft website