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):
- 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"
}