0
\$\begingroup\$

I have a Unity GameObject with a TextMeshPro component (let's call this object "White Letter"). The Font Material is using the "TextMeshPro/Distance Field" shader. This works perfectly.

I'd like to make the White Letter "wiggle" a bit using a shader (see below). I'd like the effect to look something like this (made using After Effects):

https://www.youtube.com/watch?v=HUmzcU8llPQ

Here's how I combined my Font Material together with this WiggleTextDF Shader:

  • I created a WiggleTextDF shader.
  • I created a WiggleTextDF Material and set the shader to WiggleTextDF shader.
  • I added a Displacement Map and Texture to the WiggleTextDF Material (here they are):

enter image description hereenter image description here

  • Finally, I applied this Material (as Element 0) to the Mesh Renderer for the White Letter GameObject.

Every time I do that last step... the font stops rendering and the TextMeshPro renders as a pink box.

Any advice on how to move forward?

Shader "WiggleTextDF" {
Properties {
    _MainTex ("Texture", 2D) = "white" {}
    _DisplacementMap ("Displacement Map", 2D) = "white" {}
    _WiggleAmount ("Wiggle Amount", Range(0, 1)) = 0.1
    _WiggleSpeed ("Wiggle Speed", Range(0, 10)) = 5
}
SubShader {
    Tags {"Queue"="Transparent" "RenderType"="Transparent"}
    Pass {
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"
        #include "UnityTextMeshPro.cginc"

        struct appdata {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
            UNITY_VERTEX_INPUT_INSTANCE_ID
        };

        struct v2f {
            float2 uv : TEXCOORD0;
            UNITY_VERTEX_OUTPUT_STEREO
            float4 vertex : SV_POSITION;
        };

        sampler2D _MainTex;
        sampler2D _DisplacementMap;
        float _WiggleAmount;
        float _WiggleSpeed;

        v2f vert (appdata v) {
            v2f o;
            UNITY_SETUP_INSTANCE_ID(v);
            UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
            o.vertex = UnityObjectToClipPos(v.vertex);
            o.uv = v.uv;
            return o;
        }

        fixed4 frag (v2f i) : SV_Target {
            UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
            UNITY_SETUP_INSTANCE_ID(i);
            // Sample the displacement map texture to get the displacement value
            float4 displacement = tex2D(_DisplacementMap, i.uv);

            // Calculate the wiggle offset based on the displacement value and time
            float2 wiggleOffset = sin(_Time.y * _WiggleSpeed + i.uv * 10) * displacement.rg * _WiggleAmount;

            // Apply the offset to the texture coordinates
            float2 uv = i.uv + wiggleOffset;

            // Sample the main texture using the offset texture coordinates
            fixed4 col = tex2D(_MainTex, uv);

            UNITY_APPLY_FOG_COLOR(i.fogCoord, col);
            return UNITY_PREPARE_FINAL_COLOR(col);
        }
        ENDCG
    }
}
FallBack "TextMeshPro/Distance Field"

}

\$\endgroup\$
5
  • \$\begingroup\$ How did you try combining these shaders? \$\endgroup\$
    – DMGregory
    May 10 at 13:45
  • \$\begingroup\$ This is my first project involving shaders, and I don't know what I'm doing. I created a Wiggle Material using the new Shader, and then dragged the Font Material onto the Wiggle Material. Then I dragged the Wiggle Material onto the GameObject with the TextMeshPro component. \$\endgroup\$
    – kanamekun
    May 10 at 13:57
  • \$\begingroup\$ Dragging a material onto a material isn't really a thing in Unity. You can drag a shader into a material, or a material onto an object with a renderer, but materials don't compose by dragging, so I'm a bit confused by what you're describing. \$\endgroup\$
    – DMGregory
    May 10 at 14:10
  • \$\begingroup\$ I am also confused, any advice or help would be appreciated! :D \$\endgroup\$
    – kanamekun
    May 10 at 14:10
  • \$\begingroup\$ @DMGregory I added some more details on the exact steps I took try and apply the new shader to a new material. \$\endgroup\$
    – kanamekun
    May 12 at 8:53

0

You must log in to answer this question.

Browse other questions tagged .