I am trying to obtain the UV tiling and offset in a surface shader. I’m referring to the standard variables you see in the material inspector when you add a texture property to your shader:
_MainTex(“Base (RGB) Gloss (A)”, 2D) = “white” {}
I have seen plenty of examples and discussion about how to use _MainTex_ST and the TRANSFORM_TEX macro, but that seems to only be useful in vertex and fragment shaders, not Unity’s magic Surface Shaders. You can declare UV coordinates as input to the Surface Shader, but that only seems to include UV, not tiling and offset. I looked at the complied code from the surface shader, and it seems to pack the UVs into the xy and the bump map UVs into the zw of a “pack” variable, with no tiling or offset.
I have also seen suggestions to create your own uv adjustment variable, and modify the UVs via that. I’m sure that works, but it bypasses the tiling and offset variables that are visible in the inspector.
Any suggestions as to how I can obtain the tiling and offset variables from the material inspector in a surface shader?
"Materials often have Tiling and Offset fields for their texture properties. This information is passed into shaders in a float4 {TextureName}_ST property
…
For example, if a shader contains texture named _MainTex, the tiling information will be in a _MainTex_ST vector."
from:
But I 'm not sure I understand what you mean about passing this in as a parameter to a surface shader. Though the UV coordinates WILL change from texel to texel, the offsets and tiling values should remain constant: Therefore I see no need to pass them as a parameters. Accessing them from the builtin variable mentioned above should be sufficient.
Here is an example using those variables in a surface shader. My usual method when figuring stuff out, or debugging shaders, is to use the output colors to represent the data I want to look at. Once this sample shader is attached to a material, and that to an object; you can change the material titling and offset, and watch color of objects follow the change, to show your data. (clearly, it does not use the actual texture to render.):