/// <summary>
        /// Imports the specified world into the SabreCSG model.
        /// </summary>
        /// <param name="model">The model to import into.</param>
        /// <param name="world">The world to be imported.</param>
        /// <param name="scale">The scale modifier.</param>
        public static void Import(CSGModelBase model, MapWorld world)
        {
            try
            {
                model.BeginUpdate();

                // create a material searcher to associate materials automatically.
                MaterialSearcher materialSearcher = new MaterialSearcher();

                // group all the brushes together.
                GroupBrush groupBrush = new GameObject("Quake 1 Map").AddComponent <GroupBrush>();
                groupBrush.transform.SetParent(model.transform);

                // iterate through all entities.
                for (int e = 0; e < world.Entities.Count; e++)
                {
#if UNITY_EDITOR
                    UnityEditor.EditorUtility.DisplayProgressBar("SabreCSG: Importing Quake 1 Map", "Converting Quake 1 Entities To SabreCSG Brushes (" + (e + 1) + " / " + world.Entities.Count + ")...", e / (float)world.Entities.Count);
#endif
                    MapEntity entity = world.Entities[e];

                    // iterate through all entity solids.
                    for (int i = 0; i < entity.Brushes.Count; i++)
                    {
                        MapBrush brush = entity.Brushes[i];
#if UNITY_EDITOR
                        if (world.Entities[e].ClassName == "worldspawn")
                        {
                            UnityEditor.EditorUtility.DisplayProgressBar("SabreCSG: Importing Quake 1 Map", "Converting Quake 1 Brushes To SabreCSG Brushes (" + (i + 1) + " / " + entity.Brushes.Count + ")...", i / (float)entity.Brushes.Count);
                        }
#endif
                        // don't add triggers to the scene.
                        if (brush.Sides.Count > 0 && IsSpecialMaterial(brush.Sides[0].Material))
                        {
                            continue;
                        }

                        // build a very large cube brush.
                        var go = model.CreateBrush(PrimitiveBrushType.Cube, Vector3.zero);
                        var pr = go.GetComponent <PrimitiveBrush>();
                        BrushUtility.Resize(pr, new Vector3(8192, 8192, 8192));

                        // clip all the sides out of the brush.
                        for (int j = brush.Sides.Count; j-- > 0;)
                        {
                            MapBrushSide side = brush.Sides[j];
                            Plane        clip = new Plane(pr.transform.InverseTransformPoint(new Vector3(side.Plane.P1.X, side.Plane.P1.Z, side.Plane.P1.Y) / (float)s_Scale), pr.transform.InverseTransformPoint(new Vector3(side.Plane.P2.X, side.Plane.P2.Z, side.Plane.P2.Y) / (float)s_Scale), pr.transform.InverseTransformPoint(new Vector3(side.Plane.P3.X, side.Plane.P3.Z, side.Plane.P3.Y) / (float)s_Scale));
                            ClipUtility.ApplyClipPlane(pr, clip, false);

                            // find the polygons associated with the clipping plane.
                            // the normal is unique and can never occur twice as that wouldn't allow the solid to be convex.
                            var polygons = pr.GetPolygons().Where(p => p.Plane.normal.EqualsWithEpsilonLower3(clip.normal));
                            foreach (var polygon in polygons)
                            {
                                // detect excluded polygons.
                                if (IsExcludedMaterial(side.Material))
                                {
                                    polygon.UserExcludeFromFinal = true;
                                }
                                // detect collision-only brushes.
                                if (IsInvisibleMaterial(side.Material))
                                {
                                    pr.IsVisible = false;
                                }
                                // find the material in the unity project automatically.
                                Material material;
                                // try finding the texture name with '*' replaced by '#' so '#teleport'.
                                string materialName = side.Material.Replace("*", "#");
                                material = materialSearcher.FindMaterial(new string[] { materialName });
                                if (material == null)
                                {
                                    Debug.Log("SabreCSG: Tried to find material '" + materialName + "' but it couldn't be found in the project.");
                                }
                                polygon.Material = material;
                                // calculate the texture coordinates.
                                int w = 256;
                                int h = 256;
                                if (polygon.Material != null && polygon.Material.mainTexture != null)
                                {
                                    w = polygon.Material.mainTexture.width;
                                    h = polygon.Material.mainTexture.height;
                                }
                                CalculateTextureCoordinates(pr, polygon, w, h, new Vector2(side.Offset.X, -side.Offset.Y), new Vector2(side.Scale.X, side.Scale.Y), side.Rotation);
                            }
                        }

                        // add the brush to the group.
                        pr.transform.SetParent(groupBrush.transform);
                    }
                }

#if UNITY_EDITOR
                UnityEditor.EditorUtility.ClearProgressBar();
#endif
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                model.EndUpdate();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Imports the specified Quake 1 Map Format file.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <returns>A <see cref="MapWorld"/> containing the imported world data.</returns>
        public MapWorld Import(string path)
        {
            // create a new world.
            MapWorld world = new MapWorld();

            // open the file for reading. we use streams for additional performance.
            // it's faster than File.ReadAllLines() as that requires two iterations.
            using (FileStream stream = new FileStream(path, FileMode.Open))
                using (StreamReader reader = new StreamReader(stream))
                {
                    // read all the lines from the file.
                    int       depth = 0;
                    string    line;
                    bool      justEnteredClosure = false;
                    string    key;
                    object    value;
                    MapBrush  brush  = null;
                    MapEntity entity = null;
                    while (!reader.EndOfStream)
                    {
                        line = reader.ReadLine().Trim();
                        if (line.Length == 0)
                        {
                            continue;
                        }

                        // skip comments.
                        if (line[0] == '/')
                        {
                            continue;
                        }

                        // parse closures and keep track of them.
                        if (line[0] == '{')
                        {
                            depth++; justEnteredClosure = true; continue;
                        }
                        if (line[0] == '}')
                        {
                            depth--; continue;
                        }

                        // parse entity.
                        if (depth == 1)
                        {
                            // create a new entity and add it to the world.
                            if (justEnteredClosure)
                            {
                                entity = new MapEntity();
                                world.Entities.Add(entity);
                            }

                            // parse entity properties.
                            if (TryParsekeyValue(line, out key, out value))
                            {
                                switch (key)
                                {
                                case "classname": entity.ClassName = (string)value; break;
                                }
                            }
                        }

                        // parse entity brush.
                        if (depth == 2)
                        {
                            // create a new brush and add it to the entity.
                            if (justEnteredClosure)
                            {
                                brush = new MapBrush();
                                entity.Brushes.Add(brush);
                            }

                            // parse brush sides.
                            MapBrushSide mapBrushSide;
                            if (TryParseBrushSide(line, out mapBrushSide))
                            {
                                brush.Sides.Add(mapBrushSide);
                            }
                        }

                        justEnteredClosure = false;
                    }
                }

            return(world);
        }