0

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.

derHugo
  • 83,094
  • 9
  • 75
  • 115
Confused
  • 6,048
  • 6
  • 34
  • 75
  • Face: Controls the appearance of the text face. Outline: Controls the appearance of the text outline. – thunderkill Jan 09 '23 at 07:59
  • try this [access shader via code](https://docs.unity3d.com/ru/2019.4/Manual/MaterialsAccessingViaScript.html) – thunderkill Jan 09 '23 at 08:00
  • [shader documentation](https://docs.unity3d.com/Packages/com.unity.textmeshpro@3.2/manual/ShadersDistanceField.html) the code should be something like this `m_sharedMaterial.SetFloat(ShaderUtilities.ID_FaceDilate,_0.1f);` – thunderkill Jan 09 '23 at 08:02
  • As said in the question, using the traditional means of accessing shader values within Unity doesn't work for these two parameters. They have their own special section of the Shader, which is quite difficult to find how to access... for me. – Confused Jan 09 '23 at 08:10

2 Answers2

1

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

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • I think he said he tried it out and didn't work see the comments above – thunderkill Jan 09 '23 at 08:39
  • 1
    @thunderkill I think OP didn't know about `SetTextureScale` and `SetTextureOffset` and that they are used via the same property name as the texture itself ;) – derHugo Jan 09 '23 at 08:42
  • let him try it out may be it will work for him – thunderkill Jan 09 '23 at 08:43
  • @derHugo exactly correct, I'd skimmed over these thinking they were for `MainTex` modification and merely had a quick wonder as to why they were seemingly redundant given there's set and get for that, explicitly, too. @thunderkill, keep eye on derHugo as he is, quite literally, a legend with truly profound insights and testing abilities (and patience) within Unity. – Confused Jan 09 '23 at 09:58
  • Similar to above, do you happen to know how to access the Distortion Strength of an Unlit Particle Shader? I can't find/get/set what I'm supposed to change to animate this value. – Confused Feb 23 '23 at 10:10
  • According to [this source](https://github.com/TwoTailsGames/Unity-Built-in-Shaders/blob/master/DefaultResourcesExtra/Particle%20Standard%20Unlit.shader) (not sure how reliable though) that should be simply `SetFloat("_DistortionStrength", value)` .. might need to also enable first `SetFloat("_DistortionEnabled", 1)` though .. – derHugo Feb 23 '23 at 10:30
1

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

Confused
  • 6,048
  • 6
  • 34
  • 75