コード例 #1
0
ファイル: Materials.cs プロジェクト: yigitnerukuc/SourceUtils
        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);
        }
コード例 #2
0
ファイル: Index.cs プロジェクト: yigitnerukuc/SourceUtils
        public Map GetIndexJson([Url] string map)
        {
            var bsp = Program.GetMap(map);

            var ents      = new List <Entity>();
            var mapParams = new MapParams(bsp);

            foreach (var ent in bsp.Entities)
            {
                var inst = InitEntity(ent, mapParams);
                if (inst != null)
                {
                    ents.Add(inst);
                }
            }

            for (var dispIndex = 0; dispIndex < bsp.DisplacementInfos.Length; ++dispIndex)
            {
                SourceUtils.Vector3 min, max;
                GetDisplacementBounds(bsp, dispIndex, out min, out max, 1f);

                ents.Add(new Displacement
                {
                    ClassName = "displacement",
                    Index     = dispIndex,
                    Clusters  = GetIntersectingClusters(mapParams.Tree, min, max)
                });
            }

            for (var propIndex = 0; propIndex < bsp.StaticProps.PropCount; ++propIndex)
            {
                SourceUtils.Vector3 origin, angles;
                bsp.StaticProps.GetPropTransform(propIndex, out origin, out angles);

                StaticPropFlags flags;
                bool            solid;
                uint            diffuseMod;
                bsp.StaticProps.GetPropInfo(propIndex, out flags, out solid, out diffuseMod);

                int propModelIndex, skin;
                bsp.StaticProps.GetPropModelSkin(propIndex, out propModelIndex, out skin);

                var modelName  = bsp.StaticProps.GetModelName(propModelIndex);
                var modelIndex = StudioModelDictionary.GetResourceIndex(bsp, modelName);

                ents.Add(new StaticProp
                {
                    ClassName = "prop_static",
                    Origin    = origin,
                    Angles    = angles,
                    Flags     = flags,
                    Clusters  = bsp.StaticProps.GetPropLeaves(propIndex)
                                .Select(x => (int)bsp.Leaves[x].Cluster)
                                .Where(x => x != -1)
                                .Distinct(),
                    Model            = modelIndex,
                    VertLighting     = (flags & StaticPropFlags.NoPerVertexLighting) == 0 ? (int?)propIndex : null,
                    LightingOrigin   = (flags & StaticPropFlags.UseLightingOrigin) == 0 ? null : (Vector3?)bsp.StaticProps.GetLightingOrigin(propIndex),
                    AlbedoModulation = diffuseMod != 0xffffffff ? (uint?)diffuseMod : null
                });
            }

            return(new Map
            {
                Name = bsp.Name,
                LightmapUrl = $"/maps/{bsp.Name}/lightmap.json",
                VisPages = GetPageLayout(bsp, bsp.Visibility.NumClusters, VisPage.ClustersPerPage, "/geom/vispage"),
                AmbientPages = GetPageLayout(bsp, bsp.Leaves.Length, AmbientPage.LeavesPerPage, "/geom/ambientpage"),
                LeafPages = GetPageLayout(bsp, bsp.Leaves.Length, LeafGeometryPage.LeavesPerPage, "/geom/leafpage"),
                DispPages = GetPageLayout(bsp, bsp.DisplacementInfos.Length, DispGeometryPage.DisplacementsPerPage, "/geom/disppage"),
                MaterialPages = GetPageLayout(bsp, MaterialDictionary.GetResourceCount(bsp), MaterialPage.MaterialsPerPage, "/materials/matpage"),
                BrushModelPages = GetPageLayout(bsp, bsp.Models.Length, BspModelPage.FacesPerPage, "/geom/bsppage", i => bsp.Models[i].NumFaces),
                StudioModelPages = GetPageLayout(bsp, StudioModelDictionary.GetResourceCount(bsp), StudioModelPage.VerticesPerPage, "/geom/mdlpage", i => StudioModelDictionary.GetVertexCount(bsp, i)),
                VertexLightingPages = GetPageLayout(bsp, bsp.StaticProps.PropCount, VertexLightingPage.PropsPerPage, "/geom/vhvpage"),
                Entities = ents
            });
        }