Ejemplo n.º 1
0
    BSPModel CreateModel(ref GEOMETRY_T geometry, dmodel_t model)
    {
        var face_id_lists = this.getFaceIdsPerTexture(geometry, model);
        var geometries    = new List <BSPModelGeometry>();
        var faces         = new List <BSPFace>();

        foreach (var i in face_id_lists.sortedKeys)
        {
            var miptex_entry = this.miptex_directory[i];
            var face_ids     = face_id_lists[i];

            foreach (var face_id in face_ids)
            {
                faces.Add(this.faces[face_id]);
            }

            var mesh = this.CreateMesh(geometry, face_ids, miptex_entry);
            geometries.Add(new BSPModelGeometry(i, mesh, faces.ToArray()));

            faces.Clear();
        }

        var result = new BSPModel(geometries.ToArray(), faces.ToArray());

        result.origin   = TransformVector(model.origin);
        result.boundbox = TransformBoundbox(model.bbox);
        return(result);
    }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LightmapViewer"/> class.
        /// </summary>
        /// <param name="meta">The meta.</param>
        /// <remarks></remarks>
        public LightmapViewer(ref Meta meta)
        {
            // Set the initial size of our form
            this.ClientSize = new Size(
                Screen.PrimaryScreen.WorkingArea.Width - 4, Screen.PrimaryScreen.WorkingArea.Height - 4);

            // And its caption
            this.Text = "Model Viewer";

            this.MouseDown += ModelViewer_MouseDown;
            this.MouseMove += this.ModelViewer_MouseDownx;
            this.MouseUp   += this.ModelViewer_MouseUp;

            bsp = new BSPModel(ref meta);

            bsp.LoadLightmaps();

            faceCount = new int[bsp.BSPRawDataMetaChunks.Length];
            for (int x = 0; x < bsp.BSPRawDataMetaChunks.Length; x++)
            {
                faceCount[x] = bsp.BSPRawDataMetaChunks[x].FaceCount;
            }

            bsp.DrawBSPPermutations = false;

            Main();
        }
Ejemplo n.º 3
0
        public GamePhysics(ILogger logger,
                           Scene scene,
                           ITime engineTime, SnapshotTime gameTime,
                           BSPModel worldModel,
                           ICommandContext commandContext)
        {
            _logger     = logger ?? throw new ArgumentNullException(nameof(logger));
            _scene      = scene ?? throw new ArgumentNullException(nameof(scene));
            _engineTime = engineTime ?? throw new ArgumentNullException(nameof(engineTime));
            _gameTime   = gameTime ?? throw new ArgumentNullException(nameof(gameTime));
            _worldModel = worldModel ?? throw new ArgumentNullException(nameof(worldModel));

            //TODO: need to reset this on map spawn for singleplayer
            //TODO: mark as server cvar
            _sv_clienttrace = commandContext.RegisterVariable(
                new VirtualVariableInfo <float>("sv_clienttrace", 1)
                .WithHelpInfo("Scale multiplier for trace lines ran against studio models"));

            InitBoxHull();

            box_hull = new Hull[1]
            {
                new Hull(0, PhysicsConstants.MaxBoxSides, Vector3.Zero, Vector3.Zero, box_clipnodes, new Memory <Models.BSP.FileFormat.Plane>(box_planes))
            };

            CreateAreaNode(0, ref _worldModel.SubModel.Mins, ref _worldModel.SubModel.Maxs);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SpawnLoads"/> class.
 /// </summary>
 /// <param name="map">The map.</param>
 /// <param name="bsp">The BSP.</param>
 /// <param name="device">The device.</param>
 /// <remarks></remarks>
 public SpawnLoads(Map map, BSPModel bsp, Device device)
 {
     this.map         = map;
     this.bsp         = bsp;
     this.device      = device;
     SpawnModel       = new List <ParsedModel>();
     spawnmodelindex  = new int[bsp.Spawns.Spawn.Count];
     BoundingBoxModel = new Mesh[bsp.Spawns.Spawn.Count];
 }
Ejemplo n.º 5
0
    BSPModel[] CreateModels(GEOMETRY_T geometry)
    {
        var models = new BSPModel[geometry.models.Length];

        for (var i = 0; i < geometry.models.Length; ++i)
        {
            models[i] = this.CreateModel(ref geometry, geometry.models[i]);
        }
        return(models);
    }
Ejemplo n.º 6
0
    static void GenerateModel(BSPFile bsp, Level level, BSPModel model, IList <Material> materials, bool[] used)
    {
        if (model.entity != null)
        {
            return;
        }

        GameObject modelObj = new GameObject("Model");

        modelObj.transform.parent = level.transform;
        modelObj.isStatic         = true;

        GenerateGeometries(bsp, model, modelObj, materials, used);
    }
Ejemplo n.º 7
0
    private void LoadModels(BinaryReader bspFile)
    {
        models = new List <BSPModel>();

        BSPDirectoryEntry modelEntry = header.GetDirectoryEntry(DIRECTORY_ENTRY.MODELS);
        long modelCount = modelEntry.size / 64;

        bspFile.BaseStream.Seek(modelEntry.fileOffset, SeekOrigin.Begin);

        for (int i = 0; i < modelCount; i++)
        {
            BSPModel model = new BSPModel(bspFile);
            models.Add(model);
        }
    }
Ejemplo n.º 8
0
    private static void CM_ClipEntity(ref TraceT dst, ref TraceT src, BSPModel model)
    {
        dst.allsolid   = src.allsolid;
        dst.startsolid = src.startsolid;

        if (src.fraction < dst.fraction)
        {
            dst.fraction  = src.fraction;
            dst.endpos    = src.endpos;
            dst.plane     = src.plane;
            dst.surface   = src.surface;
            dst.contents |= src.contents;
            dst.clipmodel = model;
        }
    }
Ejemplo n.º 9
0
    private static void GenerateGeometries(BSPFile bsp, BSPModel model, GameObject parent, IList <Material> materials, bool[] used)
    {
        foreach (var geometry in model.geometries)
        {
            GameObject brush = PrefabCache.InstantiatePrefab("LevelBrush", "Assets/Prefabs");
            brush.transform.parent = parent.transform;
            brush.isStatic         = parent.isStatic;
            GenerateBrush(bsp, brush, geometry, materials);

            foreach (var face in geometry.faces)
            {
                if (!used[face.id])
                {
                    used[face.id] = true;
                    GenerateCollision(bsp, brush.gameObject, face);
                }
            }
        }
    }
Ejemplo n.º 10
0
 public MapInfo(string name, string previousMapName, BSPModel model)
 {
     Name            = name ?? throw new ArgumentNullException(nameof(name));
     PreviousMapName = previousMapName;
     Model           = model ?? throw new ArgumentNullException(nameof(model));
 }
Ejemplo n.º 11
0
 public BSPModelResourceContainer(BSPModel bspModel)
 {
     BSPModel = bspModel ?? throw new ArgumentNullException(nameof(bspModel));
 }
Ejemplo n.º 12
0
    /// <summary>
    /// Creates the model.s
    /// </summary>
    /// <param name="model">BSP model</param>
    /// <param name="entity">Entity data.</param>
    public void CreateModel(BSPModel model, BSPEnt entity)
    {
        MeshFilter     meshFilter;
        MeshRenderer   meshRenderer;
        LightmapAtlas  newAtlas;
        Color          col;
        List <BSPFace> fs;
        float          transparency;
        bool           depthMask;
        bool           hasTexture;
        bool           hasLightmap;

        //set gameobject's name
        gameObject.name = "GM(" + entity.Classname + ")";
        Ent             = entity;
        Bmodel          = model;

        if (model == null || model.numfaces == 0)
        {
            //entity with no bmodel - run spawn and return
            EntitySpawn.Instance.SpawnEntity(this);
            return;
        }

        BSPFace[] faces = model.Faces;

        //group faces by texture and its contents
        var groupsByTexture = faces.GroupBy(i => new {
            i.BSPTexInfo.texfile_guid,
            i.BSPTexInfo.flags,
            c = Utils.GetPointContents(i.Center - (i.Normal * 0.5f)).HasFlag(BrushContents.CONTENTS_WATER) && i.Normal.y == 1
        }).Select(group => group.ToList()).ToList();

        //limit group size to maxgroup
        faceGroups = new List <List <BSPFace> >();
        foreach (List <BSPFace> list in groupsByTexture)
        {
            if (list.Count > maxgroup)
            {
                faceGroups.AddRange(list.Select((x, i) => new { Index = i, Value = x }).GroupBy(x => x.Index / maxgroup).Select(x => x.Select(v => v.Value).ToList()).ToList());
            }
            else
            {
                faceGroups.Add(list);
            }
        }

        //create Bmodel geometry
        for (int a = 0; a < faceGroups.Count; a++)
        {
            fs = faceGroups[a];

            //nodraw brushes can only have a collider mesh
            //TODO: should this only happen to clip brushes?
            if (fs[0].BSPTexInfo.flags.HasFlag(SurfFlags.SURF_NODRAW))
            {
                tris.Clear();
                verts.Clear();

                foreach (BSPFace f in fs)
                {
                    AddFaceToMeshData(f, false, false);
                }

                collisionVerts.AddRange(verts);
                collisionTris.AddRange(tris);

                continue;
            }

            //is this a depth mask?
            depthMask = fs[0].BSPTexInfo.flags.HasFlag(SurfFlags.SURF_SKY);

            //create a lightmap atlas for the group
            if (fs[0].lightmap != null && !depthMask)
            {
                Texture2D[] texs = new Texture2D[fs.Count];

                for (int g = 0; g < texs.Length; g++)
                {
                    if (fs[g].lightmap != null)
                    {
                        texs[g] = fs[g].lightmap.tex;
                    }
                }

                newAtlas = new LightmapAtlas
                {
                    tex = new Texture2D(512, 512)
                };

                //pack the atlas
                newAtlas.rect = newAtlas.tex.PackTextures(texs, 2, 512);

                newAtlas.tex.filterMode = FilterMode.Trilinear;

                for (int b = 0; b < fs.Count; b++)
                {
                    fs[b].atlas       = newAtlas;
                    fs[b].atlas_index = b;
                }

                hasLightmap = true;
            }
            else
            {
                hasLightmap = false;
            }

            GameObject o;

            //if group is made out of sky brushes make it a depth mask
            if (depthMask)
            {
                o            = Instantiate(depthMaskObject, transform);
                hasTexture   = false;
                transparency = -1;

                meshRenderer = o.GetComponent <MeshRenderer>();
            }
            else
            {
                //check object transparency
                if (fs[0].BSPTexInfo.flags.HasFlag(SurfFlags.SURF_TRANS33))
                {
                    transparency = 0.33f;
                }
                else if (fs[0].BSPTexInfo.flags.HasFlag(SurfFlags.SURF_TRANS66))
                {
                    transparency = 0.66f;
                }
                else
                {
                    transparency = -1; //solid
                }

                //instantiate object and set its transparency
                if (transparency > 0)
                {
                    o            = Instantiate(waterObject, transform);
                    meshRenderer = o.GetComponent <MeshRenderer>();

                    col   = Color.white;
                    col.a = transparency;
                    meshRenderer.material.color = col;
                }
                else
                {
                    o            = Instantiate(mapObject, transform);
                    meshRenderer = o.GetComponent <MeshRenderer>();
                }
            }

            //clear lists
            tris.Clear();
            verts.Clear();
            uvs.Clear();
            uvs2.Clear();

            meshFilter = o.GetComponent <MeshFilter>();

            //set object's name
            o.name = entity.Classname + " " + a.ToString();

            //create mesh
            Mesh mesh = new Mesh()
            {
                name = entity.Classname
            };

            //check if a texture has been successfully loaded
            hasTexture = TextureLoader.HasTexture(fs[0].BSPTexInfo.texfile_guid);

            //read all faces into mesh arrays
            foreach (BSPFace f in fs)
            {
                AddFaceToMeshData(f, hasTexture, hasLightmap);
            }

            //add mesh data to collision mesh
            collisionVerts.AddRange(verts);
            collisionTris.AddRange(tris);

            //fill the mesh
            mesh.vertices = verts.ToArray();
            if (hasTexture)
            {
                mesh.uv = uvs.ToArray();
            }
            mesh.triangles = tris.ToArray();
            if (hasLightmap)
            {
                mesh.uv2 = uvs2.ToArray();
            }

            //calculate normals and bounds
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();

            meshFilter.mesh = mesh;

            //set textures
            if (hasTexture)
            {
                meshRenderer.material.mainTexture = TextureLoader.GetTexture(fs[0].BSPTexInfo.texfile_guid);
            }

            //set lightmap textures
            if (hasLightmap)
            {
                meshRenderer.material.SetTexture("_LightmapTex", fs[0].atlas.tex);
            }

            //add object to list
            faceObjects.Add(o);
        }

        //spawn bmodel entity at the very end!
        EntitySpawn.Instance.SpawnEntity(this);

        //generate collision mesh
        MeshCollider meshCollider = GetComponent <MeshCollider>();

        if (meshCollider)
        {
            Mesh collisionMesh = new Mesh
            {
                vertices  = collisionVerts.ToArray(),
                triangles = collisionTris.ToArray()
            };

            collisionMesh.RecalculateNormals();
            collisionMesh.RecalculateBounds();
            collisionMesh.name = entity.Classname;

            meshCollider.sharedMesh = collisionMesh;
        }
    }
Ejemplo n.º 13
0
 public BSPModelResourceContainer(Scene scene, BSPModel bspModel)
     : base(scene)
 {
     BSPModel = bspModel ?? throw new ArgumentNullException(nameof(bspModel));
 }