/
Utility.cs
1094 lines (971 loc) · 45.6 KB
/
Utility.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Kopernicus Planetary System Modifier
* -------------------------------------------------------------
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* This library is intended to be used as a plugin for Kerbal Space Program
* which is copyright 2011-2017 Squad. Your usage of Kerbal Space Program
* itself is governed by the terms of its EULA, not the license above.
*
* https://kerbalspaceprogram.com
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEngine;
namespace Kopernicus
{
public class Utility
{
/// <summary>
/// Local Space Game Object
/// </summary>
public static GameObject LocalSpace
{
get { return GameObject.Find(PSystemManager.Instance.localSpaceName); }
}
// Static object representing the deactivator
private static GameObject deactivator;
/// <summary>
/// Get an object which is deactivated, essentially, and children are prefabs
/// </summary>
public static Transform Deactivator
{
get
{
if (deactivator == null)
{
deactivator = new GameObject("__deactivator");
deactivator.SetActive(false);
UnityEngine.Object.DontDestroyOnLoad(deactivator);
}
return deactivator.transform;
}
}
/// <summary>
/// Copy one objects fields to another object via reflection
/// </summary>
/// <param name="source">Object to copy fields from</param>
/// <param name="destination">Object to copy fields to</param>
/// <param name="log">Whether the function should log the actions it performs</param>
public static void CopyObjectFields<T>(T source, T destination, Boolean log = true)
{
// Reflection based copy
foreach (FieldInfo field in (typeof(T)).GetFields())
{
// Only copy non static fields
if (!field.IsStatic)
{
if (log)
{
Logger.Active.Log("Copying \"" + field.Name + "\": " + (field.GetValue(destination) ?? "<NULL>") + " => " + (field.GetValue(source) ?? "<NULL>"));
}
field.SetValue(destination, field.GetValue(source));
}
}
}
/// <summary>
/// Recursively searches for a named transform in the Transform heirarchy. The requirement of
/// such a function is sad.This should really be in the Unity3D API.Transform.Find() only
/// searches in the immediate children.
/// </summary>
/// <param name="transform">Transform in which is search for named child</param>
/// <param name="name">Name of child to find</param>
/// <returns>Desired transform or null if it could not be found</returns>
public static Transform FindInChildren(Transform transform, String name)
{
// Is this null?
if (transform == null)
{
return null;
}
// Are the names equivalent
if (transform.name == name)
{
return transform;
}
// If we did not find a transform, search through the children
foreach (Transform child in transform)
{
// Recurse into the child
Transform t = FindInChildren(child, name);
if (t != null)
{
return t;
}
}
// Return the transform (will be null if it was not found)
return null;
}
// Dump an object by reflection
public static void DumpObjectFields(object o, String title = "---------")
{
// Dump the raw PQS of Dres (by reflection)
Logger.Active.Log("---------" + title + "------------");
foreach (FieldInfo field in o.GetType().GetFields())
{
if (!field.IsStatic)
{
Logger.Active.Log(field.Name + " = " + field.GetValue(o));
}
}
Logger.Active.Log("--------------------------------------");
}
public static void DumpObjectProperties(object o, String title = "---------")
{
// Iterate through all of the properties
Logger.Active.Log("--------- " + title + " ------------");
foreach (PropertyInfo property in o.GetType().GetProperties())
{
if (property.CanRead)
Logger.Active.Log(property.Name + " = " + property.GetValue(o, null));
}
Logger.Active.Log("--------------------------------------");
}
/// <summary>
/// Recursively searches for a named PSystemBody
/// </summary>
/// <param name="body">Parent body to begin search in</param>
/// <param name="name">Name of body to find</param>
/// <returns>Desired body or null if not found</returns>
public static PSystemBody FindBody(PSystemBody body, String name)
{
// Is this the body wer are looking for?
if (body.celestialBody.bodyName == name)
return body;
// Otherwise search children
foreach (PSystemBody child in body.children)
{
PSystemBody b = FindBody(child, name);
if (b != null)
return b;
}
// Return null because we didn't find shit
return null;
}
// Copy of above, but finds homeworld
public static PSystemBody FindHomeBody(PSystemBody body)
{
// Is this the body wer are looking for?
if (body.celestialBody.isHomeWorld)
return body;
// Otherwise search children
foreach (PSystemBody child in body.children)
{
PSystemBody b = FindHomeBody(child);
if (b != null)
return b;
}
// Return null because we didn't find shit
return null;
}
// Print out a tree containing all the objects in the game
public static void PerformObjectDump()
{
Logger.Active.Log("--------- Object Dump -----------");
foreach (GameObject b in GameObject.FindObjectsOfType(typeof(GameObject)))
{
// Essentially, we iterate through all game objects currently alive and search for
// the ones without a parent. Extrememly inefficient and terrible, but its just for
// exploratory purposes
if (b.transform.parent == null)
{
// Print out the tree of child objects
GameObjectWalk(b, "");
}
}
Logger.Active.Log("---------------------------------");
}
public static void PrintTransform(Transform t, String title = "")
{
Logger.Active.Log("------" + title + "------");
Logger.Active.Log("Position: " + t.localPosition);
Logger.Active.Log("Rotation: " + t.localRotation);
Logger.Active.Log("Scale: " + t.localScale);
Logger.Active.Log("------------------");
}
// Print out the tree of components
public static void GameObjectWalk(GameObject o, String prefix = "")
{
// If null, don't do anything
if (o == null)
return;
// Print this object
Logger.Active.Log(prefix + o);
Logger.Active.Log(prefix + " >>> Components <<< ");
foreach (Component c in o.GetComponents(typeof(Component)))
{
Logger.Active.Log(prefix + " " + c);
}
Logger.Active.Log(prefix + " >>> ---------- <<< ");
// Game objects are related to each other via transforms in Unity3D.
foreach (Transform b in o.transform)
{
if (b != null)
GameObjectWalk(b.gameObject, " " + prefix);
}
}
// Print out the celestial bodies
public static void PSystemBodyWalk(PSystemBody b, String prefix = "")
{
Logger.Active.Log(prefix + b.celestialBody.bodyName + ":" + b.flightGlobalsIndex);
foreach (PSystemBody c in b.children)
{
PSystemBodyWalk(c, prefix + " ");
}
}
// slightly different:
static public void DumpUpwards(Transform t, String prefix, Boolean useKLog = true)
{
String str = prefix + "Transform " + t.name;
if (useKLog)
Logger.Default.Log(str);
else
Debug.Log(str);
foreach (Component c in t.GetComponents<Component>())
{
str = prefix + " has component " + c.name + " of type " + c.GetType().FullName;
if (useKLog)
Logger.Default.Log(str);
else
Debug.Log(str);
}
if (t.parent != null)
DumpUpwards(t.parent, prefix + " ");
}
static public void DumpDownwards(Transform t, String prefix, Boolean useKLog = true)
{
String str = prefix + "Transform " + t.name;
if (useKLog)
Logger.Default.Log(str);
else
Debug.Log("[Kopernicus] " + str);
foreach (Component c in t.GetComponents<Component>())
{
str = prefix + " has component " + c.name + " of type " + c.GetType().FullName;
if (useKLog)
Logger.Default.Log(str);
else
Debug.Log("[Kopernicus] " + str);
}
if (t.childCount > 0)
for (Int32 i = 0; i < t.childCount; ++i)
DumpDownwards(t.GetChild(i), prefix + " ");
}
public static void UpdateScaledMesh(GameObject scaledVersion, PQS pqs, CelestialBody body, String path, String cacheFile, Boolean exportBin, Boolean useSpherical)
{
const Double rJool = 6000000.0;
const Single rScaled = 1000.0f;
// Compute scale between Jool and this body
Single scale = (Single)(body.Radius / rJool);
scaledVersion.transform.localScale = new Vector3(scale, scale, scale);
Mesh scaledMesh;
// Attempt to load a cached version of the scale space
String CacheDirectory = KSPUtil.ApplicationRootPath + path;
String CacheFile = CacheDirectory + "/" + body.name + ".bin";
if (!String.IsNullOrEmpty(cacheFile))
{
CacheFile = Path.Combine(Path.Combine(KSPUtil.ApplicationRootPath, "GameData"), cacheFile);
CacheDirectory = Path.GetDirectoryName(CacheFile);
Logger.Active.Log($"{body.name} is using custom cache file '{CacheFile}' in '{CacheDirectory}'");
}
Directory.CreateDirectory(CacheDirectory);
if (File.Exists(CacheFile) && exportBin)
{
Logger.Active.Log("Body.PostApply(ConfigNode): Loading cached scaled space mesh: " + body.name);
scaledMesh = Utility.DeserializeMesh(CacheFile);
Utility.RecalculateTangents(scaledMesh);
scaledVersion.GetComponent<MeshFilter>().sharedMesh = scaledMesh;
}
// Otherwise we have to generate the mesh
else
{
Logger.Active.Log("Body.PostApply(ConfigNode): Generating scaled space mesh: " + body.name);
scaledMesh = ComputeScaledSpaceMesh(body, useSpherical ? null : pqs);
Utility.RecalculateTangents(scaledMesh);
scaledVersion.GetComponent<MeshFilter>().sharedMesh = scaledMesh;
if (exportBin)
Utility.SerializeMesh(scaledMesh, CacheFile);
}
// Apply mesh to the body
SphereCollider collider = scaledVersion.GetComponent<SphereCollider>();
if (collider != null) collider.radius = rScaled;
if (pqs != null && scaledVersion.gameObject != null && scaledVersion.gameObject.transform != null)
{
scaledVersion.gameObject.transform.localScale = Vector3.one * (Single)(pqs.radius / rJool);
}
}
// Generate the scaled space mesh using PQS (all results use scale of 1)
public static Mesh ComputeScaledSpaceMesh(CelestialBody body, PQS pqs)
{
// We need to get the body for Jool (to steal it's mesh)
const Double rScaledJool = 1000.0f;
Double rMetersToScaledUnits = (Single)(rScaledJool / body.Radius);
// Generate a duplicate of the Jool mesh
Mesh mesh = Utility.DuplicateMesh(Templates.ReferenceGeosphere);
// If this body has a PQS, we can create a more detailed object
if (pqs != null)
{
// first we enable all maps
OnDemand.OnDemandStorage.EnableBody(body.bodyName);
// In order to generate the scaled space we have to enable the mods. Since this is
// a prefab they don't get disabled as kill game performance. To resolve this we
// clone the PQS, use it, and then delete it when done
GameObject pqsVersionGameObject = UnityEngine.Object.Instantiate(pqs.gameObject) as GameObject;
PQS pqsVersion = pqsVersionGameObject.GetComponent<PQS>();
Type[] blacklist = new Type[] { typeof(OnDemand.PQSMod_OnDemandHandler) };
// Deactivate blacklisted Mods
foreach (PQSMod mod in pqsVersion.GetComponentsInChildren<PQSMod>(true).Where(m => blacklist.Contains(m.GetType())))
{
mod.modEnabled = false;
}
// Find the PQS mods and enable the PQS-sphere
IEnumerable<PQSMod> mods = pqsVersion.GetComponentsInChildren<PQSMod>(true).Where(m => m.modEnabled).OrderBy(m => m.order);
foreach (PQSMod flatten in mods.Where(m => m is PQSMod_FlattenArea))
flatten.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(f => f.FieldType == typeof(Boolean)).First().SetValue(flatten, true);
pqsVersion.StartUpSphere();
pqsVersion.isBuildingMaps = true;
// If we were able to find PQS mods
if (mods.Count() > 0)
{
// Generate the PQS modifications
Vector3[] vertices = mesh.vertices;
for (Int32 i = 0; i < mesh.vertexCount; i++)
{
// Get the UV coordinate of this vertex
Vector2 uv = mesh.uv[i];
// Since this is a geosphere, normalizing the vertex gives the direction from center center
Vector3 direction = vertices[i];
direction.Normalize();
// Build the vertex data object for the PQS mods
PQS.VertexBuildData vertex = new PQS.VertexBuildData();
vertex.directionFromCenter = direction;
vertex.vertHeight = body.Radius;
vertex.u = uv.x;
vertex.v = uv.y;
// Build from the PQS
foreach (PQSMod mod in mods)
mod.OnVertexBuildHeight(vertex);
// Check for sea level
if (body.ocean && vertex.vertHeight < body.Radius)
vertex.vertHeight = body.Radius;
// Adjust the displacement
vertices[i] = direction * (Single)(vertex.vertHeight * rMetersToScaledUnits);
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
}
// Cleanup
pqsVersion.isBuildingMaps = false;
pqsVersion.DeactivateSphere();
UnityEngine.Object.Destroy(pqsVersionGameObject);
}
// Return the generated scaled space mesh
return mesh;
}
public static void CopyMesh(Mesh source, Mesh dest)
{
//ProfileTimer.Push("CopyMesh");
Vector3[] verts = new Vector3[source.vertexCount];
source.vertices.CopyTo(verts, 0);
dest.vertices = verts;
Int32[] tris = new Int32[source.triangles.Length];
source.triangles.CopyTo(tris, 0);
dest.triangles = tris;
Vector2[] uvs = new Vector2[source.uv.Length];
source.uv.CopyTo(uvs, 0);
dest.uv = uvs;
Vector2[] uv2s = new Vector2[source.uv2.Length];
source.uv2.CopyTo(uv2s, 0);
dest.uv2 = uv2s;
Vector3[] normals = new Vector3[source.normals.Length];
source.normals.CopyTo(normals, 0);
dest.normals = normals;
Vector4[] tangents = new Vector4[source.tangents.Length];
source.tangents.CopyTo(tangents, 0);
dest.tangents = tangents;
Color[] colors = new Color[source.colors.Length];
source.colors.CopyTo(colors, 0);
dest.colors = colors;
Color32[] colors32 = new Color32[source.colors32.Length];
source.colors32.CopyTo(colors32, 0);
dest.colors32 = colors32;
//ProfileTimer.Pop("CopyMesh");
}
public static Mesh DuplicateMesh(Mesh source)
{
// Create new mesh object
Mesh dest = new Mesh();
//ProfileTimer.Push("CopyMesh");
Vector3[] verts = new Vector3[source.vertexCount];
source.vertices.CopyTo(verts, 0);
dest.vertices = verts;
Int32[] tris = new Int32[source.triangles.Length];
source.triangles.CopyTo(tris, 0);
dest.triangles = tris;
Vector2[] uvs = new Vector2[source.uv.Length];
source.uv.CopyTo(uvs, 0);
dest.uv = uvs;
Vector2[] uv2s = new Vector2[source.uv2.Length];
source.uv2.CopyTo(uv2s, 0);
dest.uv2 = uv2s;
Vector3[] normals = new Vector3[source.normals.Length];
source.normals.CopyTo(normals, 0);
dest.normals = normals;
Vector4[] tangents = new Vector4[source.tangents.Length];
source.tangents.CopyTo(tangents, 0);
dest.tangents = tangents;
Color[] colors = new Color[source.colors.Length];
source.colors.CopyTo(colors, 0);
dest.colors = colors;
Color32[] colors32 = new Color32[source.colors32.Length];
source.colors32.CopyTo(colors32, 0);
dest.colors32 = colors32;
//ProfileTimer.Pop("CopyMesh");
return dest;
}
// Taken from Nathankell's RSS Utils.cs; uniformly scaled vertices
public static void ScaleVerts(Mesh mesh, Single scaleFactor)
{
//ProfileTimer.Push("ScaleVerts");
Vector3[] vertices = new Vector3[mesh.vertexCount];
for (Int32 i = 0; i < mesh.vertexCount; i++)
{
Vector3 v = mesh.vertices[i];
v *= scaleFactor;
vertices[i] = v;
}
mesh.vertices = vertices;
//ProfileTimer.Pop("ScaleVerts");
}
public static void RecalculateTangents(Mesh theMesh)
{
Int32 vertexCount = theMesh.vertexCount;
Vector3[] vertices = theMesh.vertices;
Vector3[] normals = theMesh.normals;
Vector2[] texcoords = theMesh.uv;
Int32[] triangles = theMesh.triangles;
Int32 triangleCount = triangles.Length / 3;
var tangents = new Vector4[vertexCount];
var tan1 = new Vector3[vertexCount];
var tan2 = new Vector3[vertexCount];
Int32 tri = 0;
for (Int32 i = 0; i < (triangleCount); i++)
{
Int32 i1 = triangles[tri];
Int32 i2 = triangles[tri + 1];
Int32 i3 = triangles[tri + 2];
Vector3 v1 = vertices[i1];
Vector3 v2 = vertices[i2];
Vector3 v3 = vertices[i3];
Vector2 w1 = texcoords[i1];
Vector2 w2 = texcoords[i2];
Vector2 w3 = texcoords[i3];
Single x1 = v2.x - v1.x;
Single x2 = v3.x - v1.x;
Single y1 = v2.y - v1.y;
Single y2 = v3.y - v1.y;
Single z1 = v2.z - v1.z;
Single z2 = v3.z - v1.z;
Single s1 = w2.x - w1.x;
Single s2 = w3.x - w1.x;
Single t1 = w2.y - w1.y;
Single t2 = w3.y - w1.y;
Single r = 1.0f / (s1 * t2 - s2 * t1);
var sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
var tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
tan1[i1] += sdir;
tan1[i2] += sdir;
tan1[i3] += sdir;
tan2[i1] += tdir;
tan2[i2] += tdir;
tan2[i3] += tdir;
tri += 3;
}
for (Int32 i = 0; i < (vertexCount); i++)
{
Vector3 n = normals[i];
Vector3 t = tan1[i];
// Gram-Schmidt orthogonalize
Vector3.OrthoNormalize(ref n, ref t);
tangents[i].x = t.x;
tangents[i].y = t.y;
tangents[i].z = t.z;
// Calculate handedness
tangents[i].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[i]) < 0.0f) ? -1.0f : 1.0f;
}
theMesh.tangents = tangents;
}
// Serialize a mesh to disk
public static void SerializeMesh(Mesh mesh, String path)
{
// Open an output filestream
FileStream outputStream = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
BinaryWriter writer = new BinaryWriter(outputStream);
// Write the vertex count of the mesh
writer.Write(mesh.vertices.Length);
foreach (Vector3 vertex in mesh.vertices)
{
writer.Write(vertex.x);
writer.Write(vertex.y);
writer.Write(vertex.z);
}
writer.Write(mesh.uv.Length);
foreach (Vector2 uv in mesh.uv)
{
writer.Write(uv.x);
writer.Write(uv.y);
}
writer.Write(mesh.triangles.Length);
foreach (Int32 triangle in mesh.triangles)
{
writer.Write(triangle);
}
// Finish writing
writer.Close();
outputStream.Close();
}
// Deserialize a mesh from disk
public static Mesh DeserializeMesh(String path)
{
FileStream inputStream = new FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
BinaryReader reader = new BinaryReader(inputStream);
// Get the vertices
Int32 count = reader.ReadInt32();
Vector3[] vertices = new Vector3[count];
for (Int32 i = 0; i < count; i++)
{
Vector3 vertex;
vertex.x = reader.ReadSingle();
vertex.y = reader.ReadSingle();
vertex.z = reader.ReadSingle();
vertices[i] = vertex;
}
// Get the uvs
Int32 uv_count = reader.ReadInt32();
Vector2[] uvs = new Vector2[uv_count];
for (Int32 i = 0; i < uv_count; i++)
{
Vector2 uv;
uv.x = reader.ReadSingle();
uv.y = reader.ReadSingle();
uvs[i] = uv;
}
// Get the triangles
Int32 tris_count = reader.ReadInt32();
Int32[] triangles = new Int32[tris_count];
for (Int32 i = 0; i < tris_count; i++)
triangles[i] = reader.ReadInt32();
// Close
reader.Close();
inputStream.Close();
// Create the mesh
Mesh m = new Mesh();
m.vertices = vertices;
m.triangles = triangles;
m.uv = uvs;
m.RecalculateNormals();
m.RecalculateBounds();
return m;
}
// Credit goes to Kragrathea.
public static Texture2D BumpToNormalMap(Texture2D source, Single strength)
{
strength = Mathf.Clamp(strength, 0.0F, 10.0F);
var result = new Texture2D(source.width, source.height, TextureFormat.ARGB32, true);
for (Int32 by = 0; by < result.height; by++)
{
for (var bx = 0; bx < result.width; bx++)
{
var xLeft = source.GetPixel(bx - 1, by).grayscale * strength;
var xRight = source.GetPixel(bx + 1, by).grayscale * strength;
var yUp = source.GetPixel(bx, by - 1).grayscale * strength;
var yDown = source.GetPixel(bx, by + 1).grayscale * strength;
var xDelta = ((xLeft - xRight) + 1) * 0.5f;
var yDelta = ((yUp - yDown) + 1) * 0.5f;
result.SetPixel(bx, by, new Color(yDelta, yDelta, yDelta, xDelta));
}
}
result.Apply();
return result;
}
// Convert latitude-longitude-altitude with body radius to a vector.
public static Vector3 LLAtoECEF(Double lat, Double lon, Double alt, Double radius)
{
const Double degreesToRadians = Math.PI / 180.0;
lat = (lat - 90) * degreesToRadians;
lon *= degreesToRadians;
Double x, y, z;
Double n = radius; // for now, it's still a sphere, so just the radius
x = (n + alt) * -1.0 * Math.Sin(lat) * Math.Cos(lon);
y = (n + alt) * Math.Cos(lat); // for now, it's still a sphere, so no eccentricity
z = (n + alt) * -1.0 * Math.Sin(lat) * Math.Sin(lon);
return new Vector3((Single)x, (Single)y, (Single)z);
}
public static Boolean TextureExists(String path)
{
path = KSPUtil.ApplicationRootPath + "GameData/" + path;
return System.IO.File.Exists(path);
}
public static Texture2D LoadTexture(String path, Boolean compress, Boolean upload, Boolean unreadable)
{
Texture2D map = null;
path = KSPUtil.ApplicationRootPath + "GameData/" + path;
if (System.IO.File.Exists(path))
{
Boolean uncaught = true;
try
{
if (path.ToLower().EndsWith(".dds"))
{
// Borrowed from stock KSP 1.0 DDS loader (hi Mike!)
// Also borrowed the extra bits from Sarbian.
byte[] buffer = System.IO.File.ReadAllBytes(path);
System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(new System.IO.MemoryStream(buffer));
uint num = binaryReader.ReadUInt32();
if (num == DDSHeaders.DDSValues.uintMagic)
{
DDSHeaders.DDSHeader dDSHeader = new DDSHeaders.DDSHeader(binaryReader);
if (dDSHeader.ddspf.dwFourCC == DDSHeaders.DDSValues.uintDX10)
{
new DDSHeaders.DDSHeaderDX10(binaryReader);
}
Boolean alpha = (dDSHeader.dwFlags & 0x00000002) != 0;
Boolean fourcc = (dDSHeader.dwFlags & 0x00000004) != 0;
Boolean rgb = (dDSHeader.dwFlags & 0x00000040) != 0;
Boolean alphapixel = (dDSHeader.dwFlags & 0x00000001) != 0;
Boolean luminance = (dDSHeader.dwFlags & 0x00020000) != 0;
Boolean rgb888 = dDSHeader.ddspf.dwRBitMask == 0x000000ff && dDSHeader.ddspf.dwGBitMask == 0x0000ff00 && dDSHeader.ddspf.dwBBitMask == 0x00ff0000;
//Boolean bgr888 = dDSHeader.ddspf.dwRBitMask == 0x00ff0000 && dDSHeader.ddspf.dwGBitMask == 0x0000ff00 && dDSHeader.ddspf.dwBBitMask == 0x000000ff;
Boolean rgb565 = dDSHeader.ddspf.dwRBitMask == 0x0000F800 && dDSHeader.ddspf.dwGBitMask == 0x000007E0 && dDSHeader.ddspf.dwBBitMask == 0x0000001F;
Boolean argb4444 = dDSHeader.ddspf.dwABitMask == 0x0000f000 && dDSHeader.ddspf.dwRBitMask == 0x00000f00 && dDSHeader.ddspf.dwGBitMask == 0x000000f0 && dDSHeader.ddspf.dwBBitMask == 0x0000000f;
Boolean rbga4444 = dDSHeader.ddspf.dwABitMask == 0x0000000f && dDSHeader.ddspf.dwRBitMask == 0x0000f000 && dDSHeader.ddspf.dwGBitMask == 0x000000f0 && dDSHeader.ddspf.dwBBitMask == 0x00000f00;
Boolean mipmap = (dDSHeader.dwCaps & DDSHeaders.DDSPixelFormatCaps.MIPMAP) != (DDSHeaders.DDSPixelFormatCaps)0u;
Boolean isNormalMap = ((dDSHeader.ddspf.dwFlags & 524288u) != 0u || (dDSHeader.ddspf.dwFlags & 2147483648u) != 0u);
if (fourcc)
{
if (dDSHeader.ddspf.dwFourCC == DDSHeaders.DDSValues.uintDXT1)
{
map = new Texture2D((Int32)dDSHeader.dwWidth, (Int32)dDSHeader.dwHeight, TextureFormat.DXT1, mipmap);
map.LoadRawTextureData(binaryReader.ReadBytes((Int32)(binaryReader.BaseStream.Length - binaryReader.BaseStream.Position)));
}
else if (dDSHeader.ddspf.dwFourCC == DDSHeaders.DDSValues.uintDXT3)
{
map = new Texture2D((Int32)dDSHeader.dwWidth, (Int32)dDSHeader.dwHeight, (TextureFormat)11, mipmap);
map.LoadRawTextureData(binaryReader.ReadBytes((Int32)(binaryReader.BaseStream.Length - binaryReader.BaseStream.Position)));
}
else if (dDSHeader.ddspf.dwFourCC == DDSHeaders.DDSValues.uintDXT5)
{
map = new Texture2D((Int32)dDSHeader.dwWidth, (Int32)dDSHeader.dwHeight, TextureFormat.DXT5, mipmap);
map.LoadRawTextureData(binaryReader.ReadBytes((Int32)(binaryReader.BaseStream.Length - binaryReader.BaseStream.Position)));
}
else if (dDSHeader.ddspf.dwFourCC == DDSHeaders.DDSValues.uintDXT2)
{
Debug.Log("[Kopernicus] DXT2 not supported" + path);
}
else if (dDSHeader.ddspf.dwFourCC == DDSHeaders.DDSValues.uintDXT4)
{
Debug.Log("[Kopernicus] DXT4 not supported: " + path);
}
else if (dDSHeader.ddspf.dwFourCC == DDSHeaders.DDSValues.uintDX10)
{
Debug.Log("[Kopernicus] DX10 dds not supported: " + path);
}
else
fourcc = false;
}
if(!fourcc)
{
TextureFormat textureFormat = TextureFormat.ARGB32;
Boolean ok = true;
if (rgb && (rgb888 /*|| bgr888*/))
{
// RGB or RGBA format
textureFormat = alphapixel
? TextureFormat.RGBA32
: TextureFormat.RGB24;
}
else if (rgb && rgb565)
{
// Nvidia texconv B5G6R5_UNORM
textureFormat = TextureFormat.RGB565;
}
else if (rgb && alphapixel && argb4444)
{
// Nvidia texconv B4G4R4A4_UNORM
textureFormat = TextureFormat.ARGB4444;
}
else if (rgb && alphapixel && rbga4444)
{
textureFormat = TextureFormat.RGBA4444;
}
else if (!rgb && alpha != luminance)
{
// A8 format or Luminance 8
textureFormat = TextureFormat.Alpha8;
}
else
{
ok = false;
Debug.Log("[Kopernicus] Only DXT1, DXT5, A8, RGB24, RGBA32, RGB565, ARGB4444 and RGBA4444 are supported");
}
if (ok)
{
map = new Texture2D((Int32)dDSHeader.dwWidth, (Int32)dDSHeader.dwHeight, textureFormat, mipmap);
map.LoadRawTextureData(binaryReader.ReadBytes((Int32)(binaryReader.BaseStream.Length - binaryReader.BaseStream.Position)));
}
}
if (map != null)
if (upload)
map.Apply(false, unreadable);
}
else
Debug.Log("[Kopernicus] Bad DDS header.");
}
else
{
map = new Texture2D(2, 2);
map.LoadImage(System.IO.File.ReadAllBytes(path));
if (compress)
map.Compress(true);
if (upload)
map.Apply(false, unreadable);
}
}
catch (Exception e)
{
uncaught = false;
Debug.Log("[Kopernicus] failed to load " + path + " with exception " + e.Message);
}
if (map == null && uncaught)
{
Debug.Log("[Kopernicus] failed to load " + path);
}
map.name = path.Remove(0, (KSPUtil.ApplicationRootPath + "GameData/").Length);
}
else
Debug.Log("[Kopernicus] texture does not exist! " + path);
return map;
}
public static T FindMapSO<T>(String url) where T : MapSO
{
T retVal = Resources.FindObjectsOfTypeAll<T>().FirstOrDefault(m => m.name == url);
if (retVal != null)
{
return retVal;
}
Boolean modFound = false;
String trim = url.Replace("BUILTIN/", "");
String mBody = Regex.Replace(trim, @"/.*", "");
trim = Regex.Replace(trim, mBody + "/", "");
String mTypeName = Regex.Replace(trim, @"/.*", "");
String mName = Regex.Replace(trim, mTypeName + "/", "");
PSystemBody body = FindBody(PSystemManager.Instance.systemPrefab.rootBody, mBody);
if (body != null && body.pqsVersion != null)
{
Type mType = null;
try
{
mType = Type.GetType(mTypeName + ", Assembly-CSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
}
catch (Exception e)
{
Logger.Active.Log("MapSO grabber: Tried to grab " + url + " but type not found. VertexHeight type for reference = " + typeof (PQSMod_VertexHeightMap).FullName + ". Exception: " + e);
}
if (mType != null)
{
PQSMod[] mods = body.pqsVersion.GetComponentsInChildren<PQSMod>(true).Where(m => m.GetType() == mType).ToArray();
foreach (PQSMod m in mods.Where(m => m.name == mName))
{
modFound = true;
foreach (FieldInfo fi in m.GetType().GetFields().Where(fi => fi.FieldType == typeof (MapSO)))
{
retVal = fi.GetValue(m) as T;
break;
}
}
}
}
else
Logger.Active.Log("MapSO grabber: Tried to grab " + url + " but body not found.");
if (retVal == null)
{
if (modFound)
Logger.Active.Log("MapSO grabber: Tried to grab " + url + " but mods of correct name and type lack MapSO.");
else
Logger.Active.Log("MapSO grabber: Tried to grab " + url + " but could not find PQSMod of that type of the given name");
}
if (retVal != null)
retVal.name = url;
return retVal;
}
/// <summary>
/// Will remove all mods of given types (or all, if types null)
/// </summary>
/// <param name="types">If null, will remove all mods except blacklisted mods</param>
/// <param name="p">PQS to remove from</param>
/// <param name="blacklist">list of mod types to not remove (optional)</param>
public static void RemoveModsOfType(List<Type> types, PQS p, List<Type> blacklist = null)
{
Logger.Active.Log("Removing mods from pqs " + p.name);
List<PQSMod> cpMods = p.GetComponentsInChildren<PQSMod>(true).ToList();
Boolean addTypes = (types == null);
if(addTypes)
types = new List<Type>();
if (blacklist == null)
{
Logger.Active.Log("Creating blacklist");
blacklist = new List<Type>();
if(!types.Contains(typeof(PQSMod_CelestialBodyTransform)))
blacklist.Add(typeof(PQSMod_CelestialBodyTransform));
if(!types.Contains(typeof(PQSMod_MaterialSetDirection)))
blacklist.Add(typeof(PQSMod_MaterialSetDirection));
if(!types.Contains(typeof(PQSMod_UVPlanetRelativePosition)))
blacklist.Add(typeof(PQSMod_UVPlanetRelativePosition));
if(!types.Contains(typeof(PQSMod_QuadMeshColliders)))
blacklist.Add(typeof(PQSMod_QuadMeshColliders));
Logger.Active.Log("Blacklist count = " + blacklist.Count);
}
if (addTypes)
{
Logger.Active.Log("Adding all found PQSMods in pqs " + p.name);
foreach(PQSMod m in cpMods)
{
Type mType = m.GetType();
if (!types.Contains(mType) && !blacklist.Contains(mType))
{
Logger.Active.Log("Adding to removelist: " + mType);
types.Add(mType);
}
}
}
List<GameObject> toCheck = new List<GameObject>();
foreach (Type mType in types)
{
List<PQSMod> mods = cpMods.Where(m => m.GetType() == mType).ToList();
foreach (PQSMod delMod in mods)
{
if (delMod != null)
{
Logger.Active.Log("Removed mod " + mType.ToString());
if (!toCheck.Contains(delMod.gameObject))
toCheck.Add(delMod.gameObject);
delMod.sphere = null;
cpMods.Remove(delMod);
PQSMod.DestroyImmediate(delMod);
}
}
}
RemoveEmptyGO(toCheck);
}
static public void RemoveEmptyGO(List<GameObject> toCheck)
{
Int32 oCount = toCheck.Count;
Int32 nCount = oCount;
List<GameObject> toDestroy = new List<GameObject>();
do
{
oCount = nCount;
foreach (GameObject go in toCheck)
{