-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLODApp.cs
More file actions
3068 lines (2991 loc) · 162 KB
/
LODApp.cs
File metadata and controls
3068 lines (2991 loc) · 162 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
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
using LODGenerator.Common;
using LODGenerator.NifMain;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using StringList = System.Collections.Generic.List<string>;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Globalization;
using System.Text.RegularExpressions;
namespace LODGenerator
{
public class LODApp
{
private string worldspaceName;
private string outputDir;
private string terrainDir;
private string gameDir;
private LogFile logFile;
public List<QuadDesc> quadList;
public bool verbose;
public string texturesListFile;
public bool fixTangents;
public bool generateTangents;
public bool generateVertexColors;
public bool mergeShapes;
public bool useHDFlag;
public bool useOptimizer;
public bool useFadeNode;
public bool removeUnseenFaces;
public bool removeUnderwaterFaces;
public float removeUnseenZShift;
public bool ignoreMaterial;
public bool alphaDoublesided;
public bool useAlphaThreshold;
public bool useBacklightPower;
public bool useDecalFlag;
public bool dontGroup;
public bool skyblivionTexPath;
public bool removeBlocks;
public int flatLODLevelLODFlag;
public float globalScale;
public float eliminateSize;
public int southWestX;
public int southWestY;
public float atlasToleranceMin;
public float atlasToleranceMax;
public int lodLevelToGenerate;
public int lodX;
public int lodY;
public StringList ignoreTransRot;
public StringList HDTextureList;
public StringList notHDTextureList;
public StringList HDMeshList;
public StringList notHDMeshList;
public StringList PassThruMeshList;
private int quadLevel;
private int quadIndex;
private float quadOffset;
private static readonly Random getrandom = new Random();
private static readonly object syncLock = new object();
public static float GetRandomNumber()
{
lock (syncLock)
{
return (float)getrandom.NextDouble();
}
}
public LODApp(string wsn, string od, string sd, LogFile lf)
{
this.worldspaceName = wsn;
this.outputDir = od;
this.terrainDir = od + "\\..";
this.gameDir = sd;
this.logFile = lf;
this.quadList = new List<QuadDesc>();
this.verbose = false;
this.fixTangents = false;
this.generateTangents = true;
this.generateVertexColors = true;
this.mergeShapes = true;
this.useHDFlag = false;
this.useFadeNode = false;
this.removeUnseenFaces = false;
this.removeUnseenZShift = 0;
this.globalScale = 1f;
this.eliminateSize = 0.0f;
this.lodLevelToGenerate = Int32.MinValue;
}
private int cellquad(float pos, int southWest)
{
// properly address offset world coordinates to match CK
double xoffset = pos - ((southWest % this.quadLevel) * 4096);
int num1 = (int)((double)xoffset / (double)this.quadOffset);
if ((double)xoffset < 0.0)
{
--num1;
}
int num3 = num1 * this.quadLevel;
if (southWest % this.quadLevel != 0)
{
num3 += southWest % this.quadLevel;
}
return num3;
}
private List<QuadDesc> SortMeshesIntoQuads(List<StaticDesc> statics)
{
List<QuadDesc> list = new List<QuadDesc>();
bool flag = false;
foreach (StaticDesc staticDesc in statics)
{
int num3 = cellquad(staticDesc.x, this.southWestX);
int num4 = cellquad(staticDesc.y, this.southWestY);
for (int index = 0; index < list.Count; ++index)
{
if (num3 == list[index].x && num4 == list[index].y)
{
list[index].statics.Add(staticDesc);
flag = true;
break;
}
}
if (!flag)
{
QuadDesc quadDesc = new QuadDesc(true);
quadDesc.x = num3;
quadDesc.y = num4;
//quadDesc.statics = new List<StaticDesc>();
quadDesc.statics.Add(staticDesc);
//quadDesc.outValues = new OutDesc();
quadDesc.outValues.totalTriCount = 0;
quadDesc.outValues.reducedTriCount = 0;
//quadDesc.textureBlockIndex = new Dictionary<string, int>();
//quadDesc.textureBlockIndexPassThru = new Dictionary<string, int>();
//quadDesc.dataBlockIndex = new Dictionary<string, int>();
//
quadDesc.shaderBlockIndex = new Dictionary<string, int>();
list.Add(quadDesc);
}
flag = false;
}
return list;
}
private void GenerateMultibound(NiFile file, BSMultiBoundNode node, QuadDesc curQuad, BBox bb)
{
if ((Game.Mode != "fo3") || (Game.Mode == "fo3" && node.GetMultiBound() == -1))
{
BSMultiBound bsMultiBound = new BSMultiBound();
node.SetMultiBound(file.AddBlock((NiObject)bsMultiBound));
BSMultiBoundAABB bsMultiBoundAabb = new BSMultiBoundAABB();
bsMultiBound.SetData(file.AddBlock((NiObject)bsMultiBoundAabb));
float num1 = (float)curQuad.x * 4096f;
float num2 = (float)curQuad.y * 4096f;
bsMultiBoundAabb.SetPosition(new Vector3((float)(((double)num1 + (double)bb.px1 + ((double)num1 + (double)bb.px2)) / 2.0), (float)(((double)num2 + (double)bb.py1 + ((double)num2 + (double)bb.py2)) / 2.0), (float)(((double)bb.pz1 + (double)bb.pz2) / 2.0)));
bsMultiBoundAabb.SetExtent(new Vector3((float)(((double)bb.px2 - (double)bb.px1) / 2.0), (float)(((double)bb.py2 - (double)bb.py1) / 2.0), (float)(((double)bb.pz2 - (double)bb.pz1) / 2.0)));
}
}
private void GenerateSegments(QuadDesc curQuad, ref ShapeDesc shape)
{
// use x, y of object instead of boundingbox center to determine segment
SegmentDesc segmentDesc = new SegmentDesc();
if (this.quadLevel == 4)
{
int num1 = (int)((double)shape.x / ((double)this.quadOffset / 4.0));
int num2 = (int)((double)shape.y / ((double)this.quadOffset / 4.0));
if (num1 > 3)
num1 = 3;
if (num1 < 0)
num1 = 0;
if (num2 > 3)
num2 = 3;
if (num2 < 0)
num2 = 0;
segmentDesc.id = 4 * num1 + num2;
}
else
{
segmentDesc.id = 0;
}
segmentDesc.startTriangle = 0U;
segmentDesc.numTriangles = shape.geometry.GetNumTriangles();
shape.segments = new List<SegmentDesc>();
shape.segments.Add(segmentDesc);
}
private List<ShapeDesc> IterateNodes(QuadDesc quad, StaticDesc stat, int level, NiFile file, NiNode parentNode, Matrix44 parentTransform, float parentScale)
{
List<ShapeDesc> list = new List<ShapeDesc>();
if (parentNode == null || parentNode.IsHidden())
{
return list;
}
int nameIndex = parentNode.GetNameIndex();
string str = nameIndex != -1 ? file.GetStringAtIndex(nameIndex) : parentNode.GetName();
if (str != null && str.ToLower(CultureInfo.InvariantCulture).Contains("editormarker"))
{
return list;
}
Matrix44 parentTransform1 = parentNode.GetTransform() * parentTransform;
if (ignoreTransRot.Any(stat.staticModels[level].Contains) || Game.Mode.Contains("convert"))
{
parentTransform1 = parentTransform;
}
float parentScale1 = parentNode.GetScale() * parentScale;
uint numChildren = parentNode.GetNumChildren();
int numGeom = -1;
for (int index = 0; (long)index < (long)numChildren; ++index)
{
if (parentNode.GetChildAtIndex(index) != -1)
{
NiObject blockAtIndex = file.GetBlockAtIndex(parentNode.GetChildAtIndex(index));
if (blockAtIndex != null)
{
if (blockAtIndex.IsDerivedType("NiNode"))
{
list.AddRange((IEnumerable<ShapeDesc>)this.IterateNodes(quad, stat, level, file, (NiNode)blockAtIndex, parentTransform1, parentScale1));
}
else if (blockAtIndex.IsDerivedType("NiTriBasedGeom"))
{
numGeom++;
NiTriBasedGeom geom = (NiTriBasedGeom)blockAtIndex;
if (geom.GetSkinInstance() == -1)
{
ShapeDesc shapeDesc = new ShapeDesc();
if (!dontGroup && (stat.staticFlags & 1) == 1)
{
shapeDesc = this.GroupShape(quad, stat, file, geom, parentTransform1, parentScale1, numGeom);
}
else
{
shapeDesc = this.TransformShape(quad, stat, file, geom, parentTransform1, parentScale1, numGeom);
}
if (shapeDesc != null && shapeDesc.geometry != null)
{
//logFile.WriteLog(shapeDesc.material + shapeDesc.shaderType + shapeDesc.shaderHash);
list.Add(shapeDesc);
}
}
}
else if (blockAtIndex.IsDerivedType("BSTriShape"))
{
numGeom++;
BSTriShape geomOld = (BSTriShape)blockAtIndex;
NiTriBasedGeom geomNew = new NiTriBasedGeom();
if (index < numChildren)
{
geomNew.SetFlags(14);
geomNew.SetTranslation(geomOld.GetTranslation());
geomNew.SetRotation(geomOld.GetRotation());
geomNew.SetScale(geomOld.GetScale());
geomNew.SetNumProperties(geomOld.GetNumProperties());
for (int index2 = 0; (long)index2 < (long)geomOld.GetNumProperties(); ++index2)
{
geomNew.SetProperties(geomOld.GetProperty(index2));
}
geomNew.SetData(parentNode.GetChildAtIndex(index));
geomNew.SetBSProperty(0, geomOld.GetBSProperty(0));
geomNew.SetBSProperty(1, geomOld.GetBSProperty(1));
//geomNew.SetGeo(geomOld.GetGeom());
}
ShapeDesc shapeDesc = new ShapeDesc();
if (!dontGroup && (stat.staticFlags & 1) == 1)
{
shapeDesc = this.GroupShape(quad, stat, file, geomNew, parentTransform1, parentScale1, numGeom);
}
else
{
shapeDesc = this.TransformShape(quad, stat, file, geomNew, parentTransform1, parentScale1, numGeom);
}
if (shapeDesc != null && shapeDesc.geometry != null)
{
//logFile.WriteLog(shapeDesc.material + shapeDesc.shaderType + shapeDesc.shaderHash);
list.Add(shapeDesc);
}
}
}
else
{
//nothing
}
}
}
return list;
}
private ShapeDesc GroupShape(QuadDesc quad, StaticDesc stat, NiFile file, NiTriBasedGeom geom, Matrix44 parentTransform, float parentScale, int numGeom)
{
BBox bbox = new BBox(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue, float.MaxValue, float.MinValue);
ShapeDesc shapedesc = new ShapeDesc(this.gameDir, file, geom, stat, this.quadIndex, PassThruMeshList, skyblivionTexPath, useOptimizer, fixTangents, useDecalFlag, false, verbose, this.logFile);
parentTransform *= stat.transrot;
parentScale *= stat.transscale;
Matrix33 matrix33_1 = new Matrix33(true);
Matrix33 matrix33_2 = new Matrix33(true);
Matrix33 matrix33_3 = new Matrix33(true);
matrix33_1.SetRotationX(Utils.ToRadians(-stat.rotX));
matrix33_2.SetRotationY(Utils.ToRadians(-stat.rotY));
matrix33_3.SetRotationZ(Utils.ToRadians(-stat.rotZ));
Matrix44 matrix44 = new Matrix44(new Matrix33(true) * matrix33_1 * matrix33_2 * matrix33_3, new Vector3(stat.x, stat.y, stat.z), 1f);
List<Vector3> vertices = new List<Vector3>(shapedesc.geometry.GetVertices());
if (geom.GetClassName() == "NiTriStrips")
{
List<int> extradatalist = geom.GetExtraData();
if (extradatalist.Count == 1)
{
NiBinaryExtraData extradata = (NiBinaryExtraData)file.GetBlockAtIndex(extradatalist[0]);
shapedesc.geometry.SetTangents(extradata.GetTangents());
shapedesc.geometry.SetBitangents(extradata.GetBitangents());
}
}
for (int index = 0; index < vertices.Count; ++index)
{
bbox.GrowByVertex(vertices[index]);
if (Game.Mode != "merge4" && Game.Mode != "merge5")
{
vertices[index] /= (float)this.quadLevel;
}
}
shapedesc.geometry.SetVertices(vertices);
shapedesc.x = stat.x;
shapedesc.y = stat.y;
shapedesc.boundingBox = bbox;
shapedesc.enableParent = stat.enableParent;
shapedesc.isHighDetail = false;
// only snow/ash shader care about HD flag -> use vertex color alpha for shader
if (stat.materialName != "" && useHDFlag)
{
if ((stat.staticFlags & 131072) == 131072)
{
if (HDMeshList.Any(stat.staticModels[this.quadIndex].ToLower(CultureInfo.InvariantCulture).Contains))
{
shapedesc.isHighDetail = true;
}
else if (notHDMeshList.Any(stat.staticModels[this.quadIndex].ToLower(CultureInfo.InvariantCulture).Contains))
{
shapedesc.isHighDetail = false;
}
else if (this.quadLevel == 4)
{
shapedesc.isHighDetail = true;
}
}
}
if (shapedesc.name != "")
{
string key = (stat.refID + "_" + numGeom + "_" + shapedesc.name).ToLower(CultureInfo.InvariantCulture);
if (AltTextureList.Contains(key))
{
AltTextureDesc altTexDesc = new AltTextureDesc();
altTexDesc = AltTextureList.Get(key);
if ((shapedesc.textures[0].Contains("\\lod") && altTexDesc.textures[0].Contains("\\lod")) || ((!shapedesc.textures[0].Contains("\\lod") && !altTexDesc.textures[0].Contains("\\lod"))))
{
string s = "";
for (int index = 0; index < altTexDesc.textures.Count(); index++)
{
shapedesc.textures[index] = altTexDesc.textures[index];
s += altTexDesc.textures[index];
}
if (s == "")
{
return (ShapeDesc)null;
}
}
}
}
if (shapedesc.isPassThru)
{
shapedesc.hasVertexColor = shapedesc.geometry.HasVertexColors();
}
else
{
//shapedesc.geometry = shapedesc.geometry.ReUV(shapedesc, shapedesc.textures[0], logFile, verbose);
//shapedesc = shapedesc.ReUV(logFile, verbose);
if (AtlasList.Contains(shapedesc.textures[0]))
{
if (this.UVAtlas(shapedesc.geometry, shapedesc.textures[0], stat))
{
string[] strArray = new string[10] { "", "", "", "", "", "", "", "", "", "" };
strArray[0] = AtlasList.Get(shapedesc.textures[0]).AtlasTexture;
strArray[1] = AtlasList.Get(shapedesc.textures[0]).AtlasTextureN;
if (shapedesc.textures[2] != "")
{
strArray[2] = AtlasList.Get(shapedesc.textures[2]).AtlasTexture;
}
if (Game.Mode == "fo4")
{
strArray[7] = AtlasList.Get(shapedesc.textures[0]).AtlasTextureS;
}
shapedesc.textures = strArray;
shapedesc.TextureClampMode = 0U;
shapedesc.isHighDetail = false;
}
}
else
{
if (useOptimizer && quadLevel != 4 && shapedesc.textures[0].ToLower(CultureInfo.InvariantCulture).Contains("mountainslab01"))
{
string[] strArray = new string[10] { "", "", "", "", "", "", "", "", "", "" };
strArray[0] = "textures\\landscape\\mountains\\mountainslab02.dds";
strArray[1] = "textures\\landscape\\mountains\\mountainslab02_n.dds";
shapedesc.textures = strArray;
}
if (notHDTextureList.Any(shapedesc.textures[0].Contains))
{
if (this.verbose && shapedesc.textures[0].Contains("dyndolodtreelod"))
{
logFile.WriteLog("No atlas for " + shapedesc.textures[0] + " in " + stat.staticModels[this.quadIndex]);
}
}
else
{
if (!useHDFlag && stat.materialName != "")
{
shapedesc.isHighDetail = true;
}
}
}
if (notHDTextureList.Any(shapedesc.textures[0].Contains))
{
shapedesc.isHighDetail = false;
}
if (HDTextureList.Any(shapedesc.textures[0].Contains) && stat.materialName != "")
{
shapedesc.isHighDetail = true;
}
if (shapedesc.textures[0].Contains("dyndolod\\lod"))
{
shapedesc.TextureClampMode = 0U;
shapedesc.isHighDetail = false;
}
}
if (!shapedesc.isPassThru && (!this.generateVertexColors || (this.quadLevel != 4 && !shapedesc.isHighDetail)))
{
shapedesc.hasVertexColor = false;
}
quad.outValues.totalTriCount += shapedesc.geometry.GetNumTriangles();
quad.outValues.reducedTriCount += shapedesc.geometry.GetNumTriangles();
this.GenerateSegments(quad, ref shapedesc);
return shapedesc;
}
private ShapeDesc TransformShape(QuadDesc quad, StaticDesc stat, NiFile file, NiTriBasedGeom geom, Matrix44 parentTransform, float parentScale, int numGeom)
{
BBox bbox = new BBox(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue, float.MaxValue, float.MinValue);
ShapeDesc shapedesc = new ShapeDesc(this.gameDir, file, geom, stat, this.quadIndex, PassThruMeshList, skyblivionTexPath, useOptimizer, fixTangents, useDecalFlag, false, verbose, this.logFile);
if (shapedesc.name != "")
{
string key = (stat.refID + "_" + numGeom + "_" + shapedesc.name).ToLower(CultureInfo.InvariantCulture);
if (AltTextureList.Contains(key))
{
AltTextureDesc altTexDesc = new AltTextureDesc();
altTexDesc = AltTextureList.Get(key);
if ((shapedesc.textures[0].Contains("\\lod") && altTexDesc.textures[0].Contains("\\lod")) || ((!shapedesc.textures[0].Contains("\\lod") && !altTexDesc.textures[0].Contains("\\lod"))))
{
string s = "";
for (int index = 0; index < altTexDesc.textures.Count(); index++)
{
shapedesc.textures[index] = altTexDesc.textures[index];
s += altTexDesc.textures[index];
}
if (s == "")
{
return (ShapeDesc)null;
}
}
}
}
if (AtlasList.Contains(shapedesc.textures[0]))
{
bool ok = false;
for (int index = 0; index < shapedesc.textures.Count(); index++)
{
if (Game.Mode == "tes5" || Game.Mode == "sse" || Game.Mode == "fo4")
{
if (Game.Mode != "fo4" && (shapedesc.textures[index] == "" || shapedesc.textures[index] == shapedesc.textures[0] || shapedesc.textures[index] == shapedesc.textures[1]))
{
ok = true;
}
else if (Game.Mode == "fo4" && (shapedesc.textures[index] == "" || shapedesc.textures[index] == shapedesc.textures[0] || shapedesc.textures[index] == shapedesc.textures[1] || shapedesc.textures[index] == shapedesc.textures[7]))
{
ok = true;
}
else
{
ok = false;
break;
}
}
else
{
ok = true;
}
}
if (Game.Mode == "tes5" || Game.Mode == "sse")
{
shapedesc = shapedesc.ReUV(logFile, verbose);
if (!AtlasList.Contains(shapedesc.textures[0]))
{
ok = false;
}
}
if (ok && this.UVAtlas(shapedesc.geometry, shapedesc.textures[0], stat))
{
string[] strArray = new string[10] { "", "", "", "", "", "", "", "", "", "" };
for (int index = 0; index < shapedesc.textures.Count(); index++)
{
if (shapedesc.textures[index] == shapedesc.textures[0])
{
strArray[index] = AtlasList.Get(shapedesc.textures[0]).AtlasTexture;
}
if (shapedesc.textures[index] == shapedesc.textures[1])
{
strArray[index] = AtlasList.Get(shapedesc.textures[0]).AtlasTextureN;
}
if (Game.Mode == "fo4" && shapedesc.textures[index] == shapedesc.textures[7])
{
strArray[index] = AtlasList.Get(shapedesc.textures[0]).AtlasTextureS;
}
}
shapedesc.textures = strArray;
shapedesc.TextureClampMode = 0U;
shapedesc.isHighDetail = false;
}
}
if (verbose && !shapedesc.geometry.HasVertexColors() && shapedesc.hasVertexColor)
{
if (!stat.staticModels[this.quadIndex].Contains("glacierrubbletrim0"))
{
logFile.WriteLog("Vertex Colors Flag, but no vertex colors in " + stat.staticModels[this.quadIndex]);
}
}
parentTransform *= stat.transrot;
parentScale *= stat.transscale;
float _x = stat.x - (float)quad.x * 4096f;
float _y = stat.y - (float)quad.y * 4096f;
Matrix33 matrix33_1 = new Matrix33(true);
Matrix33 matrix33_2 = new Matrix33(true);
Matrix33 matrix33_3 = new Matrix33(true);
matrix33_1.SetRotationX(Utils.ToRadians(-stat.rotX));
matrix33_2.SetRotationY(Utils.ToRadians(-stat.rotY));
matrix33_3.SetRotationZ(Utils.ToRadians(-stat.rotZ));
Matrix44 matrix44 = new Matrix44(new Matrix33(true) * matrix33_1 * matrix33_2 * matrix33_3, new Vector3(_x, _y, stat.z), 1f);
List<Triangle> triangles = new List<Triangle>(shapedesc.geometry.GetTriangles());
List<Vector3> vertices = new List<Vector3>(shapedesc.geometry.GetVertices());
List<Vector3> normals = new List<Vector3>(shapedesc.geometry.GetNormals());
List<Vector3> tangents = new List<Vector3>(shapedesc.geometry.GetTangents());
List<Vector3> bitangents = new List<Vector3>(shapedesc.geometry.GetBitangents());
List<UVCoord> uvcoords = new List<UVCoord>(shapedesc.geometry.GetUVCoords());
for (int index = 0; index < vertices.Count; ++index)
{
vertices[index] *= geom.GetScale() * parentScale;
vertices[index] *= geom.GetTransform() * parentTransform;
vertices[index] *= stat.scale;
vertices[index] *= matrix44;
if (shapedesc.geometry.HasNormals())
{
normals[index] *= parentTransform.RemoveTranslation() * geom.GetTransform().RemoveTranslation();
normals[index] *= matrix44.RemoveTranslation();
if (this.generateTangents)
{
// adjust tangents as well
if (tangents.Count != 0)
{
tangents[index] *= parentTransform.RemoveTranslation() * geom.GetTransform().RemoveTranslation();
tangents[index] *= matrix44.RemoveTranslation();
}
// adjust bitangents as well
if (bitangents.Count != 0)
{
bitangents[index] *= parentTransform.RemoveTranslation() * geom.GetTransform().RemoveTranslation();
bitangents[index] *= matrix44.RemoveTranslation();
}
}
}
bbox.GrowByVertex(vertices[index]);
if (Game.Mode != "merge4" && Game.Mode != "merge5")
{
vertices[index] /= (float)this.quadLevel;
}
}
shapedesc.geometry.SetVertices(vertices);
if (shapedesc.geometry.HasNormals())
{
shapedesc.geometry.SetNormals(normals);
}
//if (shapedesc.geometry.HasTangents())
//{
// shapedesc.geometry.SetTangents(tangents);
//}
//if (shapedesc.geometry.HasBitangents())
//{
// shapedesc.geometry.SetBitangents(bitangents);
//}
// relative x, y for segment
shapedesc.x = _x;
shapedesc.y = _y;
shapedesc.boundingBox = bbox;
if (shapedesc.material == "")
{
shapedesc.material = stat.materialName;
}
shapedesc.enableParent = stat.enableParent;
shapedesc.isHighDetail = false;
if (useHDFlag && stat.materialName != "")
{
if ((stat.staticFlags & 131072) == 131072)
{
if (HDMeshList.Any(stat.staticModels[this.quadIndex].ToLower(CultureInfo.InvariantCulture).Contains))
{
shapedesc.isHighDetail = true;
}
else if (notHDMeshList.Any(stat.staticModels[this.quadIndex].ToLower(CultureInfo.InvariantCulture).Contains))
{
shapedesc.isHighDetail = false;
}
else
{
shapedesc.isHighDetail = true;
}
}
}
if (shapedesc.isPassThru)
{
shapedesc.hasVertexColor = shapedesc.geometry.HasVertexColors();
}
else
{
if (useOptimizer && quadLevel != 4 && shapedesc.textures[0].ToLower(CultureInfo.InvariantCulture).Contains("mountainslab01"))
{
string[] strArray = new string[10] { "", "", "", "", "", "", "", "", "", "" };
strArray[0] = "textures\\landscape\\mountains\\mountainslab02.dds";
strArray[1] = "textures\\landscape\\mountains\\mountainslab02_n.dds";
shapedesc.textures = strArray;
}
if (notHDTextureList.Any(shapedesc.textures[0].Contains))
{
if (this.verbose && shapedesc.textures[0].Contains("dyndolodtreelod"))
{
logFile.WriteLog("No atlas for " + shapedesc.textures[0] + " in " + stat.staticModels[this.quadIndex]);
}
}
else
{
if (!useHDFlag && stat.materialName != "")
{
shapedesc.isHighDetail = true;
}
}
if (notHDTextureList.Any(shapedesc.textures[0].Contains))
{
shapedesc.isHighDetail = false;
}
if (HDTextureList.Any(shapedesc.textures[0].Contains) && stat.materialName != "")
{
shapedesc.isHighDetail = true;
}
if (shapedesc.textures[0].Contains("dyndolod\\lod"))
{
shapedesc.TextureClampMode = 0U;
shapedesc.isHighDetail = false;
}
}
if (this.generateTangents && (!useOptimizer || (useOptimizer && (this.quadLevel == 4 || shapedesc.isHighDetail || shapedesc.isPassThru))))
{
shapedesc.geometry.SetTangents(tangents);
shapedesc.geometry.SetBitangents(bitangents);
}
else
{
if (!Game.Mode.Contains("merge") && !Game.Mode.Contains("convert"))
{
shapedesc.geometry.SetTangents(new List<Vector3>());
shapedesc.geometry.SetBitangents(new List<Vector3>());
}
}
if (!shapedesc.isPassThru && (!this.generateVertexColors || (this.quadLevel != 4 && !shapedesc.isHighDetail)))
{
shapedesc.hasVertexColor = false;
}
if (this.removeUnseenFaces && quad.hasTerrainVertices)
{
this.RemoveUnseenFaces(quad, shapedesc);
if ((int)shapedesc.geometry.GetNumTriangles() == 0)
{
return (ShapeDesc)null;
}
}
else
{
quad.outValues.totalTriCount += shapedesc.geometry.GetNumTriangles();
quad.outValues.reducedTriCount += shapedesc.geometry.GetNumTriangles();
}
this.GenerateSegments(quad, ref shapedesc);
return shapedesc;
}
private bool UVAtlas(Geometry geometry, string texture, StaticDesc stat)
{
List<UVCoord> uvcoords = geometry.GetUVCoords();
List<UVCoord> uvcoords2 = new List<UVCoord>();
for (int index = 0; index < uvcoords.Count; ++index)
{
float u = uvcoords[index][0];
float v = uvcoords[index][1];
//logFile.WriteLog("B " + u + ", " + v);
if (u < atlasToleranceMin || u > atlasToleranceMax || v < atlasToleranceMin || u > atlasToleranceMax)
{
if (this.verbose && !texture.Contains("glacierslablod"))
{
logFile.WriteLog("Out of range " + atlasToleranceMin + " <= " + u + ", " + v + " <= " + atlasToleranceMax + " for " + texture + " in " + stat.staticModels[this.quadIndex]);
}
return false;
}
u = Math.Max(u, AtlasList.Get(texture).minU);
v = Math.Max(v, AtlasList.Get(texture).minV);
u = Math.Min(u, AtlasList.Get(texture).maxU);
v = Math.Min(v, AtlasList.Get(texture).maxV);
u *= AtlasList.Get(texture).scaleU;
v *= AtlasList.Get(texture).scaleV;
u += AtlasList.Get(texture).posU;
v += AtlasList.Get(texture).posV;
UVCoord coords = new UVCoord(u, v);
uvcoords2.Add(coords);
}
geometry.SetUVCoords(uvcoords2);
return true;
}
private float GetTriangleHeight(List<Vector3> verts, List<Triangle> tris, Vector3 pt)
{
float result = float.MinValue;
//Parallel.For(0, tris.Count - 1, (index, state) => {
for (int index = 0; index < tris.Count; index++)
{
Triangle triangle = tris[index];
float u;
float v;
if (Utils.PointInTriangle(new Vector2(pt[0], pt[1]),
new Vector2(verts[(int)triangle[0]][0], verts[(int)triangle[0]][1]),
new Vector2(verts[(int)triangle[1]][0], verts[(int)triangle[1]][1]),
new Vector2(verts[(int)triangle[2]][0], verts[(int)triangle[2]][1]), out u, out v))
{
Vector3 vector3_1 = verts[(int)triangle[0]];
Vector3 vector3_2 = verts[(int)triangle[1]];
Vector3 vector3_3 = verts[(int)triangle[2]];
result = (vector3_2[2] - vector3_1[2]) * v + vector3_1[2] + ((vector3_3[2] - vector3_1[2]) * u + vector3_1[2]) - vector3_1[2];
break; //state.Stop();
}
};
return result;
}
private void RemoveUnseenFaces(QuadDesc quad, ShapeDesc shapedesc)
{
List<Triangle> triangles = shapedesc.geometry.GetTriangles();
List<Vector3> vertices = shapedesc.geometry.GetVertices();
Dictionary<ushort, bool> whatever = new Dictionary<ushort, bool>();
float zShift = removeUnseenZShift / this.quadLevel;
int count = triangles.Count;
quad.outValues.totalTriCount += count;
int loops = 1;
if (this.removeUnderwaterFaces)
{
loops = 0;
}
for (int loop = loops; loop < 2; loop++)
{
for (int index = 0; index < triangles.Count; ++index)
{
QuadDesc quadCurrent = new QuadDesc(true);
//List<Triangle> trianglesCompare = new List<Triangle>();
//List<Vector3> verticesCompare = new List<Vector3>();
bool[] vertexBelow = new bool[3];
for (int index1 = 0; index1 < 3; index1++)
{
ushort tri = triangles[index][index1];
if (whatever.ContainsKey(tri))
{
if (!whatever[tri])
{
break;
}
else
{
vertexBelow[index1] = true;
}
}
else
{
//logFile.WriteLog(shapedesc.name + " - " + triangles[index][index1] + " = " + verticesUnder[triangles[index][index1]]);
Vector3 vertex1 = vertices[triangles[index][index1]];
//float x = vertex[0];
//float y = vertex[1];
/*int vertexQuadx = quad.x + cellquad(x * quadLevel, southWestX);
int vertexQuady = quad.y + cellquad(y * quadLevel, southWestY);
if (quad.x != vertexQuadx || quad.y != vertexQuady)
{
for (int index3 = 0; index3 < this.quadList.Count; ++index3)
{
if (vertexQuadx == this.quadList[index3].x && vertexQuady == this.quadList[index3].y)
{
quadCurrent = this.quadList[index3];
x -= (vertexQuadx - quad.x) / quadLevel * 4096;
y -= (vertexQuady - quad.y) / quadLevel * 4096;
break;
}
}
}
else*/
{
quadCurrent = quad;
}
if (!quadCurrent.hasTerrainVertices)
{
continue;
}
//Vector3 vertex1 = new Vector3(x, y, vertex[2]);
List<Triangle> trianglesTerrain = new List<Triangle>();
List<Vector3> verticesTerrain = new List<Vector3>();
if (loop == 0)
{
if (quadCurrent.waterQuadTree != null)
{
//logFile.WriteLog("w " + shapedesc.boundingBox.pz1 + " = " + shapedesc.boundingBox.pz2 + " === " + quadCurrent.boundingBox.pz1 + " = " + quadCurrent.boundingBox.pz2);
if (vertex1[2] < quadCurrent.boundingBox.pz2)
{
trianglesTerrain = quadCurrent.waterQuadTree.entirequad.triangles;
verticesTerrain = quadCurrent.waterQuadTree.vertices;
}
else
{
vertexBelow[index1] = false;
break;
}
}
}
else
{
//logFile.WriteLog("t " + shapedesc.boundingBox.pz1 + " = " + shapedesc.boundingBox.pz2 + " === " + quadCurrent.boundingBox.pz1 + " = " + quadCurrent.boundingBox.pz2);
if (vertex1[2] < quadCurrent.boundingBox.pz2)
{
trianglesTerrain = quadCurrent.terrainQuadTree.GetSegment(vertex1, quadLevel);
verticesTerrain = quadCurrent.terrainQuadTree.vertices;
}
else
{
vertexBelow[index1] = false;
whatever.Add(tri, false);
break;
}
}
if (trianglesTerrain == null)
{
vertexBelow[index1] = true;
whatever.Add(tri, true);
}
else
{
if (trianglesTerrain.Count != 0)
{
float num1 = GetTriangleHeight(verticesTerrain, trianglesTerrain, vertex1) - zShift;
if (vertex1[2] < num1)
{
vertexBelow[index1] = true;
whatever.Add(tri, true);
}
else
{
vertexBelow[index1] = false;
if (loop == 1)
{
whatever.Add(tri, false);
}
break;
}
}
else
{
if (loop == 1)
{
vertexBelow[index1] = false;
whatever.Add(tri, false);
break;
}
}
}
}
}
if (vertexBelow[0] && vertexBelow[1] && vertexBelow[2])
{
triangles[index] = new Triangle(0, 0, 0);
triangles.RemoveAt(index);
--index;
}
}
}
shapedesc.geometry.SetTriangles(triangles);
if (useOptimizer)
{
shapedesc.geometry.RemoveDuplicate(true);
}
else
{
shapedesc.geometry.RemoveUnused();
}
quad.outValues.reducedTriCount += triangles.Count;
}
private List<ShapeDesc> ParseNif(QuadDesc quad, StaticDesc curStat, int level)
{
NiFile file1 = new NiFile();
if (curStat.staticModels[level].Contains(".nif"))
{
try
{
file1.Read(this.gameDir, curStat.staticModels[level], this.logFile);
}
catch
{
Console.WriteLine(curStat.staticModels[level] + " can not read file");
}
return this.IterateNodes(quad, curStat, level, file1, (NiNode)file1.GetBlockAtIndex(0), new Matrix44(true), 1f);
}
if (curStat.staticModels[level].Contains(".dds"))
{
if (FlatList.Contains(curStat.staticModels[level]))
{
//this.logFile.WriteLog(curStat.staticModels[level] + " = " + FlatList.Get(curStat.staticModels[level]).width + ", " + FlatList.Get(curStat.staticModels[level]).height);
BSFadeNode rootFadeNode = new BSFadeNode();
rootFadeNode.SetNameIndex(file1.AddString("LODGenPassThru"));
NiTriShape nitrishape = new NiTriShape();
nitrishape.SetNameIndex(file1.AddString("LODGenPassThru"));
nitrishape.SetFlags(14);
nitrishape.SetFlags2(8);
nitrishape.SetScale(FlatList.Get(curStat.staticModels[level]).scale);
NiTriShapeData data = new NiTriShapeData();
List<Vector3> vertices = new List<Vector3>();
vertices.Add(new Vector3(-FlatList.Get(curStat.staticModels[level]).width / 2, 0, FlatList.Get(curStat.staticModels[level]).shiftZ - 5));
vertices.Add(new Vector3(+FlatList.Get(curStat.staticModels[level]).width / 2, 0, FlatList.Get(curStat.staticModels[level]).shiftZ - 5));
vertices.Add(new Vector3(+FlatList.Get(curStat.staticModels[level]).width / 2, 0, FlatList.Get(curStat.staticModels[level]).height + FlatList.Get(curStat.staticModels[level]).shiftZ - 5));
vertices.Add(new Vector3(-FlatList.Get(curStat.staticModels[level]).width / 2, 0, FlatList.Get(curStat.staticModels[level]).height + FlatList.Get(curStat.staticModels[level]).shiftZ - 5));
vertices.Add(new Vector3(0, -FlatList.Get(curStat.staticModels[level]).width / 2, FlatList.Get(curStat.staticModels[level]).shiftZ + 5));
vertices.Add(new Vector3(0, +FlatList.Get(curStat.staticModels[level]).width / 2, FlatList.Get(curStat.staticModels[level]).shiftZ + 5));
vertices.Add(new Vector3(0, +FlatList.Get(curStat.staticModels[level]).width / 2, FlatList.Get(curStat.staticModels[level]).height + FlatList.Get(curStat.staticModels[level]).shiftZ + 5));
vertices.Add(new Vector3(0, -FlatList.Get(curStat.staticModels[level]).width / 2, FlatList.Get(curStat.staticModels[level]).height + FlatList.Get(curStat.staticModels[level]).shiftZ + 5));
List<UVCoord> uv = new List<UVCoord>();
uv.Add(new UVCoord(0.0f, 1.0f));
uv.Add(new UVCoord(1.0f, 1.0f));
uv.Add(new UVCoord(1.0f, 0.0f));
uv.Add(new UVCoord(0.0f, 0.0f));
uv.Add(new UVCoord(0.0f, 1.0f));
uv.Add(new UVCoord(1.0f, 1.0f));
uv.Add(new UVCoord(1.0f, 0.0f));
uv.Add(new UVCoord(0.0f, 0.0f));
List<Triangle> triangles = new List<Triangle>();
triangles.Add(new Triangle(0, 1, 2));
triangles.Add(new Triangle(2, 3, 0));
triangles.Add(new Triangle(4, 5, 6));
triangles.Add(new Triangle(6, 7, 4));
data.SetNumVertices(8);
data.SetHasVertices(true);
data.SetVertices(vertices);
data.SetNumUVSets(1);
data.SetHasTangents(true);
data.SetHasNormals(true);
data.SetNormals(FlatList.Get(curStat.staticModels[level]).normals);
data.SetTangents(FlatList.Get(curStat.staticModels[level]).tangents);
data.SetBitangents(FlatList.Get(curStat.staticModels[level]).bitangents);
data.SetCenter(new Vector3(0.0f, 0.0f, (FlatList.Get(curStat.staticModels[level]).height + FlatList.Get(curStat.staticModels[level]).shiftZ) / 2));