namespace GFramework.Godot.Text.Effects;
///
/// 为文本提供抖动效果。
///
///
/// 默认注册表会在每次宿主刷新时为该效果创建独立实例。
/// 内部会复用并修改 的 Seed,因此该类型假定 Godot 在主线程顺序
/// 执行字符效果,不支持跨多个 共享同一实例,也不保证并发调用下的线程安全。
///
[GlobalClass]
[Tool]
public partial class RichTextJitterEffect : RichTextEffectBase
{
private readonly bool _animatedEffectsEnabled;
private readonly FastNoiseLite _noise;
///
/// 初始化抖动效果。
///
/// 是否允许动态效果实际生效。
public RichTextJitterEffect(bool animatedEffectsEnabled = true)
{
_animatedEffectsEnabled = animatedEffectsEnabled;
_noise = new FastNoiseLite
{
NoiseType = FastNoiseLite.NoiseTypeEnum.Perlin,
FractalOctaves = 8,
FractalGain = 0.8f
};
}
///
/// 获取标签名。
///
protected override string TagName => "jitter";
///
/// 应用抖动位移。
///
/// 当前字符上下文。
/// 始终返回 。
public override bool _ProcessCustomFX(CharFXTransform charFx)
{
if (!_animatedEffectsEnabled)
{
return true;
}
var amplitude = GetFloat(charFx, "amplitude", 3.0f);
var speed = GetFloat(charFx, "speed", 600.0f);
_noise.Seed = (charFx.RelativeIndex + 1) * 131;
var x = _noise.GetNoise1D((float)charFx.ElapsedTime * speed);
_noise.Seed = (charFx.RelativeIndex + 1) * 737;
var y = _noise.GetNoise1D((float)charFx.ElapsedTime * speed);
charFx.Offset += new Vector2(x, y) * amplitude;
ApplyVisibility(charFx);
return true;
}
}