1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
25 EffectParameter weightsParameter =
Effect.Parameters[
"SampleWeights"];
26 EffectParameter offsetsParameter =
Effect.Parameters[
"SampleOffsets"];
29 int sampleCount = weightsParameter.Elements.Count;
32 float[] sampleWeights =
new float[sampleCount];
33 Vector2[] sampleOffsets =
new Vector2[sampleCount];
35 sampleWeights[0] = ComputeGaussian(0);
36 sampleOffsets[0] =
new Vector2(0);
38 float totalWeights = sampleWeights[0];
42 for (
int i = 0; i < sampleCount / 2; i++)
45 float weight = ComputeGaussian(i + 1);
47 sampleWeights[i * 2 + 1] = weight;
48 sampleWeights[i * 2 + 2] = weight;
50 totalWeights += weight * 2;
60 float sampleOffset = i * 2 + 1.5f;
62 Vector2 delta =
new Vector2(dx, dy) * sampleOffset;
65 sampleOffsets[i * 2 + 1] = delta;
66 sampleOffsets[i * 2 + 2] = -delta;
70 for (
int i = 0; i < sampleWeights.Length; i++)
72 sampleWeights[i] /= totalWeights;
75 weightsParameter.SetValue(sampleWeights);
76 offsetsParameter.SetValue(sampleOffsets);
84 float ComputeGaussian(
float n)
88 return (
float)((1.0 / Math.Sqrt(2 * Math.PI * theta)) *
89 Math.Exp(-(n * n) / (2 * theta * theta)));
void SetParameters(float dx, float dy)
Computes sample weightings and texture coordinate offsets for one pass of a separable gaussian blur f...
BlurEffect(Effect effect, float dx, float dy)