Search code examples
c#unity-game-enginepropertiesshadertextmeshpro

Access TextMesh Pro Texture Tiling and Offset how?


TextMesh Pro shaders have two unusual facilities for adjusting the Textures used for both the Face and the Outline: Tiling and Offset.

They're not accessible via the normal ways of using keywords to access shader properties in Unity.

Is it possible to access these properties from Monobehaviours? How?

If you're wanting to see sample code... there's little point... as I've tried all the normal ways of accessing shader properties in Unity and found none of them work, all throwing errors relating to symbols not existing. Or returning nulls.

These properties are somewhat nested, somehow.

If you've ever successfully edited these values with a Script in Unity, you'll likely know that they're a bit different.


Within the Shader files for TextMesh Pro, these values are known as:

        float4 _FaceTex_ST;
        float4 _OutlineTex_ST;

Note, the .x and .y of these float4 are the scaling/tiling along their respective axis, whilst .z and .w are used for their offsets.


Solution

  • Depending a bit on which shader exactly you use - for now assuming one of the built-in ones like e.g. TextMeshPro/Distance Field (Surface) you can search for the shader e.g. in Assets/TextMeshPro/Shaders, select the Shader and now see which properties are exposed in the Inspector.

    enter image description here

    In that case it would be the _FaceTex texture.

    Now the Tiling and Offset are indeed quite tricky since they store those directly along with the Texture property itself! You can see his when setting the Inspector to Debug mode with the TextMeshPro selected

    enter image description here

    => You want to use Material.SetTextureOffset and Material.SetTextureScale (= Tiling) with the property name of the texture itself

    yourTextMeshPro.fontMaterial.SetTextureScale("_FaceTex", new Vector2(42, 42));
    yourTextMeshPro.fontMaterial.SetTextureOffset("_FaceTex", new Vector2(123, 456));
    

    enter image description here

    The Tiling and Offset have no effect for the Outline apparently. See e.g. Distance Field (Surface) Shader.

    Outline
    ...
    Tiling: ????
    Offset: ????

    You could still try though and do the same just with _OutlineTex