Skip to content

Commit 794217c

Browse files
committed
Changed exclude faces and exclude models in bsp map to percentages.
1 parent a2a80b3 commit 794217c

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

BSP/BSPMap.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@ public class BSPMap
2020
public string mapName { get; private set; }
2121
public string mapDir { get; private set; }
2222
public static bool combineMeshesWithSameTexture;
23-
public static bool excludeModels;
24-
public static bool excludeMapFaces;
23+
private static float modelLoadPercent = 1;
24+
public static float ModelLoadPercent { get { return Mathf.Clamp01(modelLoadPercent); } set { modelLoadPercent = value; } }
25+
private static float faceLoadPercent = 1;
26+
public static float FaceLoadPercent { get { return Mathf.Clamp01(faceLoadPercent); } set { faceLoadPercent = value; } }
2527
public static bool applyLightmaps;
2628
public static string vpkLoc;
2729
public GameObject gameObject { get; private set; }
2830

29-
//private Material noTexture;
30-
//private Dictionary<SourceTexture, Material> materialsCreated = new Dictionary<SourceTexture, Material>();
31-
3231
private List<FaceMesh> allFaces = new List<FaceMesh>();
3332
private StaticPropData[] staticProps;
34-
//private SourceModel[] staticProps;
3533
public Dictionary<int, SourceLightmap> lightmaps; //Maps from face lightofs to lightmap
3634
#endregion
3735

@@ -109,7 +107,7 @@ public List<string> GetDependencies(CancellationToken cancelToken)
109107
// added by other dependencies, those archives will not be added. That would require us to read the materials and models to get what textures they use.
110108

111109
#region Map face textures dependencies
112-
if (!excludeMapFaces)
110+
if (FaceLoadPercent > 0)
113111
{
114112
foreach (dface_t face in bspParser.faces)
115113
{
@@ -135,7 +133,7 @@ public List<string> GetDependencies(CancellationToken cancelToken)
135133
#endregion
136134

137135
#region Model dependencies
138-
if (!excludeModels)
136+
if (ModelLoadPercent > 0)
139137
{
140138
for (int i = 0; i < bspParser.staticProps.staticPropInfo.Length; i++)
141139
{
@@ -185,20 +183,19 @@ public void ParseFile(CancellationToken cancelToken, Action<float, string> onPro
185183
//if (!cancelToken.IsCancellationRequested)
186184
// lightmaps = bspParser.GetLightmaps(cancelToken);
187185

188-
int facesCount = excludeMapFaces ? 0 : bspParser.faces.Length;
189-
int propsCount = excludeModels ? 0 : bspParser.staticProps.staticPropInfo.Length;
186+
int facesCount = Mathf.RoundToInt(bspParser.faces.Length * FaceLoadPercent);
187+
int propsCount = Mathf.RoundToInt(bspParser.staticProps.staticPropInfo.Length * ModelLoadPercent);
190188
totalItemsToLoad = facesCount + propsCount;
191189

192190
bool validVPK = vpkParser.IsValid();
193191

194192
currentMessage = "Parsing Faces";
195193
onProgressChanged?.Invoke(PercentLoaded, currentMessage);
196-
if (!excludeMapFaces)
197-
ReadFaces(bspParser, validVPK ? vpkParser : null, cancelToken, onProgressChanged);
194+
ReadFaces(bspParser, validVPK ? vpkParser : null, cancelToken, onProgressChanged);
198195

199196
currentMessage = "Loading Static Props";
200197
onProgressChanged?.Invoke(PercentLoaded, currentMessage);
201-
if (validVPK && !excludeModels)
198+
if (validVPK)
202199
ReadStaticProps(bspParser, vpkParser, cancelToken, onProgressChanged);
203200
}
204201

@@ -234,7 +231,7 @@ private bool IsUndesiredTexture(string textureLocation, texflags tf)
234231
}
235232
private void ReadFaces(BSPParser bspParser, VPKParser vpkParser, CancellationToken cancelToken, Action<float, string> onProgressChanged = null)
236233
{
237-
for (int i = 0; i < bspParser.faces.Length; i++)
234+
for (int i = 0; i < Mathf.RoundToInt(bspParser.faces.Length * FaceLoadPercent); i++)
238235
{
239236
if (cancelToken.IsCancellationRequested)
240237
return;
@@ -497,8 +494,9 @@ private void AddFaceMesh(FaceMesh faceMesh, bool combine)
497494

498495
private void ReadStaticProps(BSPParser bspParser, VPKParser vpkParser, CancellationToken cancelToken, Action<float, string> onProgressChanged = null)
499496
{
500-
staticProps = new StaticPropData[bspParser.staticProps.staticPropInfo.Length];
501-
for (int i = 0; i < bspParser.staticProps.staticPropInfo.Length; i++)
497+
int staticPropCount = Mathf.RoundToInt(bspParser.staticProps.staticPropInfo.Length * ModelLoadPercent);
498+
staticProps = new StaticPropData[staticPropCount];
499+
for (int i = 0; i < staticPropCount; i++)
502500
{
503501
if (cancelToken.IsCancellationRequested)
504502
return;

Examples/Scripts/LoadMapAsyncExample.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ public class LoadMapAsyncExample : MonoBehaviour
1010
public string mapPath = @"C:\Program Files (x86)\Steam\steamapps\common\Counter-Strike Global Offensive\csgo\maps\ar_monastery.bsp";
1111
[Space(10)]
1212
public bool combineMeshesWithSameTextures = true;
13-
public bool excludeMapFaces = false;
14-
public bool excludeModels = false;
13+
[Range(0, 1)]
14+
public float faceLoadPercent = 1;
15+
[Range(0, 1)]
16+
public float modelLoadPercent = 1;
1517
public bool flatTextures = false;
1618
public int maxTextureSize = 2048;
1719

@@ -28,7 +30,7 @@ private void Update()
2830
}
2931
private void OnEnable()
3032
{
31-
map = LoadMap(vpkPath, mapPath, combineMeshesWithSameTextures, excludeMapFaces, excludeModels, flatTextures, maxTextureSize);
33+
map = LoadMap(vpkPath, mapPath, combineMeshesWithSameTextures, faceLoadPercent, modelLoadPercent, flatTextures, maxTextureSize);
3234
}
3335
private void OnDisable()
3436
{
@@ -40,14 +42,14 @@ private void OnDisable()
4042
map = null;
4143
}
4244

43-
public BSPMap LoadMap(string vpkLoc, string mapLoc, bool combineMeshesWithSameTextures = true, bool excludeMapFaces = false, bool excludeModels = false, bool flatTextures = false, int maxTextureSize = 2048)
45+
public BSPMap LoadMap(string vpkLoc, string mapLoc, bool combineMeshesWithSameTextures = true, float faceLoadPercent = 1, float modelLoadPercent = 1, bool flatTextures = false, int maxTextureSize = 2048)
4446
{
4547
BSPMap.vpkLoc = vpkLoc;
4648
BSPMap map = new BSPMap(mapLoc);
4749

4850
BSPMap.combineMeshesWithSameTexture = combineMeshesWithSameTextures;
49-
BSPMap.excludeMapFaces = excludeMapFaces;
50-
BSPMap.excludeModels = excludeModels;
51+
BSPMap.FaceLoadPercent = faceLoadPercent;
52+
BSPMap.ModelLoadPercent = modelLoadPercent;
5153
SourceTexture.averageTextures = flatTextures;
5254
SourceTexture.maxTextureSize = maxTextureSize;
5355

Helpers/SourceMapLoader.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ public class SourceMapLoader : MonoBehaviour
77
public string mapPath = @"C:\Program Files (x86)\Steam\steamapps\common\Counter-Strike Global Offensive\csgo\maps\ar_monastery.bsp";
88
[Space(10)]
99
public bool combineMeshesWithSameTextures = true;
10-
public bool excludeMapFaces = false;
11-
public bool excludeModels = false;
10+
[Range(0, 1)]
11+
public float faceLoadPercent = 1;
12+
[Range(0, 1)]
13+
public float modelLoadPercent = 1;
1214
public bool flatTextures = false;
1315
public int maxTextureSize = 2048;
1416

1517
private BSPMap map;
1618

1719
private void Start()
1820
{
19-
map = LoadMap(vpkPath, mapPath, combineMeshesWithSameTextures, excludeMapFaces, excludeModels, flatTextures, maxTextureSize);
21+
map = LoadMap(vpkPath, mapPath, combineMeshesWithSameTextures, faceLoadPercent, modelLoadPercent, flatTextures, maxTextureSize);
2022
}
2123
private void OnDestroy()
2224
{
@@ -25,14 +27,14 @@ private void OnDestroy()
2527
map = null;
2628
}
2729

28-
public BSPMap LoadMap(string vpkLoc, string mapLoc, bool combineMeshesWithSameTextures = true, bool excludeMapFaces = false, bool excludeModels = false, bool flatTextures = false, int maxTextureSize = 2048)
30+
public BSPMap LoadMap(string vpkLoc, string mapLoc, bool combineMeshesWithSameTextures = true, float faceLoadPercent = 1, float modelLoadPercent = 1, bool flatTextures = false, int maxTextureSize = 2048)
2931
{
3032
BSPMap.vpkLoc = vpkLoc;
3133
BSPMap map = new BSPMap(mapLoc);
3234

3335
BSPMap.combineMeshesWithSameTexture = combineMeshesWithSameTextures;
34-
BSPMap.excludeMapFaces = excludeMapFaces;
35-
BSPMap.excludeModels = excludeModels;
36+
BSPMap.FaceLoadPercent = faceLoadPercent;
37+
BSPMap.ModelLoadPercent = modelLoadPercent;
3638
SourceTexture.averageTextures = flatTextures;
3739
SourceTexture.maxTextureSize = maxTextureSize;
3840

0 commit comments

Comments
 (0)