Sample Texture 2D node
The Sample Texture 2D node samples a Texture 2D asset and returns a Vector 4 color value. You can specify the UV coordinates for a texture sample and use a Sampler State node to define a specific Sampler State.
A Sample Texture 2D node can also sample a normal map. For more information, see the Controls section, or Normal map (Bump mapping) in the Unity User manual.
Note
If you experience texture sampling errors for this node in a graph with Custom Function Nodes or Sub Graphs, upgrade to Shader Graph version 10.3 or later.
Create Node menu category
The Sample Texture 2D node is under the Input > Texture category in the Create Node menu.
Compatibility
The Sample Texture 2D node is supported on the following render pipelines:
Built-In Render Pipeline | Universal Render Pipeline (URP) | High Definition Render Pipeline (HDRP) |
---|---|---|
Yes | Yes | Yes |
With its default settings, this node can only connect to a Block node in the Fragment Context of a Shader Graph. To sample a Texture for the Vertex Context of a Shader Graph, set the Mip Sampling Mode to LOD.
Inputs
The Sample Texture 2D node has the following input ports:
[!include[nodes-sample-uv-table](./snippets/sample-nodes/nodes-sample-uv-table.md)] [!include[nodes-sample-ss-table](./snippets/sample-nodes/nodes-sample-ss-table.md)] [!include[nodes-sample-lod-table](./snippets/sample-nodes/nodes-sample-lod-table.md)] [!include[nodes-sample-mip-bias-table](./snippets/sample-nodes/nodes-sample-mip-bias-table.md)] [!include[nodes-sample-ddx-table](./snippets/sample-nodes/nodes-sample-ddx-table.md)] [!include[nodes-sample-ddy-table](./snippets/sample-nodes/nodes-sample-ddy-table.md)]Name | Type | Binding | Description |
---|---|---|---|
Texture | Texture 2D | None | The Texture 2D asset to sample. |
Controls
The Sample Texture 2D node has the following controls:
Name | Type | Description | |
---|---|---|---|
Type | Dropdown | Select whether the texture is a Texture asset or a normal map. | |
Default | The texture is a Texture asset. | ||
Normal | The texture is a normal map. | ||
Space | Dropdown | When the node's Type is Normal to use a texture as a normal map, choose the Space for the normal map. | |
Tangent | Use a Tangent normal map whenever the mesh for a geometry needs to deform or change, such as when animating a character. With Tangent Space, the normal map's normals are relative to the existing vertex normals of any geometry rendered with your Shader Graph. Your Shader Graph only adjusts the vertex normals and not override them. | ||
Object | Use an Object normal map whenever the mesh for a geometry is static and doesn't deform. With Object Space, the normal map's normals are explicit and override the normals of any geometry rendered with your Shader Graph. Because a static mesh's normals never change, an Object normal map also maintains consistent lighting across different levels of detail (LODs). For more information about normal maps, see Normal map (Bump mapping) in the User manual. |
Additional node settings
The Sample Texture 2D node has some additional settings that you can access from the Graph Inspector:
Name | Type | Description | |
---|---|---|---|
Use Global Mip Bias | Toggle | Enable Use Global Mip Bias to use the render pipeline's Global Mip Bias. This bias adjusts the percentage of texture information taken from a specific mip when sampling. For more information on mip bias, see Mipmaps introduction in the Unity User Manual. | |
Enabled | Shader Graph uses the render pipeline's Global Mip Bias to adjust the texture information taken when sampling. | ||
Disabled | Shader Graph doesn't use the render pipeline's Global Mip Bias to adjust texture information when sampling. | ||
Mip Sampling Mode | Dropdown | Choose the sampling mode to use to calculate the mip level of the texture. | |
Standard | The render pipeline calculates and automatically selects the mip for the texture. | ||
LOD | The render pipeline lets you set an explicit mip for the texture on the node. The texture will always use this mip, regardless of the DDX or DDY calculations between pixels. Set the Mip Sampling Mode to LOD to connect the node to a Block node in the Vertex Context. For more information on Block nodes and Contexts, see Master Stack. | ||
Gradient | The render pipeline lets you set the DDX and DDY values to use for its mip calculation, instead of using the values calculated from the texture's UV coordinates. For more information on DDX and DDY values, see Mipmaps introduction in the User Manual. | ||
Bias | The render pipeline lets you set a bias to adjust the calculated mip for a texture up or down. Negative values bias the mip to a higher resolution. Positive values bias the mip to a lower resolution. The render pipeline can add this value to the value of the Global Mip Bias, or use this value instead of its Global Mip Bias. For more information on mip bias, see Mipmaps introduction in the User Manual. |
Outputs
The Sample Texture 2D node has the following output ports:
Name | Type | Description |
---|---|---|
RGBA | Vector 4 | The full RGBA Vector 4 color value of the texture sample. |
R | Float | The Red channel or X component of the texture sample. |
G | Float | The Green channel or Y component of the texture sample. |
B | Float | The Blue channel or Z component of the texture sample. |
A | Float | The Alpha channel or W component of the texture sample. |
Example graph usage
In the following example, the Sample Texture 2D node uses a Subgraph node that generates UV coordinates in latitude and longitude format. These latitude and longitude UV coordinates help render the latlong_test 2D Texture asset, which was created and formatted with a latitude and longitude projection. The generated latitude and longitude UVs accurately map the 2D Texture asset onto a spherical geometry.
If the Sample Texture 2D node uses the Standard Mip Sampling Mode, the Texture displays with a seam along the side of the sphere where the left and right sides of the texture meet. The latitude and longitude UV coordinates for sampling the texture jump from 0
to 1
at the seam on the model, which causes a problem with the mip level calculation in the sample. The error in the mip level calculation causes the seam. The texture requires a different mip sampling mode to remove the seam.
When the Mip Sampling Mode is set to Gradient, the Sample Texture 2D node can use the standard set of UVs for the model in the mip level calculation, instead of the latitude and longitude UVs needed for sampling the texture. The new UV coordinates passed into the DDX and DDY input ports result in a continuous mip level, and remove the seam.
Generated code example
The following code represents this node in Unity's shader code, based on the selected Type on the Sample Texture 2D node:
Default
float4 _SampleTexture2D_RGBA = SAMPLE_TEXTURE2D(Texture, Sampler, UV);
float _SampleTexture2D_R = _SampleTexture2D_RGBA.r;
float _SampleTexture2D_G = _SampleTexture2D_RGBA.g;
float _SampleTexture2D_B = _SampleTexture2D_RGBA.b;
float _SampleTexture2D_A = _SampleTexture2D_RGBA.a;
Normal
float4 _SampleTexture2D_RGBA = SAMPLE_TEXTURE2D(Texture, Sampler, UV);
_SampleTexture2D_RGBA.rgb = UnpackNormalmapRGorAG(_SampleTexture2D_RGBA);
float _SampleTexture2D_R = _SampleTexture2D_RGBA.r;
float _SampleTexture2D_G = _SampleTexture2D_RGBA.g;
float _SampleTexture2D_B = _SampleTexture2D_RGBA.b;
float _SampleTexture2D_A = _SampleTexture2D_RGBA.a;
Related nodes
The following nodes are related or similar to the Sample Texture 2D node: