This repository was archived by the owner on Aug 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPQSMod_VertexPlanet.cs
More file actions
340 lines (305 loc) · 12.8 KB
/
PQSMod_VertexPlanet.cs
File metadata and controls
340 lines (305 loc) · 12.8 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* libpqsmods - A standalone implementation of KSP's PQSMods
* Copyright (c) Thomas P. 2016
* Licensed under the terms of the MIT license
*/
using System;
using System.Linq;
using LibNoise;
using LibNoise.Generator;
using LibNoise.Operator;
using ProceduralQuadSphere.KSP;
using ProceduralQuadSphere.Unity;
using XnaGeometry;
namespace ProceduralQuadSphere
{
/// <summary>
/// A mod that can make a planet from scratch
/// </summary>
/// <seealso cref="PQSMod" />
public class PQSMod_VertexPlanet : PQSMod
{
#region I won't document this
public Int32 seed;
public Double deformity;
public Double colorDeformity;
public Double oceanLevel;
public Double oceanStep;
public Double oceanDepth;
public Boolean oceanSnap;
public Double terrainSmoothing = 0.25;
public Double terrainShapeStart = 2;
public Double terrainShapeEnd = -2;
public Double terrainRidgesMin = 0.4;
public Double terrainRidgesMax = 1;
public SimplexWrapper continental;
public SimplexWrapper continentalRuggedness;
private SimplexWrapper continentalSmoothing;
public SimplexWrapper continentalSharpnessMap;
public NoiseModWrapper continentalSharpness;
public SimplexWrapper terrainType;
public Boolean buildHeightColors;
public LandClass[] landClasses;
public Double terrainRidgeBalance = 0.1;
private Int32 lcCount;
private Double continentialHeightPreSmooth;
#endregion
/// <summary>
/// Initializes the base mod
/// </summary>
public override void OnSetup()
{
lcCount = landClasses.Length;
Int32 i = 0;
while (i < lcCount)
{
landClasses[i].colorNoiseMap.Setup(seed + i + 10);
i++;
}
continental.Setup(seed);
continentalRuggedness.Setup(seed + 1);
continentalSmoothing = new SimplexWrapper(continental);
continentalSmoothing.Setup(seed);
continentalSmoothing.persistance = continental.persistance * terrainSmoothing;
continentalSmoothing.frequency = continental.frequency;
terrainType.Setup(seed + 2);
continentalSharpness.Setup(new RidgedMultifractal(continentalSharpness.frequency, continentalSharpness.persistance, continentalSharpness.octaves, seed + 3, QualityMode.High));
continentalSharpnessMap.Setup(seed + 4);
}
/// <summary>
/// Called when the parent sphere builds it's color
/// </summary>
public override void OnVertexBuild(VertexBuildData data)
{
if (!buildHeightColors)
{
Double d1 = terrainType.simplex.noiseNormalized(data.directionFromCenter);
Double tHeight = Mathf.Clamp01((Single)((continentialHeightPreSmooth + d1 * terrainType.deformity) * (Single)((data.vertHeight - sphere.radius) / colorDeformity)));
LandClass lcSelected = SelectLandClassByHeight(tHeight);
Color c1 = Color.Lerp(lcSelected.baseColor, lcSelected.colorNoise, (Single)(lcSelected.colorNoiseAmount * lcSelected.colorNoiseMap.simplex.noiseNormalized(data.directionFromCenter)));
if (lcSelected.lerpToNext)
{
LandClass lcLerp = landClasses[landClasses.ToList().IndexOf(lcSelected) + 1];
Color c2 = Color.Lerp(lcLerp.baseColor, lcLerp.colorNoise, (Single)(lcLerp.colorNoiseAmount * lcLerp.colorNoiseMap.simplex.noiseNormalized(data.directionFromCenter)));
c1 = Color.Lerp(c1, c2, (Single)((tHeight - lcSelected.fractalStart) / (lcSelected.fractalEnd - lcSelected.fractalStart)));
}
data.vertColor = c1;
data.vertColor.a = (Single)continentialHeightPreSmooth;
}
else
{
data.vertColor.r = data.vertColor.g = data.vertColor.b = (Single)((data.vertHeight - sphere.radius) / colorDeformity);
}
}
/// <summary>
/// Selects the land class responsible for the given height
/// </summary>
public LandClass SelectLandClassByHeight(double height)
{
for (Int32 i = 0; i < lcCount; i++)
{
if (height >= landClasses[i].fractalStart && height <= landClasses[i].fractalEnd)
{
return landClasses[i];
}
}
return landClasses[0];
}
/// <summary>
/// Called when the parent sphere builds it's height
/// </summary>
public override void OnVertexBuildHeight(VertexBuildData data)
{
Double continentalDeformity = 1;
Double continental2Height = continentalSmoothing.simplex.noiseNormalized(data.directionFromCenter);
continental.simplex.persistence = continental.persistance - continentalSmoothing.persistance * continental2Height;
Double continentialHeight = continental.simplex.noiseNormalized(data.directionFromCenter);
Double continentialSharpnessValue = (continentalSharpness.noise.GetValue(data.directionFromCenter) + 1) * 0.5;
continentialSharpnessValue *= MathHelper.Lerp(continentalSharpness.deformity, continentalSharpness.deformity * terrainRidgeBalance, (continental2Height + continentialSharpnessValue) * 0.5);
Double continentialSharpnessMapValue = MathHelper.Clamp((continentalSharpnessMap.simplex.noise(data.directionFromCenter) + 1) * 0.5, terrainRidgesMin, terrainRidgesMax);
continentialSharpnessMapValue = (continentialSharpnessMapValue - terrainRidgesMin) / (terrainRidgesMax - terrainRidgesMin) * continentalSharpnessMap.deformity;
continentialSharpnessValue = continentialSharpnessValue + MathHelper.Lerp(0, continentialSharpnessValue, continentialSharpnessMapValue);
continentialHeight += continentialSharpnessValue;
continentalDeformity = continentalDeformity + continentalSharpness.deformity * continentalSharpnessMap.deformity;
continentialHeight = continentialHeight / continentalDeformity;
Double continentalDelta = (continentialHeight - oceanLevel) / (1 - oceanLevel);
Double vHeight;
if (continentialHeight >= oceanLevel)
{
continentalRuggedness.simplex.persistence = continentalRuggedness.persistance*continentalDelta;
Double continentalRHeight = continentalRuggedness.simplex.noiseNormalized(data.directionFromCenter)*continentalDelta*continentalDelta;
continentialHeight = continentalDelta*continental.deformity + continentalRHeight*this.continentalRuggedness.deformity;
continentialHeight /= (continental.deformity + continentalRuggedness.deformity);
continentialHeightPreSmooth = continentialHeight;
continentialHeight = MathHelper.Hermite(0, terrainShapeStart, 1, terrainShapeEnd, continentialHeight);
vHeight = continentialHeight;
}
else
{
if (!oceanSnap)
vHeight = continentalDelta*this.oceanDepth - this.oceanStep;
else
vHeight = -oceanStep;
continentialHeightPreSmooth = vHeight;
}
data.vertHeight += Math.Round(vHeight, 5) * deformity;
}
/// <summary>
/// Gets the maximum height of the mod.
/// </summary>
public override Double GetVertexMaxHeight()
{
return deformity;
}
/// <summary>
/// Gets the minimum height of the mod.
/// </summary>
public override Double GetVertexMinHeight()
{
return 0;
}
/// <summary>
/// An object that connects an area on the planet with a color
/// </summary>
public class LandClass
{
/// <summary>
/// The name of the class
/// </summary>
public String name;
/// <summary>
/// Where the class starts
/// </summary>
public Double fractalStart;
/// <summary>
/// Where the class ends
/// </summary>
public Double fractalEnd;
/// <summary>
/// The base color
/// </summary>
public Color baseColor;
/// <summary>
/// The color that is applied together with the noise
/// </summary>
public Color colorNoise;
/// <summary>
/// How much of the noise color should get taken into account
/// </summary>
public Double colorNoiseAmount;
/// <summary>
/// The color noise
/// </summary>
public SimplexWrapper colorNoiseMap;
/// <summary>
/// Whether the color should get lerped into the color of the next class
/// </summary>
public Boolean lerpToNext;
/// <summary>
/// The difference between the two fractal borders
/// </summary>
public Double fractalDelta => fractalEnd - fractalStart;
public LandClass(String name, Double fractalStart, Double fractalEnd, Color baseColor, Color colorNoise, Double colorNoiseAmount)
{
this.name = name;
this.fractalStart = fractalStart;
this.fractalEnd = fractalEnd;
this.baseColor = baseColor;
this.colorNoise = colorNoise;
this.colorNoiseAmount = colorNoiseAmount;
}
}
/// <summary>
/// A wrapper for a custom Noise
/// </summary>
public class NoiseModWrapper
{
/// <summary>
/// The deformity of the noise
/// </summary>
public Double deformity;
/// <summary>
/// The octaves for the noise
/// </summary>
public Int32 octaves;
/// <summary>
/// The persistance of the noise
/// </summary>
public Double persistance;
/// <summary>
/// The frequency of the noise
/// </summary>
public Double frequency;
/// <summary>
/// The final noise
/// </summary>
public ModuleBase noise;
/// <summary>
/// The seed for the noise
/// </summary>
public Int32 seed;
public NoiseModWrapper(NoiseModWrapper copyFrom)
{
deformity = copyFrom.deformity;
octaves = copyFrom.octaves;
persistance = copyFrom.persistance;
frequency = copyFrom.frequency;
}
public NoiseModWrapper(Double deformity, Int32 octaves, Double persistance, Double frequency)
{
this.deformity = deformity;
this.octaves = octaves;
this.persistance = persistance;
this.frequency = frequency;
}
public void Setup(ModuleBase mod)
{
noise = mod;
}
}
/// <summary>
/// A wrapper for a custom Simplex
/// </summary>
public class SimplexWrapper
{
/// <summary>
/// The deformity of the simplex
/// </summary>
public Double deformity;
/// <summary>
/// The octaves for the simplex
/// </summary>
public Double octaves;
/// <summary>
/// The persistance of the simplex
/// </summary>
public Double persistance;
/// <summary>
/// The frequency of the simplex
/// </summary>
public Double frequency;
/// <summary>
/// The final simplex
/// </summary>
public Simplex simplex;
public SimplexWrapper(SimplexWrapper copyFrom)
{
deformity = copyFrom.deformity;
octaves = copyFrom.octaves;
persistance = copyFrom.persistance;
frequency = copyFrom.frequency;
}
public SimplexWrapper(Double deformity, Double octaves, Double persistance, Double frequency)
{
this.deformity = deformity;
this.octaves = octaves;
this.persistance = persistance;
this.frequency = frequency;
}
public void Setup(Int32 seed)
{
simplex = new Simplex(seed, octaves, persistance, frequency);
}
}
}
}