Client LuaCsForBarotrauma
NoiseDeformation.cs
1 using Microsoft.Xna.Framework;
2 using System.Xml.Linq;
3 
5 {
7  {
8  [Serialize(0.0f, IsPropertySaveable.Yes, description: "The frequency of the noise."), Editable(MinValueFloat = 0.0f, MaxValueFloat = 100.0f, DecimalCount = 2, ValueStep = 1f)]
9  public override float Frequency { get; set; }
10 
11  [Serialize(1.0f, IsPropertySaveable.Yes, description: "How much the noise distorts the sprite."), Editable(MinValueFloat = 0.0f, MaxValueFloat = 10.0f, DecimalCount = 2, ValueStep = 0.01f)]
12  public float Amplitude { get; set; }
13 
14  [Serialize(0.0f, IsPropertySaveable.Yes, description: "How fast the noise changes."), Editable(MinValueFloat = 0.0f, MaxValueFloat = 10.0f, DecimalCount = 2, ValueStep = 0.01f)]
15  public float ChangeSpeed { get; set; }
16 
17  public NoiseDeformationParams(XElement element) : base(element)
18  {
19  }
20  }
21 
23  {
25 
26  private float phase;
27 
28  public NoiseDeformation(XElement element) : base(element, new NoiseDeformationParams(element))
29  {
30  phase = Rand.Range(0.0f, 255.0f);
31  UpdateNoise();
32  }
33 
34  private void UpdateNoise()
35  {
36  for (int x = 0; x < Resolution.X; x++)
37  {
38  float normalizedX = x / (float)(Resolution.X - 1) * NoiseDeformationParams.Frequency;
39  for (int y = 0; y < Resolution.Y; y++)
40  {
41  float normalizedY = y / (float)(Resolution.X - 1) * NoiseDeformationParams.Frequency;
42 
43  Deformation[x, y] = new Vector2(
44  PerlinNoise.GetPerlin(normalizedX + phase, normalizedY + phase) - 0.5f,
45  PerlinNoise.GetPerlin(normalizedY - phase, normalizedX - phase) - 0.5f);
46  }
47  }
48  }
49 
50  protected override void GetDeformation(out Vector2[,] deformation, out float multiplier, bool inverse)
51  {
52  deformation = Deformation;
54  }
55 
56  public override void Update(float deltaTime)
57  {
59  {
60  phase += deltaTime * NoiseDeformationParams.ChangeSpeed / 100.0f;
61  phase %= 1.0f;
62  UpdateNoise();
63  }
64  }
65  }
66 }
override void GetDeformation(out Vector2[,] deformation, out float multiplier, bool inverse)