PostProcessing/WobbleEffect.cs

A post-processing effect component that applies a screen-space wobble/ripple using a custom shader. It exposes Frequency, Amplitude and Speed properties, computes a weighted amplitude, sets shader attributes, creates a material from a shader path and blits it before post-process stage.

Native Interop
using Sandbox;
using Sandbox.Rendering;

namespace RedSnail.WaterTool2;

/// <summary>
/// A gentle screen-space ripple: the view wavering under the water.
/// </summary>
[Title("Wobble Effect")]
[Category("Post Processing")]
[Icon("waves")]
public sealed class WobbleEffect : BasePostProcess<WobbleEffect>
{
	public const string ShaderPath = "watertool2/shaders/pp_wobble.shader";

	[Property, Range(0.1f, 100.0f)] private float Frequency { get; set; } = 20.0f;
	[Property, Range(0.1f, 10.0f)] private float Amplitude { get; set; } = 1.0f;
	[Property, Range(0.1f, 10.0f)] private float Speed { get; set; } = 1.0f;



	public override void Render()
	{
		float amplitude = GetWeighted(x => x.Amplitude);

		if (amplitude.AlmostEqual(0.0f))
			return;

		Attributes.Set("Frequency", Frequency);
		Attributes.Set("Amplitude", amplitude);
		Attributes.Set("Speed", Speed);

		Material shader = Material.FromShader(ShaderPath);
		BlitMode blit = BlitMode.WithBackbuffer(shader, Stage.BeforePostProcess, 70);
		Blit(blit, "Wobble Effect");
	}
}