Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NoiseHelper.cs
Go to the documentation of this file.
1using System;
4
6
7public static class NoiseHelper
8{
9 private const int RANDOM_SEED = 1;
10
11 private const int NOISE_2D_SIZE = 32;
12
13 private const int NOISE_2D_SIZE_MASK = 31;
14
15 private const int NOISE_SIZE_MASK = 1023;
16
17 private static readonly float[] StaticNoise = CreateStaticNoise(1024);
18
19 private static float[] CreateStaticNoise(int length)
20 {
22 float[] array = new float[length];
23 for (int i = 0; i < array.Length; i++)
24 {
25 array[i] = r.NextFloat();
26 }
27 return array;
28 }
29
30 public static float GetDynamicNoise(int index, float currentTime)
31 {
32 float num = StaticNoise[index & 0x3FF];
33 float num2 = currentTime % 1f;
34 return Math.Abs(Math.Abs(num - num2) - 0.5f) * 2f;
35 }
36
37 public static float GetStaticNoise(int index)
38 {
39 return StaticNoise[index & 0x3FF];
40 }
41
42 public static float GetDynamicNoise(int x, int y, float currentTime)
43 {
44 return GetDynamicNoiseInternal(x, y, currentTime % 1f);
45 }
46
47 private static float GetDynamicNoiseInternal(int x, int y, float wrappedTime)
48 {
49 x &= 0x1F;
50 y &= 0x1F;
51 return Math.Abs(Math.Abs(StaticNoise[y * 32 + x] - wrappedTime) - 0.5f) * 2f;
52 }
53
54 public static float GetStaticNoise(int x, int y)
55 {
56 x &= 0x1F;
57 y &= 0x1F;
58 return StaticNoise[y * 32 + x];
59 }
60
61 public static float GetDynamicNoise(Vector2 position, float currentTime)
62 {
63 position *= 10f;
64 currentTime %= 1f;
65 Vector2 vector = new Vector2((float)Math.Floor(position.X), (float)Math.Floor(position.Y));
66 Point point = new Point((int)vector.X, (int)vector.Y);
67 Vector2 vector2 = new Vector2(position.X - vector.X, position.Y - vector.Y);
68 float value = MathHelper.Lerp(GetDynamicNoiseInternal(point.X, point.Y, currentTime), GetDynamicNoiseInternal(point.X, point.Y + 1, currentTime), vector2.Y);
69 float value2 = MathHelper.Lerp(GetDynamicNoiseInternal(point.X + 1, point.Y, currentTime), GetDynamicNoiseInternal(point.X + 1, point.Y + 1, currentTime), vector2.Y);
70 return MathHelper.Lerp(value, value2, vector2.X);
71 }
72
73 public static float GetStaticNoise(Vector2 position)
74 {
75 position *= 10f;
76 Vector2 vector = new Vector2((float)Math.Floor(position.X), (float)Math.Floor(position.Y));
77 Point point = new Point((int)vector.X, (int)vector.Y);
78 Vector2 vector2 = new Vector2(position.X - vector.X, position.Y - vector.Y);
79 float value = MathHelper.Lerp(GetStaticNoise(point.X, point.Y), GetStaticNoise(point.X, point.Y + 1), vector2.Y);
80 float value2 = MathHelper.Lerp(GetStaticNoise(point.X + 1, point.Y), GetStaticNoise(point.X + 1, point.Y + 1), vector2.Y);
81 return MathHelper.Lerp(value, value2, vector2.X);
82 }
83}
static float Lerp(float value1, float value2, float amount)
Definition MathHelper.cs:53
static double Abs(double value)
static double Floor(double d)
static readonly float[] StaticNoise
static float GetStaticNoise(int x, int y)
static float GetStaticNoise(int index)
static float GetDynamicNoise(int x, int y, float currentTime)
static float GetDynamicNoise(int index, float currentTime)
static float[] CreateStaticNoise(int length)
static float GetDynamicNoise(Vector2 position, float currentTime)
static float GetDynamicNoiseInternal(int x, int y, float wrappedTime)
static float GetStaticNoise(Vector2 position)