示例#1
0
        // For Quake 1 Standard format
        private static bool GetTextureAxises(Plane plane, out MapVector3 t1, out MapVector3 t2)
        {
            // feel free to improve this uv mapping code, it has some issues.
            // • 45 degree angled walls may not have correct UV texture coordinates (are not correctly picking the dominant axis because there are two).
            // • negative vertex coordinates may not have correct UV texture coordinates.

            int dominantAxis = 0; // 0 == x, 1 == y, 2 == z

            // find the axis closest to the polygon's normal.
            float[] axes =
            {
                Mathf.Abs(plane.normal.x),
                Mathf.Abs(plane.normal.z),
                Mathf.Abs(plane.normal.y)
            };

            // defaults to use x-axis.
            dominantAxis = 0;
            // check whether the y-axis is more likely.
            if (axes[1] > axes[dominantAxis])
            {
                dominantAxis = 1;
            }
            // check whether the z-axis is more likely.
            if (axes[2] >= axes[dominantAxis])
            {
                dominantAxis = 2;
            }

            // x-axis:
            if (dominantAxis == 0)
            {
                t1 = new MapVector3(0, 0, 1);
                t2 = new MapVector3(0, 1, 0);
                return(true);
            }

            // y-axis:
            if (dominantAxis == 1)
            {
                t1 = new MapVector3(0, 0, 1);
                t2 = new MapVector3(1, 0, 0);
                return(true);
            }

            // z-axis:
            if (dominantAxis == 2)
            {
                t1 = new MapVector3(1, 0, 0);
                t2 = new MapVector3(0, 1, 0);
                return(true);
            }

            t1 = null;
            t2 = null;
            return(false);
        }
        /// <summary>
        /// Tries the parse a brush side line.
        /// </summary>
        /// <param name="line">The line to be parsed.</param>
        /// <param name="mapBrushSide">The map brush side or null.</param>
        /// <returns>True if successful else false.</returns>
        private bool TryParseBrushSide(string line, out MapBrushSide mapBrushSide, bool valveFormat)
        {
            if (valveFormat)
            {
                return(TryParseBrushSideValve(line, out mapBrushSide));
            }

            mapBrushSide = new MapBrushSide();

            // detect brush side definition.
            if (line[0] == '(')
            {
                string[] values = line.Replace("(", "").Replace(")", "").Replace("  ", " ").Replace("  ", " ").Trim().Split(' ');
                if (values.Length != 15)
                {
                    return(false);
                }

                try
                {
                    MapVector3 p1 = new MapVector3(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], CultureInfo.InvariantCulture));
                    MapVector3 p2 = new MapVector3(float.Parse(values[3], CultureInfo.InvariantCulture), float.Parse(values[4], CultureInfo.InvariantCulture), float.Parse(values[5], CultureInfo.InvariantCulture));
                    MapVector3 p3 = new MapVector3(float.Parse(values[6], CultureInfo.InvariantCulture), float.Parse(values[7], CultureInfo.InvariantCulture), float.Parse(values[8], CultureInfo.InvariantCulture));

                    var tex1Vec = new UnityEngine.Vector3(p1.X, p1.Y, p1.Z).normalized;
                    var tex2Vec = new UnityEngine.Vector3(p3.X, p3.Y, p3.Z).normalized;

                    mapBrushSide.t1 = new MapVector3(tex1Vec.x, tex1Vec.y, tex1Vec.z);
                    mapBrushSide.t2 = new MapVector3(tex2Vec.x, tex2Vec.y, tex2Vec.z);

                    mapBrushSide.Plane    = new MapPlane(p1, p2, p3);
                    mapBrushSide.Material = values[9];
                    mapBrushSide.Offset   = new MapVector2(float.Parse(values[10], CultureInfo.InvariantCulture), float.Parse(values[11], CultureInfo.InvariantCulture));
                    mapBrushSide.Rotation = float.Parse(values[12], CultureInfo.InvariantCulture);
                    mapBrushSide.Scale    = new MapVector2(float.Parse(values[13], CultureInfo.InvariantCulture), float.Parse(values[14], CultureInfo.InvariantCulture));
                }
                catch (Exception)
                {
                    throw new Exception("Encountered invalid brush side. The format of the map file must be slightly different, please open an issue on github if you think you did everything right.");
                }

                return(true);
            }

            return(false);
        }
        private bool TryParseBrushSideValve(string line, out MapBrushSide mapBrushSide)
        {
            mapBrushSide = new MapBrushSide();

            // detect brush side definition.
            if (line[0] == '(')
            {
                string[] values = line.Replace("(", "").Replace(")", "").Replace("[", "").Replace("]", "").Replace("  ", " ").Replace("  ", " ").Trim().Split(' ');

                //UnityEngine.Debug.Log($"Values Length = {values.Length}");

                if (values.Length != 21)
                {
                    return(false);
                }

                try
                {
                    MapVector3 p1 = new MapVector3(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], CultureInfo.InvariantCulture));
                    MapVector3 p2 = new MapVector3(float.Parse(values[3], CultureInfo.InvariantCulture), float.Parse(values[4], CultureInfo.InvariantCulture), float.Parse(values[5], CultureInfo.InvariantCulture));
                    MapVector3 p3 = new MapVector3(float.Parse(values[6], CultureInfo.InvariantCulture), float.Parse(values[7], CultureInfo.InvariantCulture), float.Parse(values[8], CultureInfo.InvariantCulture));
                    mapBrushSide.Plane    = new MapPlane(p1, p2, p3);
                    mapBrushSide.Material = values[9];

                    mapBrushSide.t1 = new MapVector3(float.Parse(values[10], CultureInfo.InvariantCulture), float.Parse(values[11], CultureInfo.InvariantCulture), float.Parse(values[12], CultureInfo.InvariantCulture));
                    mapBrushSide.t2 = new MapVector3(float.Parse(values[14], CultureInfo.InvariantCulture), float.Parse(values[15], CultureInfo.InvariantCulture), float.Parse(values[16], CultureInfo.InvariantCulture));

                    mapBrushSide.Offset   = new MapVector2(float.Parse(values[13], CultureInfo.InvariantCulture), float.Parse(values[17], CultureInfo.InvariantCulture));
                    mapBrushSide.Rotation = float.Parse(values[18], CultureInfo.InvariantCulture);
                    mapBrushSide.Scale    = new MapVector2(float.Parse(values[19], CultureInfo.InvariantCulture), float.Parse(values[20], CultureInfo.InvariantCulture));
                }
                catch (Exception)
                {
                    throw new Exception("Encountered invalid brush side. The format of the map file must be slightly different, please open an issue on github if you think you did everything right.");
                }

                return(true);
            }

            return(false);
        }