Access TextMesh Pro Texture Tiling and Offset how?

546 Views Asked by At

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.

2

There are 2 best solutions below

6
On BEST ANSWER

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

0
On

Thanks to the incomparable derHugo, the resultant code works perfectly, in both directions:

using TMPro;
using UnityEngine;

public class testMaterailProps : MonoBehaviour {
    public Vector2 FaceTiling;
    public Vector2 FaceOffset;
    public Vector2 OutLineTiling;
    public Vector2 OutlineOffset;
    public Material myFontMaterial;
    TextMeshPro myTexmMeshPro;
    static readonly int FaceTex = Shader.PropertyToID( "_FaceTex" );
    static readonly int OutlineTex = Shader.PropertyToID( "_OutlineTex" );

    void Start ( ) {
        myTexmMeshPro = GetComponent<TextMeshPro>( );
        myFontMaterial = myTexmMeshPro.fontSharedMaterial;
        FaceTiling = myFontMaterial.GetTextureScale( FaceTex );
        FaceOffset = myFontMaterial.GetTextureOffset( FaceTex );
        OutLineTiling = myFontMaterial.GetTextureScale( OutlineTex );
        OutlineOffset = myFontMaterial.GetTextureOffset( OutlineTex );
    }

    void OnValidate ( ) {
        myFontMaterial.SetTextureScale( FaceTex, FaceTiling );
        myFontMaterial.SetTextureOffset( FaceTex, FaceOffset );

        myFontMaterial.SetTextureScale( OutlineTex, OutLineTiling );
        myFontMaterial.SetTextureOffset( OutlineTex, OutlineOffset );
    }
}

Making it possible to copy text styles accurately from one text object to another, since a bug in the copy/paste of Unity properties prevents these values being copy-able through the Editor UI...

enter image description here