Esempio n. 1
0
        public IEnumerable <ILump> GetLumps(BspFile bsp)
        {
            var types = new[]
            {
                typeof(Entities),
                typeof(Planes),
                typeof(Textures),
                typeof(Vertices),
                typeof(Visibility),
                typeof(Nodes),
                typeof(Texinfo),
                typeof(Faces),
                typeof(Lightmaps),
                typeof(Clipnodes),
                typeof(Leaves),
                typeof(LeafFaces),
                typeof(Edges),
                typeof(Surfedges),
                typeof(Models),
            };

            return(types
                   .Select(x => bsp.Lumps.FirstOrDefault(l => l.GetType() == x) ?? Activator.CreateInstance(x))
                   .OfType <ILump>());
        }
 protected BspFaceGroupRenderable(BspFile bsp, Environment environment, int mipTexture, IEnumerable <Face> faces)
 {
     Bsp               = bsp;
     Environment       = environment;
     _texture          = bsp.Textures[mipTexture];
     _faces            = faces.ToList();
     _textureResources = new List <ResourceSet>();
 }
Esempio n. 3
0
 public void WriteHeader(BspFile file, IEnumerable <Blob> blobs, BinaryWriter bw)
 {
     bw.Write((int)Version.Goldsource);
     foreach (var blob in blobs)
     {
         bw.Write(blob.Offset);
         bw.Write(blob.Length);
     }
 }
Esempio n. 4
0
        public static OpenGLArrayBuffer[] CreateArrayBuffers(this BspFile bsp)
        {
            var buffers = new OpenGLArrayBuffer[bsp.Models.Count];

            for (int i = 0; i < buffers.Length; i++)
            {
                buffers[i] = CreateArrayBuffer(bsp, i);
            }
            return(buffers);
        }
Esempio n. 5
0
        public BspEntityRenderable(BspFile bsp, Environment env, EntityData entity, Model model)
        {
            _bsp      = bsp;
            _env      = env;
            _entity   = entity;
            _model    = model;
            _children = new List <IRenderable>();

            _colour = GetColour();

            var origin = _model.Origin + entity.GetVector3("origin", Vector3.Zero);

            var entityFaces = new List <Face>();
            var nodes       = new Queue <Node>();

            nodes.Enqueue(bsp.Nodes[_model.HeadNodes[0]]);
            while (nodes.Any())
            {
                var node = nodes.Dequeue();
                foreach (var child in node.Children)
                {
                    if (child >= 0)
                    {
                        nodes.Enqueue(_bsp.Nodes[child]);
                    }
                    else
                    {
                        var leaf = _bsp.Leaves[-1 - child];
                        if (leaf.Contents == Contents.Sky)
                        {
                            continue;
                        }
                        for (var ms = 0; ms < leaf.NumMarkSurfaces; ms++)
                        {
                            var faceidx = _bsp.MarkSurfaces[ms + leaf.FirstMarkSurface];
                            var face    = _bsp.Faces[faceidx];
                            if (face.Styles[0] != byte.MaxValue)
                            {
                                entityFaces.Add(face);
                            }
                        }
                    }
                }
            }

            foreach (var group in entityFaces.GroupBy(x => _bsp.TextureInfos[x.TextureInfo].MipTexture))
            {
                _children.Add(new BspEntityFaceGroupRenderable(_bsp, _env, group.Key, group)
                {
                    Origin = origin,
                    Colour = _colour
                });
            }
        }
Esempio n. 6
0
 private void EnsureLoaded()
 {
     lock (this)
     {
         if (_array != null)
         {
             return;
         }
         _array = new T[Length];
         BspFile.ReadLumpValues(LumpType, 0, _array, 0, Length);
     }
 }
Esempio n. 7
0
        public static OpenGLIndicesBufferWithTextureName[] CreateIndicesBuffer(this BspFile bsp, int modelID)
        {
            var model   = bsp.Models[modelID];
            var buffers = new OpenGLIndicesBufferWithTextureName[model.Triangles.Count];
            int index   = 0;

            foreach (var kvp in model.Triangles)
            {
                var buff = new OpenGLIndicesBufferWithTextureName(kvp.Key, kvp.Value);
                buffers[index++] = buff;
            }
            return(buffers);
        }
Esempio n. 8
0
            private T GetSingle(int index)
            {
                if (_array != null)
                {
                    return(_array[index]);
                }
                if (_sSingleArray == null)
                {
                    _sSingleArray = new T[1];
                }

                BspFile.ReadLumpValues(LumpType, index, _sSingleArray, 0, 1);
                return(_sSingleArray[0]);
            }
Esempio n. 9
0
        private void LoadModels(BspFile bsp, Environment env)
        {
            var loadedModels = new Dictionary <string, MdlFile>(StringComparer.InvariantCultureIgnoreCase);

            foreach (var ent in bsp.Entities.Where(x => EntityModelMap.ContainsKey(x.ClassName)))
            {
                var model = EntityModelMap[ent.ClassName];

                if (model == "")
                {
                    // Use model from the entity
                    if (!ent.KeyValues.ContainsKey("model"))
                    {
                        continue;
                    }
                    model = ent.KeyValues["model"];
                }

                if (ent.KeyValues.ContainsKey("targetname"))
                {
                    var targetters = bsp.Entities.Where(x => x.KeyValues.ContainsKey("target") && x.KeyValues["target"] == ent.KeyValues["targetname"]).ToList();
                }

                var file = env.GetFile(model);
                if (file != null)
                {
                    file = file.Replace('/', '\\'); // normalise path
                    try
                    {
                        MdlFile mdl;
                        if (loadedModels.ContainsKey(file))
                        {
                            mdl = loadedModels[file];
                        }
                        else
                        {
                            mdl = MdlFile.FromFile(file);
                            loadedModels[file] = mdl;
                        }

                        _scene.AddRenderable(new MdlRenderable(mdl, ent.GetVector3("origin", Vector3.Zero)));
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
        }
Esempio n. 10
0
        public static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Usage();
                Console.WriteLine("ERROR: No arguments were given.");
                return;
            }

            var map1 = new BspFile();

            map1.Load(args[0]);
            //LoadMap(args[0]);

            foreach (var texture in map1.GetLump <TexDataLump>().Data)
            {
                texture.TexName = "R997/Mc/Mc-Jackolantern";
            }
            map1.Save("test.bsp");

            var map2 = new BspFile();

            map2.Load("test.bsp");

            //
            // foreach (var ent in map.GetLump<EntityLump>().Data.Where(ent => ent.Properties.ContainsKey("skyname")))
            // {
            //     ent.Properties["skyname"] = "sky_day01_09";
            //     Console.WriteLine("hi");
            // }

            // var lastoffset = 0;
            // foreach (var lump in map.Lumps.OrderBy(lump => lump.Offset))
            // {
            //     Console.WriteLine("Lump {0}, offset {1}, length {2}, last offset {3}, diff {4}", lump.Type, lump.Offset, lump.Length, lastoffset, lump.Offset - lastoffset);
            //     lastoffset = lump.Offset;
            // }

            // var pakfile = new Pakfile(map);
            // ZipArchive archive = pakfile.GetZipArchive();
            // foreach (var entry in archive.Entries)
            // {
            //     Console.WriteLine(entry.Name);
            // }
        }
        public BspFile LoadBsp(string path)
        {
            BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open));

            BspHeader header = new BspHeader(reader);

            BspFile parsed = ScriptableObject.CreateInstance <BspFile>();

            parsed.entities  = ReadEntities(reader, header);
            parsed.verts     = ReadVerts(reader, header);
            parsed.edges     = ReadEdges(reader, header);
            parsed.faces     = ReadFaces(reader, header);
            parsed.faceEdges = ReadFaceEdges(reader, header);
            parsed.texInfo   = ReadTexInfo(reader, header);

            reader.BaseStream.Dispose();

            return(parsed);
        }
Esempio n. 12
0
            public IEnumerable <T> Range(int start, int count)
            {
                if (_array != null)
                {
                    return(_array.Skip(start).Take(count));
                }
                if (start + count > Length)
                {
                    count = Length - start;
                }
                if (count <= 0)
                {
                    return(Enumerable.Empty <T>());
                }

                var array = new T[count];

                BspFile.ReadLumpValues(LumpType, start, array, 0, count);
                return(array);
            }
Esempio n. 13
0
            private Type FindStructType()
            {
                var version = BspFile.GetLumpInfo(LumpType).Version;

                foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
                {
                    if (!typeof(T).IsAssignableFrom(type))
                    {
                        continue;
                    }

                    var versionAttrib = type.GetCustomAttribute <StructVersionAttribute>();

                    if (versionAttrib != null && versionAttrib.MinVersion <= version && versionAttrib.MaxVersion >= version)
                    {
                        return(type);
                    }
                }

                throw new NotSupportedException($"Version {version} of lump {LumpType} is not supported.");
            }
Esempio n. 14
0
        private static BspFile LoadMap(string path)
        {
            try
            {
                var map = new BspFile();

                map.Load(path);

                return(map);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("ERROR: File {0} not found.", path);
            }
            catch (InvalidDataException)
            {
                Console.WriteLine("ERROR: File {0} is not a valid Valve BSP.", path);
            }

            return(null);
        }
Esempio n. 15
0
        public void Open(Environment environment, string path)
        {
            var options = new GraphicsDeviceOptions()
            {
                HasMainSwapchain     = false,
                ResourceBindingModel = ResourceBindingModel.Improved,
                SwapchainDepthFormat = PixelFormat.R32_Float,
            };

            //_graphicsDevice = GraphicsDevice.CreateVulkan(options);
            _graphicsDevice = GraphicsDevice.CreateD3D11(options);

            _view = new VeldridControl(_graphicsDevice, options)
            {
                Dock = DockStyle.Fill
            };
            _panel.Controls.Add(_view);

            _sc = new SceneContext(_graphicsDevice);
            _sc.AddRenderTarget(_view);

            _scene = new Scene();

            BspFile bsp;

            using (var stream = File.OpenRead(path))
            {
                bsp = new BspFile(stream);
            }

            _scene.AddRenderableSource(new BspRenderable(bsp, environment));

            LoadModels(bsp, environment);


            _sc.Scene = _scene;
            _sc.Start();
        }
Esempio n. 16
0
 public void EndHeader(BspFile file, BinaryReader br)
 {
     //
 }
Esempio n. 17
0
 public void PostReadProcess(BspFile bsp)
 {
 }
Esempio n. 18
0
 public BspEntityFaceGroupRenderable(BspFile bsp, Environment environment, int mipTexture, IEnumerable <Face> faces) : base(bsp, environment, mipTexture, faces)
 {
 }
Esempio n. 19
0
 public void StartHeader(BspFile file, BinaryReader br)
 {
     //
 }
Esempio n. 20
0
        public void PostReadProcess(BspFile bsp)
        {
            Texinfo     textureInfos = bsp.GetLump <Texinfo>();
            Planes      planes       = bsp.GetLump <Planes>();
            Surfedges   surfEdges    = bsp.GetLump <Surfedges>();
            Edges       edges        = bsp.GetLump <Edges>();
            Vertices    vertices     = bsp.GetLump <Vertices>();
            List <Face> faces        = bsp.GetLump <Faces>()
                                       .Where(x => x.Styles.Length > 0 && x.Styles[0] != byte.MaxValue)              // Indicates a fullbright face, no offset
                                       .Where(x => x.LightmapOffset >= 0 && x.LightmapOffset < _lightmapData.Length) // Invalid offset
                                       .ToList();

            Dictionary <int, Lightmap> offsetDict = new Dictionary <int, Lightmap>();

            foreach (Face face in faces)
            {
                if (offsetDict.ContainsKey(face.LightmapOffset))
                {
                    continue;
                }

                TextureInfo ti = textureInfos[face.TextureInfo];
                Plane       pl = planes[face.Plane];

                List <Vector2> uvs = new List <Vector2>();
                for (int i = 0; i < face.NumEdges; i++)
                {
                    int     ei    = surfEdges[face.FirstEdge + i];
                    Edge    edge  = edges[Math.Abs(ei)];
                    Vector3 point = vertices[ei > 0 ? edge.Start : edge.End];

                    Vector3 sn = new Vector3(ti.S.X, ti.S.Y, ti.S.Z);
                    float   u  = Vector3.Dot(point, sn) + ti.S.W;

                    Vector3 tn = new Vector3(ti.T.X, ti.T.Y, ti.T.Z);
                    float   v  = Vector3.Dot(point, tn) + ti.T.W;

                    uvs.Add(new Vector2(u, v));
                }

                float minu = uvs.Min(x => x.X);
                float maxu = uvs.Max(x => x.X);
                float minv = uvs.Min(x => x.Y);
                float maxv = uvs.Max(x => x.Y);

                int width  = (int)Math.Ceiling(maxu / 16) - (int)Math.Floor(minu / 16) + 1;
                int height = (int)Math.Ceiling(maxv / 16) - (int)Math.Floor(minv / 16) + 1;
                int bpp    = bsp.Version == Version.Quake1 ? 1 : 3;

                byte[] data = new byte[bpp * width * height];
                Array.Copy(_lightmapData, face.LightmapOffset, data, 0, data.Length);

                Lightmap map = new Lightmap
                {
                    Offset       = face.LightmapOffset,
                    Width        = width,
                    Height       = height,
                    BitsPerPixel = bpp,
                    Data         = data
                };
                _lightmaps.Add(map);
                offsetDict.Add(map.Offset, map);
            }
        }
Esempio n. 21
0
 public void PreWriteProcess(BspFile bsp, Version version)
 {
     // throw new NotImplementedException("Lightmap data must be pre-processed");
 }
Esempio n. 22
0
 public void PostReadProcess(BspFile bsp)
 {
     //throw new NotImplementedException("Visibility data must be post-processed");
 }
Esempio n. 23
0
 public Pakfile(BspFile bspFile)
 {
     ParentFile = bspFile;
     Paklump    = (UnmanagedLump)bspFile.GetLump(LumpType.LUMP_PAKFILE);
 }
Esempio n. 24
0
    public static void CreateMap(BspImportSettings settings)
    {
        //FIXME Save prev scene

        var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);

        BspFile bsp = new BspFile();

        BspFile.LoadAllFromFile(bsp, BspFile.AllLoadFlags, settings.BspPath);

        // Import models
        {
            bsp.PackedGoldSourceTextures.Clear();
            BspFile.LoadPackedTexturesFromFile(bsp, settings.BspPath);
            List <BspLib.Wad.Texture> packedTextures = bsp.PackedGoldSourceTextures;
            Debug.LogFormat("{0} packed textures", packedTextures.Count);

            var go_all_models = new GameObject("Models");
            for (int i = 0; i < bsp.Models.Count; i++)
            {
                var model = bsp.Models[i];
                var go    = new GameObject("" + i);
                go.transform.SetParent(go_all_models.transform);
                if (i == 0)
                {
                    go.isStatic = true;
                }
                go.layer = i == 0 ? settings.Model0Layer : settings.OtherModelLayer;

                // Count triangles
                bool hasSkybox = false;
                int  submeshCountWithoutSky = 0;
                System.Collections.Generic.KeyValuePair <string, uint[]> kvp_sky = default(KeyValuePair <string, uint[]>);
                foreach (var kvp in model.Triangles)
                {
                    if (kvp.Key == "sky")
                    {
                        hasSkybox = true;
                        kvp_sky   = kvp;
                    }
                    else
                    {
                        submeshCountWithoutSky++;
                    }
                }

                // Create Mesh
                var mesh = new Mesh();
                mesh.name = string.Format("Model {0}", i);
                // Submesh Count
                mesh.subMeshCount = submeshCountWithoutSky;
                //mesh_renderer.materials = new Material[model.Triangles.Count];

                // Create vertices and uv
                var vertices = new List <Vector3>();
                var uv       = new List <Vector2>();
                for (int ii = 0; ii < model.Positions.Length; ii++)
                {
                    var p = model.Positions[ii];
                    vertices.Add(new Vector3(p.x * settings.Scale, p.z * settings.Scale, p.y * settings.Scale));

                    var t = model.TextureCoordinates[ii];
                    uv.Add(new Vector2(t.x, -t.y));
                }
                mesh.SetVertices(vertices);
                mesh.SetUVs(0, uv);

                var materials = new Material[submeshCountWithoutSky];

                int submesh = 0;
                foreach (var kvp in model.Triangles)
                {
                    if (kvp.Key == "sky")
                    {
                        continue;
                    }

                    var indices = new int[kvp.Value.Length];
                    for (int ii = 0; ii < indices.Length; ii += 3)
                    {
                        indices[ii + 0] = (int)kvp.Value[ii + 0];
                        indices[ii + 1] = (int)kvp.Value[ii + 1];
                        indices[ii + 2] = (int)kvp.Value[ii + 2];
                    }
                    mesh.SetTriangles(indices, submesh: submesh);

                    // Add material
                    {
                        var material = new Material(Shader.Find("Diffuse"));
                        material.name = kvp.Key;

                        //Texture2D texture = settings.AdditionalTextures == null ? null : settings.AdditionalTextures.FirstOrDefault((t) => t != null && string.Equals(t.name, kvp.Key, System.StringComparison.InvariantCultureIgnoreCase));
                        Texture2D texture = null;
                        foreach (var pt in packedTextures)
                        {
                            if (pt.Name == kvp.Key)
                            {
                                texture       = pt.Bitmap;
                                material.name = string.Format("{0} (PACKED IN BSP)", material.name);
                                break;
                            }
                        }
                        if (texture == null)
                        {
                            var texture_path = Path.Combine(settings.TextureLookupDirectory, kvp.Key + ".png");
                            if (File.Exists(texture_path))
                            {
                                var data = File.ReadAllBytes(texture_path);
                                texture = new Texture2D(1, 1);
                                texture.LoadImage(data);
                            }
                            else
                            {
                                Debug.LogWarning(string.Format("Texture '{0}' could not be found. '{1}'", kvp.Key, Path.GetFullPath(texture_path)));
                                material.name  = string.Format("{0} (MISSING FILE)", material.name);
                                material.color = settings.MissingTextureColor;
                            }
                        }

                        material.mainTexture = texture;

                        materials[submesh] = material;
                    }

                    submesh++;
                }

                mesh.RecalculateBounds();
                mesh.RecalculateNormals();

                // Create Mesh Renderer
                MeshRenderer mesh_renderer = go.AddComponent <MeshRenderer>();
                mesh_renderer.materials = materials;

                // Create Mesh Filter
                MeshFilter mesh_filter = go.AddComponent <MeshFilter>();
                mesh_filter.sharedMesh = mesh;

                // Crete Mesh Collider
                if (settings.Colliders == BspImportSettings.CollidersEnum.Visuals)
                {
                    MeshCollider mesh_collider = go.AddComponent <MeshCollider>();
                    mesh_collider.sharedMesh = mesh;
                }

                if (hasSkybox && settings.Sky)
                {
                    var sky_go = new GameObject("Skybox");
                    sky_go.transform.SetParent(go.transform);

                    var sky_mesh = new Mesh();
                    sky_mesh.name = string.Format("Model {0} Skybox", i);

                    // Create vertices and uv
                    var sky_vertices = new List <Vector3>();
                    for (int ii = 0; ii < model.Positions.Length; ii++)
                    {
                        var p = model.Positions[ii];
                        sky_vertices.Add(new Vector3(p.x * settings.Scale, p.z * settings.Scale, p.y * settings.Scale));
                    }
                    sky_mesh.SetVertices(vertices);


                    var sky_indices = new int[kvp_sky.Value.Length];
                    for (int ii = 0; ii < sky_indices.Length; ii += 3)
                    {
                        sky_indices[ii + 0] = (int)kvp_sky.Value[ii + 0];
                        sky_indices[ii + 1] = (int)kvp_sky.Value[ii + 1];
                        sky_indices[ii + 2] = (int)kvp_sky.Value[ii + 2];
                    }
                    sky_mesh.SetTriangles(sky_indices, submesh: 0);

                    var sky_mesh_filter = sky_go.AddComponent <MeshFilter>();
                    sky_mesh_filter.sharedMesh = sky_mesh;

                    var sky_mesh_renderer = sky_go.AddComponent <MeshRenderer>();
                    {
                        var sky_material = new Material(Shader.Find("Diffuse"));
                        sky_material.name = "Skybox";

                        //TODO load image from file
                        sky_material.color = Color.cyan;

                        /*
                         * var sky_texture = Resources.Load<Texture2D>("textures/sky");
                         * if(sky_texture == null)
                         * {
                         *      sky_texture = new Texture2D(1, 1);
                         *      sky_texture.SetPixel(0, 0, Color.cyan);
                         * }
                         * sky_material.mainTexture = sky_texture;
                         */

                        sky_mesh.RecalculateBounds();
                        sky_mesh.RecalculateNormals();

                        sky_mesh_renderer.sharedMaterial = sky_material;
                    }

                    if (settings.Colliders == BspImportSettings.CollidersEnum.Visuals)
                    {
                        var sky_mesh_collider = sky_go.AddComponent <MeshCollider>();
                        sky_mesh_collider.sharedMesh = sky_mesh;
                    }
                }

                if (settings.Colliders == BspImportSettings.CollidersEnum.ClipNodes)
                {
                    // model.
                    //TODO collision
                }
            }
        }

        // Import Entities
        if (settings.Entities)
        {
            var go_entities = new GameObject("Entities");
            foreach (var e in bsp.Entities)
            {
                string classname;
                if (!e.TryGetValue("classname", out classname))
                {
                    Debug.LogError("Error during BSP import. Entity does not contain 'classname' tag. Probably not valid entity. Skipping.");
                    continue;
                }

                var go = new GameObject(classname);
                go.transform.SetParent(go_entities.transform);

                {
                    var raw = go.AddComponent <RawEntity>();
                    foreach (var kvp in e)
                    {
                        raw.Text += string.Format("\"{0}\" \"{1}\"\n", kvp.Key, kvp.Value);
                    }
                }

                if (e.ContainsKey("origin"))
                {
                    string[] spl = e["origin"].Trim().Split(' ');
                    if (spl.Length == 3)
                    {
                        float x, y, z;
                        if (float.TryParse(spl[0], out x) && float.TryParse(spl[1], out y) && float.TryParse(spl[2], out z))
                        {
                            go.transform.position = new Vector3(x * settings.Scale, z * settings.Scale, y * settings.Scale);
                        }
                    }
                }

                if (e.ContainsKey("model"))
                {
                    var m = e["model"];
                    if (m.StartsWith("*"))
                    {
                        int m_index;
                        if (int.TryParse(m.Substring(1), out m_index))
                        {
                            var m_go = GameObject.Find("Models/" + m_index);
                            if (m_go == null)
                            {
                                Debug.LogWarningFormat("Model {0} for {1} could not be found", m_index, classname);
                            }
                            else
                            {
                                var curr_m_go = Object.Instantiate(m_go);                                // Clone
                                curr_m_go.transform.SetParent(go.transform);

                                m_go.SetActive(false);
                            }
                        }
                        else
                        {
                            Debug.LogWarningFormat("Invalid model number {0} for {1}", m_index, classname);
                        }
                    }
                }

                switch (classname)
                {
                default:
                    Debug.LogFormat("Unknown / unsupported entity '{0}'.", classname);
                    break;

                case "light":
                case "light_environment":
                    if (!settings.LightEntities)
                    {
                        break;
                    }

                    var light = go.AddComponent <Light>();
                    light.type    = classname == "light" ? LightType.Point : LightType.Directional;
                    light.enabled = settings.LightEntitiesEnabled;

                    if (e.ContainsKey("angle"))
                    {
                        int angle;
                        if (int.TryParse(e["angle"], out angle))
                        {
                            light.transform.eulerAngles += new Vector3(angle, 0, 0);
                        }
                    }

                    if (e.ContainsKey("_light"))
                    {
                        string[] _light = e["_light"].Trim().Split(' ');

                        if (_light.Length == 4)
                        {
                            int l_brightness;
                            if (int.TryParse(_light[3], out l_brightness))
                            {
                                light.intensity = 1;
                                light.range     = l_brightness * settings.Scale * 16;                                    // probably in ft
                            }
                        }

                        if (_light.Length == 3 || _light.Length == 4)
                        {
                            byte l_r, l_g, l_b;
                            if (byte.TryParse(_light[0], out l_r) && byte.TryParse(_light[1], out l_g) && byte.TryParse(_light[2], out l_b))
                            {
                                light.color = new Color(l_r / 255f, l_g / 255f, l_b / 255f);
                            }

                            light.cullingMask = settings.LightMask;
                        }
                    }
                    break;

                case "func_buyzone":
                {
                    if (!settings.BuyzoneEntity)
                    {
                        break;
                    }
                    var buyzone = go.AddComponent <BuyZone>();
                    int team;
                    if (int.TryParse(e["team"], out team))
                    {
                        buyzone.Team = team;
                    }

                    foreach (var mr in go.GetComponents <MeshRenderer>())
                    {
                        mr.enabled = false;
                    }
                    foreach (Transform t in go.transform)
                    {
                        foreach (var mr in t.gameObject.GetComponents <MeshRenderer>())
                        {
                            mr.enabled = false;
                        }
                    }

                    foreach (var mc in go.GetComponents <MeshCollider>())
                    {
                        mc.isTrigger = true;
                    }
                    foreach (Transform t in go.transform)
                    {
                        foreach (var mc in t.gameObject.GetComponents <MeshCollider>())
                        {
                            mc.isTrigger = true;
                        }
                    }
                }
                break;

                case "func_bomb_target":
                {
                    if (!settings.BombPlantEntity)
                    {
                        break;
                    }

                    var buyzone = go.AddComponent <BombTarget>();

                    foreach (var mr in go.GetComponents <MeshRenderer>())
                    {
                        mr.enabled = false;
                    }
                    foreach (Transform t in go.transform)
                    {
                        foreach (var mr in t.gameObject.GetComponents <MeshRenderer>())
                        {
                            mr.enabled = false;
                        }
                    }

                    foreach (var mc in go.GetComponents <MeshCollider>())
                    {
                        mc.isTrigger = true;
                    }
                    foreach (Transform t in go.transform)
                    {
                        foreach (var mc in t.gameObject.GetComponents <MeshCollider>())
                        {
                            mc.isTrigger = true;
                        }
                    }
                }
                break;
                }
            }
        }

        // Colliders
        if (bsp.Colliders.Count > 0)
        {
            var go_colliders = new GameObject("Colliders");
            for (int i = 0; i < bsp.Colliders.Count; i++)
            {
                var m_source = bsp.Colliders[i];

                var m = new Mesh();
                m.name = "Collision Mesh " + i;

                // scale + transform vertices
                {
                    var vertices_source = m_source.vertices;
                    var vertices        = new Vector3[vertices_source.Length];
                    for (int vi = 0; vi < vertices_source.Length; vi++)
                    {
                        var v = vertices_source[vi];
                        vertices[vi] = new Vector3(v.x * settings.Scale, v.z * settings.Scale, v.y * settings.Scale);
                    }
                    m.vertices = vertices;
                }
                m.SetIndices(m_source.GetIndices(0), m_source.GetTopology(0), 0);

                var go = new GameObject(i.ToString());
                go.transform.SetParent(go_colliders.transform);

                var collider = go.AddComponent <MeshCollider>();
                collider.sharedMesh = m;
            }
        }
    }
Esempio n. 25
0
 public TexDataStringTableLump(BspFile parent) : base(parent)
 {
 }
Esempio n. 26
0
    // The actual window code goes here
    void OnGUI()
    {
        int width  = (int)position.width;
        int width1 = width - 165;

        _scroll = EditorGUILayout.BeginScrollView(_scroll);
        {
            EditorGUILayout.BeginVertical("Box");
            _foldout_bspfile = EditorGUILayout.Foldout(_foldout_bspfile, "BSP File");
            if (_foldout_bspfile)
            {
                EditorGUILayout.BeginHorizontal();
                {
                    GUILayout.Label("Scene name");
                    ImportSettings.SceneName = GUILayout.TextField(ImportSettings.SceneName, 16, GUILayout.Width(width1));
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                {
                    GUILayout.Label("Bsp file path");
                    ImportSettings.BspPath = GUILayout.TextField(ImportSettings.BspPath, GUILayout.Width(width1 - 20 - 4));
                    if (GUILayout.Button("...", GUILayout.Width(20)))
                    {
                        var path = EditorUtility.OpenFilePanel("Select BSP file", "", "bsp");
                        if (!string.IsNullOrEmpty(path))
                        {
                            ImportSettings.BspPath = path;
                            if (string.IsNullOrEmpty(ImportSettings.SceneName))
                            {
                                ImportSettings.SceneName = System.IO.Path.GetFileNameWithoutExtension(path);
                            }
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();

                {
                    // Refresh loaded info
                    if (_last_bspPath != ImportSettings.BspPath)
                    {
                        _last_bspPath = ImportSettings.BspPath;

                        bool exists = File.Exists(_last_bspPath);

                        _bspVersion_name = exists ? BspLib.Bsp.BspFile.GetVersionName(_last_bspPath) : "File Not Found";

                        if (exists)
                        {
                            try
                            {
                                var bsp = new BspFile();
                                BspFile.LoadPackedTexturesFromFile(bsp, _last_bspPath);

                                {
                                    var packed = bsp.PackedGoldSourceTextures.ToArray();
                                    PackedTextures = new string[packed.Length];

                                    for (int i = 0; i < packed.Length; i++)
                                    {
                                        BspLib.Wad.Texture wt = packed[i];
                                        PackedTextures[i] = wt.Name;
                                    }
                                }

                                RequiredTextures = BspFile.GetUsedTextures(_last_bspPath);
                            }
                            catch
                            {
                                PackedTextures   = new string[0];
                                RequiredTextures = new string[0];
                            }
                        }
                        else
                        {
                            PackedTextures   = new string[0];
                            RequiredTextures = new string[0];
                        }
                    }

                    EditorGUILayout.LabelField("Version", _bspVersion_name);
                }
            }
            EditorGUILayout.EndVertical();

            EditorGUILayout.BeginVertical("Box");
            {
                _foldout_textures = EditorGUILayout.Foldout(_foldout_textures, "Textures");
                if (_foldout_textures)
                {
                    EditorGUILayout.BeginHorizontal();
                    {
                        GUILayout.Label("Texture lookup directory");
                        ImportSettings.TextureLookupDirectory = GUILayout.TextField(ImportSettings.TextureLookupDirectory, GUILayout.Width(width1 - 20 - 4));
                        if (GUILayout.Button("...", GUILayout.Width(20)))
                        {
                            var path = EditorUtility.OpenFolderPanel("Select Texture lookup directory", "", ImportSettings.TextureLookupDirectory);
                            if (!string.IsNullOrEmpty(path))
                            {
                                ImportSettings.TextureLookupDirectory = path;
                            }
                        }
                    }
                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.BeginHorizontal();
                    {
                        GUILayout.Label("Missing Texture Color");

                        ImportSettings.MissingTextureColor = EditorGUILayout.ColorField(ImportSettings.MissingTextureColor);
                    }
                    EditorGUILayout.EndHorizontal();

                    var missing = RequiredTextures.Where((t) => t != null && (PackedTextures == null || !PackedTextures.Any((texture) => texture != t)) && !File.Exists(Path.Combine(ImportSettings.TextureLookupDirectory, t + ".png"))).ToArray();
                    if (missing.Any())
                    {
                        EditorGUILayout.BeginVertical("Box");
                        {
                            _foldout_textures_missing = EditorGUILayout.Foldout(_foldout_textures_missing, "Missing");
                            if (_foldout_textures_missing)
                            {
                                foreach (var m in missing)
                                {
                                    GUILayout.Label(m);
                                }
                            }
                        }
                        EditorGUILayout.EndVertical();
                    }

                    if (PackedTextures.Length > 0)
                    {
                        EditorGUILayout.BeginVertical("Box");
                        {
                            _foldout_textures_packed = EditorGUILayout.Foldout(_foldout_textures_packed, "Packed Textures");
                            if (_foldout_textures_packed)
                            {
                                foreach (string pt in PackedTextures)
                                {
                                    GUILayout.Label(pt);
                                }
                            }
                        }
                        EditorGUILayout.EndVertical();
                    }
                }
            }
            EditorGUILayout.EndVertical();

            EditorGUILayout.BeginVertical("Box");
            {
                _foldout_scale = EditorGUILayout.Foldout(_foldout_scale, "Scale");
                if (_foldout_scale)
                {
                    EditorGUILayout.BeginHorizontal();
                    {
                        float scale = EditorGUILayout.FloatField("Value", ImportSettings.Scale);
                        if (scale != ImportSettings.Scale)
                        {
                            ImportSettings.Scale = scale;

                            if (scale == Scale1hu)
                            {
                                _popup_scale_id = 1;
                            }
                            else if (scale == Scale16hu)
                            {
                                _popup_scale_id = 2;
                            }
                            else if (scale == Scale1ft)
                            {
                                _popup_scale_id = 3;
                            }
                            else if (scale == Scale1m)
                            {
                                _popup_scale_id = 4;
                            }
                            else
                            {
                                _popup_scale_id = 0;
                            }
                        }
                    }
                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.BeginHorizontal();
                    {
                        EditorGUILayout.PrefixLabel("Preset");
                        _popup_scale_id = EditorGUILayout.Popup(_popup_scale_id, new string[] { "<custom>", "1 hammer", "16 hammer", "1 feet", "1 meter" });
                        switch (_popup_scale_id)
                        {
                        default:
                            break;

                        case 1:
                            ImportSettings.Scale = Scale1hu;
                            break;

                        case 2:
                            ImportSettings.Scale = Scale16hu;
                            break;

                        case 3:
                            ImportSettings.Scale = Scale1ft;
                            break;

                        case 4:
                            ImportSettings.Scale = Scale1m;
                            break;
                        }
                    }
                    EditorGUILayout.EndHorizontal();

                    EditorGUILayout.BeginHorizontal();
                    {
                        EditorGUILayout.BeginVertical("Box");
                        {
                            GUILayout.Label("1 in Hammer", EditorStyles.boldLabel);
                            GUILayout.Label(string.Format("{0} in Unity3D", ImportSettings.Scale * 16));
                            GUILayout.Label(string.Format("{0} feet", 1 / 16f));
                            GUILayout.Label(string.Format("{0} meter", 1 / 16f * 0.3048f));
                        }
                        EditorGUILayout.EndVertical();

                        EditorGUILayout.BeginVertical("Box");
                        {
                            GUILayout.Label("1 in Unity3D", EditorStyles.boldLabel);
                            float hu = 16f * ImportSettings.Scale;
                            GUILayout.Label(string.Format("{0} in Hammer", hu));
                            GUILayout.Label(string.Format("{0} feet", hu / 16f));
                            GUILayout.Label(string.Format("{0} meter", (hu / 16f) * 0.3048f));
                        }
                        EditorGUILayout.EndVertical();
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }
            EditorGUILayout.EndVertical();

            EditorGUILayout.BeginVertical("Box");
            {
                _foldout_settings = EditorGUILayout.Foldout(_foldout_settings, "Settings");
                if (_foldout_settings)
                {
                    ImportSettings.Colliders = (BspImportSettings.CollidersEnum)EditorGUILayout.EnumPopup("Generate colliders", ImportSettings.Colliders);


                    ImportSettings.LightMap = EditorGUILayout.Toggle("Import light map", ImportSettings.LightMap);
                    ImportSettings.Sky      = EditorGUILayout.Toggle("Import sky mesh", ImportSettings.Sky);

                    ImportSettings.Model0Layer     = EditorGUILayout.LayerField("Model 0 (solid) mask", ImportSettings.Model0Layer);
                    ImportSettings.OtherModelLayer = EditorGUILayout.LayerField("Other models (entities) mask", ImportSettings.OtherModelLayer);

                    EditorGUILayout.BeginVertical("Box");
                    {
                        ImportSettings.Entities = EditorGUILayout.ToggleLeft("Import Entities", ImportSettings.Entities);

                        if (ImportSettings.Entities)
                        {
                            EditorGUILayout.BeginHorizontal();
                            {
                                if (GUILayout.Button("None", GUILayout.Width(50)))
                                {
                                    ImportSettings.SetAllEntities(false);
                                }
                                if (GUILayout.Button("All", GUILayout.Width(50)))
                                {
                                    ImportSettings.SetAllEntities(true);
                                }
                            }
                            EditorGUILayout.EndHorizontal();

                            EditorGUILayout.Space();

                            ImportSettings.LightEntities = EditorGUILayout.ToggleLeft("Light entities (light, light_environment, light_spot)", ImportSettings.LightEntities);
                            if (ImportSettings.LightEntities)
                            {
                                ImportSettings.LightMask            = EditorGUILayout.LayerField("Light mask", ImportSettings.LightMask);
                                ImportSettings.LightEntitiesEnabled = EditorGUILayout.ToggleLeft("Enable light component", ImportSettings.LightEntitiesEnabled);
                            }

                            ImportSettings.BuyzoneEntity   = EditorGUILayout.ToggleLeft("Buy zone (func_buyzone)", ImportSettings.BuyzoneEntity);
                            ImportSettings.BombPlantEntity = EditorGUILayout.ToggleLeft("Bomb plant (func_bomb_target)", ImportSettings.BombPlantEntity);
                            GUI.enabled = false;
                            ImportSettings.WallEntity        = EditorGUILayout.ToggleLeft("Wall (func_wall)", ImportSettings.WallEntity);
                            ImportSettings.IllusionaryEntity = EditorGUILayout.ToggleLeft("Illusionary wall (func_illusionary)", ImportSettings.IllusionaryEntity);
                            ImportSettings.BreakableEntity   = EditorGUILayout.ToggleLeft("Breakable objects (func_breakable)", ImportSettings.BreakableEntity);
                            ImportSettings.DoorEntity        = EditorGUILayout.ToggleLeft("Doors (func_door, func_door_rotating)", ImportSettings.DoorEntity);
                            ImportSettings.PlayerSpawns      = EditorGUILayout.ToggleLeft("Player spawns (info_player_deathmatch/start, info_vip_start)", ImportSettings.PlayerSpawns);
                            GUI.enabled = true;
                        }
                    }
                    EditorGUILayout.EndVertical();
                }
            }
            EditorGUILayout.EndVertical();

            EditorGUILayout.Space();

            if (ImportSettings.Model0Layer == ImportSettings.OtherModelLayer)
            {
                EditorGUILayout.HelpBox("It is recommended to use different layers for Model 0 and Other Models.\nOtherwise it may create light bugs or something worse.", MessageType.Warning, true);
            }

            if (ImportSettings.LightMap && ImportSettings.LightEntities && ImportSettings.LightEntitiesEnabled)
            {
                EditorGUILayout.HelpBox("Both Light map import and Light Entities are enabled.\nIt may create light bugs or something worse.", MessageType.Warning, true);
            }

            if (string.IsNullOrEmpty(ImportSettings.SceneName) || !Regex.IsMatch(ImportSettings.SceneName, "^[a-zA-Z0-9_\\\\]+$"))
            {
                EditorGUILayout.HelpBox("Invalid scene name.", MessageType.Error, true);
            }
            else if (EditorSceneManager.GetSceneByName(ImportSettings.SceneName).IsValid())
            {
                EditorGUILayout.HelpBox("Name of scene is already used.", MessageType.Error, true);
            }
            else if (!System.IO.File.Exists(ImportSettings.BspPath))
            {
                EditorGUILayout.HelpBox("Map file does not exist.", MessageType.Error, true);
            }
            else if (!Application.isEditor)
            {
                EditorGUILayout.HelpBox("Availible only in editor.", MessageType.Error, true);
            }
            else if (Application.isPlaying)
            {
                EditorGUILayout.HelpBox("Not availible while playing.", MessageType.Error, true);
            }
            else
            {
                if (GUILayout.Button("Import"))
                {
                    CreateMap(ImportSettings);
                }
            }
        }
        EditorGUILayout.EndScrollView();
    }
Esempio n. 27
0
 public TexInfoLump(BspFile parent) : base(parent)
 {
 }
Esempio n. 28
0
 public void PreWriteProcess(BspFile bsp, Version version)
 {
 }
Esempio n. 29
0
 public TexDataLump(BspFile parent) : base(parent)
 {
 }
Esempio n. 30
0
        public static OpenGLArrayBuffer CreateArrayBuffer(this BspFile bsp, int modelID)
        {
            var model = bsp.Models[modelID];

            return(CreateArrayBuffer(model));
        }