-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.cs
246 lines (224 loc) · 10.9 KB
/
script.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Collections.Generic;
public class ColoredWorleyNoise {
public enum DistanceCalculationMethod { EuclideanDistance = 0, ManhattanDistance = 1, ChebyshevDistance = 2, MinkowskiDistance = 3 }
readonly int gridSize;
readonly float halfGridSize;
readonly int amountOfColor;
Dictionary<Vector2Int, ColoredWorleyNoisePoint> pointHash = new();
readonly double[] euclideanDistanceThresholds, manhattanDistanceThresholds, chebyshevDistanceThresholds;
readonly Vector2Int[][] allOffset = new Vector2Int[][] {
//Side
new Vector2Int[] {
new Vector2Int(x: 1, y: 0),
new Vector2Int(x: -1, y: 0),
new Vector2Int(x: 0, y: 1),
new Vector2Int(x: 0, y: -1)
},
//Corner
new Vector2Int[] {
new Vector2Int(x: 1, y: 1),
new Vector2Int(x: -1, y: -1),
new Vector2Int(x: -1, y: 1),
new Vector2Int(x: 1, y: -1)
},
};
public ColoredWorleyNoise(int gridSize, int amountOfColor) {
this.gridSize = gridSize;
halfGridSize = gridSize / 2;
this.amountOfColor = amountOfColor;
euclideanDistanceThresholds = new double[] { halfGridSize, Math.Pow(halfGridSize, 2) * 2 };
manhattanDistanceThresholds = new double[] { halfGridSize, gridSize };
chebyshevDistanceThresholds = new double[] { halfGridSize, halfGridSize };
}
public int GetSampleColoredWorleyNoise(float x, float y,
DistanceCalculationMethod distanceCalculationMethod,
uint p = 0) {
if (p == 0 && distanceCalculationMethod == DistanceCalculationMethod.MinkowskiDistance) {
throw new Exception("p must be bigger than 0");
}
int gridX = CalculateChunkPos(x, halfGridSize),
gridY = CalculateChunkPos(y, halfGridSize);
Vector2Int centerKey = new(gridX, gridY);
x -= gridX * gridSize; y -= gridY * gridSize;
if (distanceCalculationMethod == DistanceCalculationMethod.EuclideanDistance) {
return EuclideanDistance(centerKey, x, y);
} else if (distanceCalculationMethod == DistanceCalculationMethod.ManhattanDistance || (distanceCalculationMethod == DistanceCalculationMethod.MinkowskiDistance && p == 1)) {
return ManhattanDistance(centerKey, x, y);
} else if (distanceCalculationMethod == DistanceCalculationMethod.ChebyshevDistance || (distanceCalculationMethod == DistanceCalculationMethod.MinkowskiDistance && p == 2)) {
return ChebyshevDistance(centerKey, x, y);
// Fuck the Minkowski distance.
} else return MinkowskiDistance(centerKey, x, y, p);
}
int EuclideanDistance(Vector2Int centerKey, double x, double y) {
if (!pointHash.ContainsKey(centerKey)) {
pointHash.Add(centerKey, new(centerKey.x, centerKey.y, gridSize, gridSize, amountOfColor));
}
ColoredWorleyNoisePoint centerPoint = pointHash[centerKey];
List<int> colorContender = new() { centerPoint.colorIndex };
double shortestDistance = Math.Pow(centerPoint.GetX(0) - x, 2) + Math.Pow(centerPoint.GetY(0) - y, 2);
double distance;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < allOffset[i].Length; j++) {
Vector2Int key = centerKey + allOffset[i][j];
int xOffset = allOffset[i][j].x, yOffset = allOffset[i][j].y;
if (!pointHash.ContainsKey(key)) {
pointHash.Add(key, new(key.x, key.y, gridSize, gridSize, amountOfColor));
}
ColoredWorleyNoisePoint point = pointHash[key];
distance = Math.Pow(point.GetX(xOffset) - x, 2)
+ Math.Pow(point.GetY(yOffset) - y, 2);
if (distance > shortestDistance) { continue; }
if (distance == shortestDistance) { colorContender.Add(point.colorIndex); continue; }
colorContender = new List<int>() { point.colorIndex };
shortestDistance = distance;
}
if (euclideanDistanceThresholds[i] > shortestDistance) {
break;
}
}
return colorContender[new System.Random((centerKey.x << 16) | (centerKey.x & 0xFFFF)).Next(0, colorContender.Count)];
}
int ManhattanDistance(Vector2Int centerKey, double x, double y) {
if (!pointHash.ContainsKey(centerKey)) {
pointHash.Add(centerKey, new(centerKey.x, centerKey.y, gridSize, gridSize, amountOfColor));
}
ColoredWorleyNoisePoint centerPoint = pointHash[centerKey];
List<int> colorContender = new() { centerPoint.colorIndex };
double shortestDistance = Math.Abs(centerPoint.GetX(0) - x)
+ Math.Abs(centerPoint.GetY(0) - y);
double distance;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < allOffset[i].Length; j++) {
Vector2Int key = centerKey + allOffset[i][j];
int xOffset = allOffset[i][j].x, yOffset = allOffset[i][j].y;
if (!pointHash.ContainsKey(key)) {
pointHash.Add(key, new(key.x, key.y, gridSize, gridSize, amountOfColor));
}
ColoredWorleyNoisePoint point = pointHash[key];
distance = Math.Abs(point.GetX(xOffset) - x)
+ Math.Abs(point.GetY(yOffset) - y);
if (distance > shortestDistance) { continue; }
if (distance == shortestDistance) { colorContender.Add(point.colorIndex); continue; }
colorContender = new List<int>() { point.colorIndex };
shortestDistance = distance;
}
if (manhattanDistanceThresholds[i] > shortestDistance) {
break;
}
}
return colorContender[new System.Random((centerKey.x << 16) | (centerKey.x & 0xFFFF)).Next(0, colorContender.Count)];
}
int ChebyshevDistance(Vector2Int centerKey, double x, double y) {
if (!pointHash.ContainsKey(centerKey)) {
pointHash.Add(centerKey, new(centerKey.x, centerKey.y, gridSize, gridSize, amountOfColor));
}
ColoredWorleyNoisePoint centerPoint = pointHash[centerKey];
List<int> colorContender = new() { centerPoint.colorIndex };
double shortestDistance = Math.Max(
Math.Abs(centerPoint.GetX(0) - x),
Math.Abs(centerPoint.GetY(0) - y));
double distance;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < allOffset[i].Length; j++) {
Vector2Int key = centerKey + allOffset[i][j];
int xOffset = allOffset[i][j].x, yOffset = allOffset[i][j].y;
if (!pointHash.ContainsKey(key)) {
pointHash.Add(key, new(key.x, key.y, gridSize, gridSize, amountOfColor));
}
ColoredWorleyNoisePoint point = pointHash[key];
distance = Math.Max(
Math.Abs(point.GetX(xOffset) - x),
Math.Abs(point.GetY(yOffset) - y));
if (distance > shortestDistance) { continue; }
if (distance == shortestDistance) { colorContender.Add(point.colorIndex); continue; }
colorContender = new List<int>() { point.colorIndex };
shortestDistance = distance;
}
if (chebyshevDistanceThresholds[i] > shortestDistance) {
break;
}
}
return colorContender[new System.Random((centerKey.x << 16) | (centerKey.x & 0xFFFF)).Next(0, colorContender.Count)];
}
int MinkowskiDistance(Vector2Int centerKey, double x, double y, uint p) {
if (!pointHash.ContainsKey(centerKey)) {
pointHash.Add(centerKey, new(centerKey.x, centerKey.y, gridSize, gridSize, amountOfColor));
}
ColoredWorleyNoisePoint centerPoint = pointHash[centerKey];
List<int> colorContender = new() { centerPoint.colorIndex };
double shortestDistance = Math.Pow(Math.Pow(Math.Abs(centerPoint.GetX(0) - x), p) + Math.Pow(Math.Abs(centerPoint.GetY(0) - y), p),1 /p);
double distance;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < allOffset[i].Length; j++) {
Vector2Int key = centerKey + allOffset[i][j];
int xOffset = allOffset[i][j].x, yOffset = allOffset[i][j].y;
if (!pointHash.ContainsKey(key)) {
pointHash.Add(key, new(key.x, key.y, gridSize, gridSize, amountOfColor));
}
ColoredWorleyNoisePoint point = pointHash[key];
distance = Math.Pow(
Math.Pow(
Math.Abs(point.GetX(xOffset) - x)
, p)
+ Math.Pow(
Math.Abs(point.GetY(yOffset) - y)
, p)
, 1 / p);
if (distance > shortestDistance) { continue; }
if (distance == shortestDistance) { colorContender.Add(point.colorIndex); continue; }
colorContender = new List<int>() { point.colorIndex };
shortestDistance = distance;
}
if (chebyshevDistanceThresholds[i] > shortestDistance) {
break;
}
}
return colorContender[new System.Random((centerKey.x << 16) | (centerKey.x & 0xFFFF)).Next(0, colorContender.Count)];
}
static int RoundAwayZero(double f) {
if (f > 0) return (int)Math.Ceiling(f);
return (int)Math.Floor(f);
}
static int RoundTowardZero(double f) {
if (f < 0) return (int)Math.Ceiling(f);
return (int)Math.Floor(f);
}
static int CalculateChunkPos(double pos, double halfGridSize) {
int layer = RoundAwayZero(pos / halfGridSize);
int chunk = RoundTowardZero(layer / 2);
return chunk;
}
public struct ColoredWorleyNoisePoint {
public double localX, localY;
readonly double width, height;
public int colorIndex;
public ColoredWorleyNoisePoint(
int globalGridPosX, int globalGridPosY,
double width, double height,
int colorCount) {
this.width = width;
this.height = height;
System.Random rng = new((globalGridPosX << 16) | (globalGridPosY & 0xFFFF));
localX = (rng.NextDouble() - 0.5f) * width;
localY = (rng.NextDouble() - 0.5f) * height;
colorIndex = rng.Next(0, colorCount);
}
public double GetX(int offsetX) {
return offsetX * width + localX;
}
public double GetY(int offsetY) {
return offsetY * height + localY;
}
}
public class Vector2Int {
public readonly int x, y;
public Vector2Int(int x, int y) {
this.x = x;
this.y = y;
}
public static Vector2Int operator +(Vector2Int a, Vector2Int b) {
return new Vector2Int(a.x + b.x, a.y + b.y);
}
}
}