Most of the time when sampling textures in shadersA program that runs on the GPU. More info
See in Glossary, the texture sampling state should come from texture settings – essentially, textures and samplers are coupled together. This is default behavior when using DX9-style shader syntax:
sampler2D _MainTex; // ... half4 color = tex2D(_MainTex, uv);
Using sampler2D, sampler3D, samplerCUBE HLSL keywords declares both texture and sampler.
Most of the time this is what you want, and is the only supported option on older graphics APIs (OpenGL ES).
Many graphics APIs and GPUs allow using fewer samplers than textures, and coupled texture+sampler syntax might not allow more complex shaders to be written. For example, Direct3D 11 allows using up to 128 textures in a single shader, but only up to 16 samplers.
Unity allows declaring textures and samplers using DX11-style HLSL syntax, with a special naming convention to match them up: samplers that have names in the form of “sampler”+TextureName will take sampling states from that texture.
The shader snippet from section above could be rewritten in DX11-style HLSL syntax, and it would do the same thing:
Texture2D _MainTex; SamplerState sampler_MainTex; // "sampler" + “_MainTex” // ... half4 color = _MainTex.Sample(sampler_MainTex, uv);
However, this way, a shader could be written to “reuse” samplers from other textures, while sampling more than one texture. In the example below, three textures are sampled, but only one sampler is used for all of them:
Texture2D _MainTex; Texture2D _SecondTex; Texture2D _ThirdTex; SamplerState sampler_MainTex; // "sampler" + “_MainTex” // ... half4 color = _MainTex.Sample(sampler_MainTex, uv); color += _SecondTex.Sample(sampler_MainTex, uv); color += _ThirdTex.Sample(sampler_MainTex, uv);
Unity provides several shader macros to help with declaring and sampling textures using this “separate samplers” approach, see built-in macros. The example above could be rewritten this way, using said macros:
UNITY_DECLARE_TEX2D(_MainTex); UNITY_DECLARE_TEX2D_NOSAMPLER(_SecondTex); UNITY_DECLARE_TEX2D_NOSAMPLER(_ThirdTex); // ... half4 color = UNITY_SAMPLE_TEX2D(_MainTex, uv); color += UNITY_SAMPLE_TEX2D_SAMPLER(_SecondTex, _MainTex, uv); color += UNITY_SAMPLE_TEX2D_SAMPLER(_ThirdTex, _MainTex, uv);
The above would compile on all platforms supported by Unity, but would fallback to using three samplers on older platforms like DX9.
In addition to recognizing HLSL SamplerState objects named as “sampler”+TextureName, Unity also recognizes some other patterns in sampler names. This is useful for declaring simple hardcoded sampling states directly in the shaders. An example:
Texture2D _MainTex; SamplerState my_point_clamp_sampler; // ... half4 color = _MainTex.Sample(my_point_clamp_sampler, uv);
The name “my_point_clamp_sampler” will be recognized as a sampler that should use Point (nearest) texture filtering, and Clamp texture wrapping mode.
Sampler names recognized as “inline” sampler states (all case insensitive):
“Point”, “Linear” or “Trilinear” (required) set up texture filtering mode.
“Clamp”, “Repeat”, “Mirror” or “MirrorOnce” (required) set up texture wrap mode.
“Compare” (optional) set up sampler for depth comparison; use with HLSL SamplerComparisonState type and SampleCmp / SampleCmpLevelZero functions.
“AnisoX” (where X can be 2/4/8 or 16, for example, Aniso8
) can be added to request anisotropic filtering.
Here’s an example of sampling texture with sampler_linear_repeat
and sampler_point_repeat
SamplerStates respectively, illustrating how the name controls filtering mode:
Here’s an example with SmpClampPoint
, SmpRepeatPoint
, SmpMirrorPoint
, SmpMirrorOncePoint
, Smp_ClampU_RepeatV_Point
SamplerStates respectively, illustrating how the name controls wrapping mode. In the last example, different wrap modes are set for horizontal (U) and vertical (V) axes. In all cases, texture coordinates go from –2.0 to +2.0.
Just like separate texture + sampler syntax, inline sampler states are not supported on some platforms. Currently they are implemented on Direct3D 11/12 and Metal.
Note that “MirrorOnce” texture wrapping mode is not supported on most mobile GPUs/APIs, and will fallback to Mirror mode when support is not present.
Note that the “AnisoX” filtering modes are a best effort based on the platform capabilities and selected API. The actual value will be clamped based on the maximum supported anisotropic level (including disabled in cases where no anisotropic filtering is supported).
Macro: | Use: |
---|---|
UNITY_DECLARE_TEX2D(name) |
Declares a Texture and Sampler pair. |
UNITY_DECLARE_TEX2D_NOSAMPLER(name) |
Declares a Texture without a Sampler. |
UNITY_DECLARE_TEX2DARRAY(name) |
Declares a Texture array Sampler variable. |
UNITY_SAMPLE_TEX2D(name,uv) |
Sample from a Texture and Sampler pair, using given Texture coordinate. |
UNITY_SAMPLE_TEX2D_SAMPLER( name,samplername,uv) |
Sample from Texture (name), using a Sampler from another Texture (samplername). |
UNITY_SAMPLE_TEX2DARRAY(name,uv) |
Sample from a Texture array with a float3 UV; the z component of the coordinate is array element index. |
UNITY_SAMPLE_TEX2DARRAY_LOD(name,uv,lod) |
Sample from a Texture array with an explicit mipmap level. |
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.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.