This repository has been 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
/
Copy pathPQS.cs
221 lines (201 loc) · 7.25 KB
/
PQS.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
/**
* 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.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using ProceduralQuadSphere.KSP;
using ProceduralQuadSphere.Unity;
using XnaGeometry;
namespace ProceduralQuadSphere
{
/// <summary>
/// A fake implementation of the PQS, to support things like radius to be centralized
/// </summary>
public class PQS
{
/// <summary>
/// The radius of the sphere.
/// </summary>
public Double radius { get; set; }
/// <summary>
/// The lowest point of the sphere.
/// </summary>
public Double radiusMin { get; set; }
/// <summary>
/// The highest point of the sphere.
/// </summary>
public Double radiusMax { get; set; }
/// <summary>
/// The delta of <see cref="radiusMin"/> and <see cref="radiusMax"/>
/// </summary>
public Double radiusDelta
{
get { return radiusMax - radiusMin; }
}
/// <summary>
/// The mods attached to this sphere.
/// </summary>
public ReadOnlyCollection<PQSMod> mods { get; protected set; }
/// <summary>
/// Initializes a new PQS.
/// </summary>
/// <param name="radius">The radius of the sphere.</param>
public PQS(Double radius)
{
this.radius = radius;
mods = new ReadOnlyCollection<PQSMod>(new List<PQSMod>());
}
/// <summary>
/// Setups the sphere.
/// </summary>
public void SetupSphere()
{
List<PQSMod> mods_ = new List<PQSMod>(mods);
mods_.Sort((a, b) => a.order.CompareTo(b.order));
if (!mods_.Any())
return;
for (Int32 i = 0; i < mods_.Count; i++)
{
if (!mods_[i].modEnabled)
{
mods_.RemoveAt(i);
}
mods_[i].sphere = this;
}
mods = mods_.AsReadOnly();
OnSetup();
radiusMin = 0;
radiusMax = 0;
foreach (PQSMod mod in mods)
{
radiusMin += mod.GetVertexMinHeight();
radiusMax += mod.GetVertexMaxHeight();
}
}
/// <summary>
/// Calls OnSetup in all Mods
/// </summary>
public void OnSetup()
{
foreach (PQSMod mod in mods)
mod.OnSetup();
}
/// <summary>
/// Call OnVertexBuildHeight in all Mods
/// </summary>
/// <param name="data">The data.</param>
public void OnVertexBuildHeight(VertexBuildData data)
{
// Build VertexBuildData
data.latitude = Math.Asin(data.directionFromCenter.Y);
if (Double.IsNaN(data.latitude))
data.latitude = Math.PI / 2;
data.directionXZ = Vector3.Normalize(new Vector3(data.directionFromCenter.X, 0, data.directionFromCenter.Z));
if (data.directionXZ.Length() <= 0)
data.longitude = 0;
else
data.longitude = data.directionXZ.Z >= 0 ? Math.Asin(data.directionXZ.X / data.directionXZ.Length()) : Math.PI - Math.Asin(data.directionXZ.X / data.directionXZ.Length());
data.v = data.latitude / Math.PI + 0.5;
data.u = data.longitude / Math.PI * 0.5;
foreach (PQSMod mod in mods)
mod.OnVertexBuildHeight(data);
}
/// <summary>
/// Call OnVertexBuild in all Mods
/// </summary>
/// <param name="data">The data.</param>
public void OnVertexBuild(VertexBuildData data)
{
// Build VertexBuildData
data.latitude = Math.Asin(data.directionFromCenter.Y);
if (Double.IsNaN(data.latitude))
data.latitude = Math.PI / 2;
data.directionXZ = Vector3.Normalize(new Vector3(data.directionFromCenter.X, 0, data.directionFromCenter.Z));
if (data.directionXZ.Length() <= 0)
data.longitude = 0;
else
data.longitude = data.directionXZ.Z >= 0 ? Math.Asin(data.directionXZ.X / data.directionXZ.Length()) : Math.PI - Math.Asin(data.directionXZ.X / data.directionXZ.Length());
data.v = data.latitude / Math.PI + 0.5;
data.u = data.longitude / Math.PI * 0.5;
foreach (PQSMod mod in mods)
mod.OnVertexBuild(data);
}
/// <summary>
/// Adds a new PQSMod of type T to the sphere
/// </summary>
public T AddPQSMod<T>() where T : PQSMod, new()
{
T mod = new T();
List<PQSMod> m_ = new List<PQSMod>(mods);
m_.Add(mod);
mods = m_.AsReadOnly();
return mod;
}
/// <summary>
/// Adds a new PQSMod of type T to the sphere
/// </summary>
public T AddPQSMod<T>(String name) where T : PQSMod, new()
{
T mod = AddPQSMod<T>();
mod.name = name;
return mod;
}
/// <summary>
/// Returns the first PQSMod with the given name, or null if nothing was found.
/// </summary>
public T GetPQSMod<T>(String name) where T : PQSMod, new()
{
if (mods.Any(m => m.name == name && m is T))
return (T)mods.FirstOrDefault(m => m.name == name && m is T);
else
return default(T);
}
/// <summary>
/// Returns the PQSMods with the given type, or null if nothing was found.
/// </summary>
public T[] GetPQSMods<T>() where T : PQSMod, new()
{
if (mods.Any(m => m is T))
return (T[])mods.Where(m => m is T).ToArray();
else
return default(T[]);
}
/// <summary>
/// Returns the PQSMods with the given name and type, or null if nothing was found.
/// </summary>
public T[] GetPQSMods<T>(String name) where T : PQSMod, new()
{
if (mods.Any(m => m.name == name && m is T))
return (T[])mods.Where(m => m.name == name && m is T).ToArray();
else
return default(T[]);
}
/// <summary>
/// Removes the given PQSMod from the sphere
/// </summary>
public void RemovePQSMod<T>(T mod) where T : PQSMod, new()
{
List<PQSMod> m_ = new List<PQSMod>(mods);
m_.Remove(mod);
mods = m_.AsReadOnly();
}
/// <summary>
/// Builds the relative rectangular point of the sphere
/// </summary>
public VertexBuildData BuildData(Double x, Double y)
{
VertexBuildData data = new VertexBuildData
{
directionFromCenter = (Quaternion.CreateFromAxisAngle(Vector3.Up, Math.PI * 2 / x) * Quaternion.CreateFromAxisAngle(Vector3.Right, 90d - Math.PI / y)) * Vector3.Forward,
vertHeight = radius
};
OnVertexBuildHeight(data);
OnVertexBuild(data);
return data;
}
}
}