Ejemplo 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);
            }
            if (line.Count(c => c == '"') != 4)
            {
                return(false);                                // fixes 'ep1_c17_03.vmf' "rulescript" with newline in the value.
            }
            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);
            }

            string rawvalue_string = rawvalue;

            rawvalue = rawvalue.Replace("--", "-");                                         // fixes 'd2_coast_05.vmf' with "-8 --64 -64" double negative.
            rawvalue = System.Text.RegularExpressions.Regex.Replace(rawvalue, @"\s+", " "); // fixes 'd2_prison_07.vmf' having two spaces in a vector declaration.

            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] == '[' && rawvalue[rawvalue.Length - 1] != ']')
            {
                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 vector3 definition.
            else if (rawvalue.Count(c => c == ' ') == 2 && rawvalue.All(c => " -.0123456789".Contains(c)))
            {
                string[] values = rawvalue.Split(' ');
                value = new VmfVector3(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], CultureInfo.InvariantCulture));
                return(true);
            }
            // detect vector4 definition.
            else if (rawvalue.Count(c => c == ' ') == 3 && rawvalue.All(c => " -.0123456789".Contains(c)))
            {
                string[] values = rawvalue.Split(' ');
                value = new VmfVector4(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], CultureInfo.InvariantCulture), float.Parse(values[3], CultureInfo.InvariantCulture));
                return(true);
            }
            // detect alternate vector3 definition.
            else if (rawvalue.Count(c => c == ' ') == 2 && rawvalue.All(c => " e-.0123456789[]".Contains(c))) // "e" exponent occurs here fixing 'ep2_outland_07_barn.vmf'.
            {
                string[] values = rawvalue.Replace("[", "").Replace("]", "").Split(' ');
                value = new VmfVector3(float.Parse(values[0], CultureInfo.InvariantCulture), float.Parse(values[1], CultureInfo.InvariantCulture), float.Parse(values[2], 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_string;
                return(true);
            }
        }
        private static void CalculateTextureCoordinates(ChiselBrush pr, ChiselSurface surface, Plane clip, int textureWidth, int textureHeight, VmfAxis UAxis, VmfAxis VAxis)
        {
            var localToPlaneSpace = (Matrix4x4)MathExtensions.GenerateLocalToPlaneSpaceMatrix(new float4(clip.normal, clip.distance));
            var planeSpaceToLocal = (Matrix4x4)math.inverse(localToPlaneSpace);

            UAxis.Translation %= textureWidth;
            VAxis.Translation %= textureHeight;

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

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

            var scaleX = textureWidth * UAxis.Scale * inchesInMeters;
            var scaleY = textureHeight * VAxis.Scale * inchesInMeters;

            var uoffset = Vector3.Dot(Vector3.zero, new Vector3(UAxis.Vector.X, UAxis.Vector.Z, UAxis.Vector.Y)) + (UAxis.Translation / textureWidth);
            var voffset = Vector3.Dot(Vector3.zero, new Vector3(VAxis.Vector.X, VAxis.Vector.Z, VAxis.Vector.Y)) + (VAxis.Translation / textureHeight);

            var uVector  = new Vector4(UAxis.Vector.X / scaleX, UAxis.Vector.Z / scaleX, UAxis.Vector.Y / scaleX, uoffset);
            var vVector  = new Vector4(VAxis.Vector.X / scaleY, VAxis.Vector.Z / scaleY, VAxis.Vector.Y / scaleY, voffset);
            var uvMatrix = new UVMatrix(uVector, -vVector);
            var matrix   = uvMatrix.ToMatrix();

            matrix = matrix * planeSpaceToLocal;

            surface.surfaceDescription.UV0 = new UVMatrix(matrix);
        }