private MeshData GetOrCreateMeshData(ValveBspFile bsp, GeometryPage page, string matPath, bool cacheVertices = true) { MaterialGroup matGroup; var matDictIndex = MaterialDictionary.GetResourceIndex(bsp, matPath); if (!page.MaterialIndices.TryGetValue(matDictIndex, out int matIndex)) { var vmt = ValveMaterialFile.FromProvider(MaterialDictionary.GetResourcePath(bsp, matDictIndex), bsp.PakFile, Program.Resources); matGroup = new MaterialGroup(matIndex = page.Materials.Count, cacheVertices) { Material = matDictIndex }; page.MaterialIndices.Add(matDictIndex, matIndex); page.Materials.Add(matGroup); FindMaterialAttributes(vmt, matGroup.MeshData.Attributes); } else { matGroup = page.Materials[matIndex]; } return(matGroup.MeshData); }
public MaterialPage GetPage([Url] string map, [Url] int index) { var bsp = Program.GetMap(map); var first = index * MaterialPage.MaterialsPerPage; var count = Math.Min(first + MaterialPage.MaterialsPerPage, MaterialDictionary.GetResourceCount(bsp)) - first; if (count < 0) { first = MaterialDictionary.GetResourceCount(bsp); count = 0; } var texDict = new Dictionary <string, int>(); var page = new MaterialPage(); for (var i = 0; i < count; ++i) { var path = MaterialDictionary.GetResourcePath(bsp, first + i); var mat = Material.Get(bsp, path); page.Materials.Add(mat); if (mat == null) { continue; } foreach (var prop in mat.Properties) { if (prop.Type != MaterialPropertyType.TextureUrl) { continue; } prop.Type = MaterialPropertyType.TextureIndex; var texUrl = (Url)prop.Value; int texIndex; if (texDict.TryGetValue(texUrl, out texIndex)) { prop.Value = texIndex; continue; } prop.Value = texIndex = page.Textures.Count; var texPath = TextureController.GetTexturePath(texUrl); var tex = Texture.Get(bsp, texPath); texDict.Add(texUrl, texIndex); page.Textures.Add(tex); } } return(page); }