简体   繁体   中英

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.

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

在此处输入图像描述

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

在此处输入图像描述

=> 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));

在此处输入图像描述

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

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

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

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...

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM