Esempio n. 1
0
        /// <summary>
        /// Tries to parse a key value line.
        /// </summary>
        /// <param name="line">The line (e.g. '"editorversion" "400"').</param>
        /// <param name="key">The key that was found.</param>
        /// <param name="value">The value that was found.</param>
        /// <returns>True if successful else false.</returns>
        private bool TryParsekeyValue(string line, out string key, out object value)
        {
            key   = "";
            value = null;

            if (!line.Contains('"'))
            {
                return(false);
            }
            int idx = line.IndexOf('"', 1);

            key = line.Substring(1, idx - 1);
            string rawvalue = line.Substring(idx + 3, line.Length - idx - 4);

            if (rawvalue.Length == 0)
            {
                return(false);
            }

            int   vi;
            float vf;

            // detect plane definition.
            if (rawvalue[0] == '(')
            {
                string[]   values = rawvalue.Replace("(", "").Replace(")", "").Split(' ');
                VmfVector3 p1     = new VmfVector3(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], CultureInfo.InvariantCulture));
                VmfVector3 p2     = new VmfVector3(float.Parse(values[3], CultureInfo.InvariantCulture), float.Parse(values[4], CultureInfo.InvariantCulture), float.Parse(values[5], CultureInfo.InvariantCulture));
                VmfVector3 p3     = new VmfVector3(float.Parse(values[6], CultureInfo.InvariantCulture), float.Parse(values[7], CultureInfo.InvariantCulture), float.Parse(values[8], CultureInfo.InvariantCulture));
                value = new VmfPlane(p1, p2, p3);
                return(true);
            }
            // detect uv definition.
            else if (rawvalue[0] == '[')
            {
                string[] values = rawvalue.Replace("[", "").Replace("]", "").Split(' ');
                value = new VmfAxis(new VmfVector3(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], CultureInfo.InvariantCulture)), float.Parse(values[3], CultureInfo.InvariantCulture), float.Parse(values[4], CultureInfo.InvariantCulture));
                return(true);
            }
            // detect floating point value.
            else if (rawvalue.Contains('.') && float.TryParse(rawvalue, out vf))
            {
                value = vf;
                return(true);
            }
            // detect integer value.
            else if (Int32.TryParse(rawvalue, out vi))
            {
                value = vi;
                return(true);
            }
            // probably a string value.
            else
            {
                value = rawvalue;
                return(true);
            }
        }
        // shoutouts to Aleksi Juvani for your vmf importer giving me a clue on why my textures were misaligned.
        // had to add the world space position of the brush to the calculations! https://github.com/aleksijuvani
        private static void CalculateTextureCoordinates(PrimitiveBrush pr, Polygon polygon, int textureWidth, int textureHeight, VmfAxis UAxis, VmfAxis VAxis)
        {
            UAxis.Translation = UAxis.Translation % textureWidth;
            VAxis.Translation = VAxis.Translation % textureHeight;

            if (UAxis.Translation < -textureWidth / 2f)
            {
                UAxis.Translation += textureWidth;
            }

            if (VAxis.Translation < -textureHeight / 2f)
            {
                VAxis.Translation += textureHeight;
            }

            // calculate texture coordinates.
            for (int i = 0; i < polygon.Vertices.Length; i++)
            {
                var vertex = pr.transform.position + polygon.Vertices[i].Position;

                Vector3 uaxis = new Vector3(UAxis.Vector.X, UAxis.Vector.Z, UAxis.Vector.Y);
                Vector3 vaxis = new Vector3(VAxis.Vector.X, VAxis.Vector.Z, VAxis.Vector.Y);

                var u = Vector3.Dot(vertex, uaxis) / (textureWidth * (UAxis.Scale * inchesInMeters)) + UAxis.Translation / textureWidth;
                var v = Vector3.Dot(vertex, vaxis) / (textureHeight * (VAxis.Scale * inchesInMeters)) + VAxis.Translation / textureHeight;

                polygon.Vertices[i].UV.x = u;
                polygon.Vertices[i].UV.y = -v;
            }
        }