From bdc0c8695b72dc326f7d2c05c999720f60204a18 Mon Sep 17 00:00:00 2001 From: wfowler Date: Thu, 21 Mar 2019 00:40:37 -0600 Subject: [PATCH] Initial support for Godot engine. --- LibBSP/Source/Extensions/ColorExtensions.cs | 9 + LibBSP/Source/Extensions/PlaneExtensions.cs | 196 ++++++++++++------ .../Source/Extensions/Vector2dExtensions.cs | 32 ++- .../Source/Extensions/Vector3dExtensions.cs | 42 +++- .../Source/Extensions/Vector4dExtensions.cs | 32 ++- LibBSP/Source/Extensions/VertexExtensions.cs | 15 +- LibBSP/Source/Structs/BSP/BSP.cs | 12 +- LibBSP/Source/Structs/BSP/Cubemap.cs | 7 +- LibBSP/Source/Structs/BSP/DisplacementInfo.cs | 7 +- .../Source/Structs/BSP/DisplacementVertex.cs | 7 +- LibBSP/Source/Structs/BSP/Face.cs | 9 +- LibBSP/Source/Structs/BSP/Patch.cs | 7 +- LibBSP/Source/Structs/BSP/StaticModel.cs | 7 +- LibBSP/Source/Structs/BSP/StaticProp.cs | 10 +- LibBSP/Source/Structs/BSP/Texture.cs | 12 +- LibBSP/Source/Structs/BSP/TextureData.cs | 7 +- LibBSP/Source/Structs/Common/Entity.cs | 20 +- LibBSP/Source/Structs/Common/Plane.cs | 2 +- LibBSP/Source/Structs/Common/Ray.cs | 17 +- LibBSP/Source/Structs/Common/TextureInfo.cs | 24 ++- LibBSP/Source/Structs/Common/Vector2d.cs | 2 +- LibBSP/Source/Structs/Common/Vector3d.cs | 2 +- LibBSP/Source/Structs/Common/Vector4d.cs | 2 +- LibBSP/Source/Structs/Common/Vertex.cs | 5 + LibBSP/Source/Structs/MAP/MAPBrushSide.cs | 16 +- LibBSP/Source/Structs/MAP/MAPDisplacement.cs | 7 +- LibBSP/Source/Structs/MAP/MAPPatch.cs | 4 + LibBSP/Source/Structs/MAP/MAPTerrainEF2.cs | 10 +- LibBSP/Source/Structs/MAP/MAPTerrainMoHAA.cs | 10 +- 29 files changed, 357 insertions(+), 175 deletions(-) diff --git a/LibBSP/Source/Extensions/ColorExtensions.cs b/LibBSP/Source/Extensions/ColorExtensions.cs index 912a287..15a10df 100644 --- a/LibBSP/Source/Extensions/ColorExtensions.cs +++ b/LibBSP/Source/Extensions/ColorExtensions.cs @@ -5,6 +5,8 @@ namespace LibBSP { #if UNITY using Color = UnityEngine.Color32; +#elif GODOT + using Color = Godot.Color; #else using Color = System.Drawing.Color; #endif @@ -25,6 +27,8 @@ namespace LibBSP { public static Color FromArgb(int a, int r, int g, int b) { #if UNITY return new Color((byte)r, (byte)g, (byte)b, (byte)a); +#elif GODOT + return new Color(r << 24 | g << 16 | b << 8 | a); #else return Color.FromArgb(a, r, g, b); #endif @@ -42,6 +46,11 @@ namespace LibBSP { bytes[1] = color.g; bytes[2] = color.b; bytes[3] = color.a; +#elif GODOT + bytes[0] = (byte)color.r8; + bytes[1] = (byte)color.g8; + bytes[2] = (byte)color.b8; + bytes[3] = (byte)color.a8; #else bytes[0] = color.R; bytes[1] = color.G; diff --git a/LibBSP/Source/Extensions/PlaneExtensions.cs b/LibBSP/Source/Extensions/PlaneExtensions.cs index 90627da..cb9148f 100644 --- a/LibBSP/Source/Extensions/PlaneExtensions.cs +++ b/LibBSP/Source/Extensions/PlaneExtensions.cs @@ -4,13 +4,15 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector3d = Vector3; + using Plane = UnityEngine.Plane; + using Vector3d = UnityEngine.Vector3; + using Ray = UnityEngine.Ray; +#elif GODOT + using Plane = Godot.Plane; + using Vector3d = Godot.Vector3; #endif /// @@ -30,6 +32,32 @@ namespace LibBSP { new Vector3d(0, -1, 0), new Vector3d(1, 0, 0), new Vector3d(0, 0, -1) }; + /// + /// Gets the normal of this . + /// + /// This . + /// The normal of this . + public static Vector3d GetNormal(this Plane p) { +#if GODOT + return p.Normal; +#else + return p.normal; +#endif + } + + /// + /// Gets the distance of this from the origin. + /// + /// This . + /// The distance of this from the origin. + public static double GetDistance(this Plane p) { +#if GODOT + return p.D; +#else + return p.distance; +#endif + } + /// /// Intersects three s at a . Returns NaN for all components if two or more s are parallel. /// @@ -38,9 +66,15 @@ namespace LibBSP { /// to intersect. /// Point of intersection if all three s meet at a point, (NaN, NaN, NaN) otherwise. public static Vector3d Intersection(Plane p1, Plane p2, Plane p3) { - Vector3d aN = p1.normal; - Vector3d bN = p2.normal; - Vector3d cN = p3.normal; +#if GODOT + return p1.Intersect3(p2, p3); +#else + Vector3d aN = p1.GetNormal(); + Vector3d bN = p2.GetNormal(); + Vector3d cN = p3.GetNormal(); + var p1d = p1.distance; + var p2d = p2.distance; + var p3d = p3.distance; var partSolx1 = (bN.y * cN.z) - (bN.z * cN.y); var partSoly1 = (bN.z * cN.x) - (bN.x * cN.z); @@ -50,9 +84,10 @@ namespace LibBSP { return new Vector3d(float.NaN, float.NaN, float.NaN); } - return new Vector3d((p1.distance * partSolx1 + p2.distance * (cN.y * aN.z - cN.z * aN.y) + p3.distance * (aN.y * bN.z - aN.z * bN.y)) / det, - (p1.distance * partSoly1 + p2.distance * (aN.x * cN.z - aN.z * cN.x) + p3.distance * (bN.x * aN.z - bN.z * aN.x)) / det, - (p1.distance * partSolz1 + p2.distance * (cN.x * aN.y - cN.y * aN.x) + p3.distance * (aN.x * bN.y - aN.y * bN.x)) / det); + return new Vector3d((p1d * partSolx1 + p2d * (cN.y * aN.z - cN.z * aN.y) + p3d * (aN.y * bN.z - aN.z * bN.y)) / det, + (p1d * partSoly1 + p2d * (aN.x * cN.z - aN.z * cN.x) + p3d * (bN.x * aN.z - bN.z * aN.x)) / det, + (p1d * partSolz1 + p2d * (cN.x * aN.y - cN.y * aN.x) + p3d * (aN.x * bN.y - aN.y * bN.x)) / det); +#endif } /// @@ -63,7 +98,11 @@ namespace LibBSP { /// to intersect. /// Point of intersection if all three s meet at a point, (NaN, NaN, NaN) otherwise. public static Vector3d Intersect(this Plane p1, Plane p2, Plane p3) { +#if GODOT + return p1.Intersect3(p2, p3); +#else return Intersection(p1, p2, p3); +#endif } /// @@ -86,6 +125,28 @@ namespace LibBSP { } } +#if GODOT + /// + /// Raycasts a against this . + /// + /// to raycast against. + /// out parameter that will contain the distance along where the collision happened. + /// + /// true and is positive if intersects this in front of the ray, + /// false and is negative if intersects this behind the ray, + /// false and is 0 if the is parallel to this . + /// + public static bool Raycast(this Plane p, Ray ray, out double enter) { + double denom = ray.direction.Dot(p.Normal); + if (denom > -0.005 && denom < 0.005) { + enter = 0; + return false; + } + enter = (-1 * ray.origin.Dot(p.Normal) + p.D) / denom; + return enter > 0; + } +#endif + /// /// Intersects this with a "" at a . Returns NaN for all components if they do not intersect. /// @@ -113,29 +174,29 @@ namespace LibBSP { /// to intersect. /// Line of intersection where "" intersects "", ((NaN, NaN, NaN) + p(NaN, NaN, NaN)) otherwise. public static Ray Intersection(Plane p1, Plane p2) { - Vector3d direction = Vector3d.Cross(p1.normal, p2.normal); - if (direction == Vector3d.zero) { + Vector3d direction = p1.GetNormal().Cross(p2.GetNormal()); + if (direction == new Vector3d(0, 0, 0)) { return new Ray(new Vector3d(float.NaN, float.NaN, float.NaN), new Vector3d(float.NaN, float.NaN, float.NaN)); } // If x == 0, solve for y in terms of z, or z in terms of y Vector3d origin; - Vector3d sqrDirection = Vector3d.Scale(direction, direction); + Vector3d sqrDirection = new Vector3d(direction.x * direction.x, direction.y * direction.y, direction.z * direction.z); if (sqrDirection.x >= sqrDirection.y && sqrDirection.x >= sqrDirection.z) { - var denom = (p1.normal.y * p2.normal.z) - (p2.normal.y * p1.normal.z); + var denom = (p1.GetNormal().y * p2.GetNormal().z) - (p2.GetNormal().y * p1.GetNormal().z); origin = new Vector3d(0, - ((p1.normal.z * p2.distance) - (p2.normal.z * p1.distance)) / denom, - ((p2.normal.y * p1.distance) - (p1.normal.y * p2.distance)) / denom); + ((p1.GetNormal().z * (float)p2.GetDistance()) - (p2.GetNormal().z * (float)p1.GetDistance())) / denom, + ((p2.GetNormal().y * (float)p1.GetDistance()) - (p1.GetNormal().y * (float)p2.GetDistance())) / denom); } else if (sqrDirection.y >= sqrDirection.x && sqrDirection.y >= sqrDirection.z) { - var denom = (p1.normal.x * p2.normal.z) - (p2.normal.x * p1.normal.z); - origin = new Vector3d(((p1.normal.z * p2.distance) - (p2.normal.z * p1.distance)) / denom, + var denom = (p1.GetNormal().x * p2.GetNormal().z) - (p2.GetNormal().x * p1.GetNormal().z); + origin = new Vector3d(((p1.GetNormal().z * (float)p2.GetDistance()) - (p2.GetNormal().z * (float)p1.GetDistance())) / denom, 0, - ((p2.normal.x * p1.distance) - (p1.normal.x * p2.distance)) / denom); + ((p2.GetNormal().x * (float)p1.GetDistance()) - (p1.GetNormal().x * (float)p2.GetDistance())) / denom); } else { - var denom = (p1.normal.x * p2.normal.y) - (p2.normal.x * p1.normal.y); - origin = new Vector3d(((p1.normal.y * p2.distance) - (p2.normal.y * p1.distance)) / denom, - ((p2.normal.x * p1.distance) - (p1.normal.x * p2.distance)) / denom, + var denom = (p1.GetNormal().x * p2.GetNormal().y) - (p2.GetNormal().x * p1.GetNormal().y); + origin = new Vector3d(((p1.GetNormal().y * (float)p2.GetDistance()) - (p2.GetNormal().y * (float)p1.GetDistance())) / denom, + ((p2.GetNormal().x * (float)p1.GetDistance()) - (p1.GetNormal().x * (float)p2.GetDistance())) / denom, 0); } @@ -161,61 +222,61 @@ namespace LibBSP { public static Vector3d[] GenerateThreePoints(this Plane p, float scalar = 16) { Vector3d[] points = new Vector3d[3]; // Figure out if the plane is parallel to two of the axes. - if (p.normal.y == 0 && p.normal.z == 0) { + if (p.GetNormal().y == 0 && p.GetNormal().z == 0) { // parallel to plane YZ - points[0] = new Vector3d(p.distance / p.normal.x, -scalar, scalar); - points[1] = new Vector3d(p.distance / p.normal.x, 0, 0); - points[2] = new Vector3d(p.distance / p.normal.x, scalar, scalar); - if (p.normal.x > 0) { + points[0] = new Vector3d((float)p.GetDistance() / p.GetNormal().x, -scalar, scalar); + points[1] = new Vector3d((float)p.GetDistance() / p.GetNormal().x, 0, 0); + points[2] = new Vector3d((float)p.GetDistance() / p.GetNormal().x, scalar, scalar); + if (p.GetNormal().x > 0) { Array.Reverse(points); } - } else if (p.normal.x == 0 && p.normal.z == 0) { + } else if (p.GetNormal().x == 0 && p.GetNormal().z == 0) { // parallel to plane XZ - points[0] = new Vector3d(scalar, p.distance / p.normal.y, -scalar); - points[1] = new Vector3d(0, p.distance / p.normal.y, 0); - points[2] = new Vector3d(scalar, p.distance / p.normal.y, scalar); - if (p.normal.y > 0) { + points[0] = new Vector3d(scalar, (float)p.GetDistance() / p.GetNormal().y, -scalar); + points[1] = new Vector3d(0, (float)p.GetDistance() / p.GetNormal().y, 0); + points[2] = new Vector3d(scalar, (float)p.GetDistance() / p.GetNormal().y, scalar); + if (p.GetNormal().y > 0) { Array.Reverse(points); } - } else if (p.normal.x == 0 && p.normal.y == 0) { + } else if (p.GetNormal().x == 0 && p.GetNormal().y == 0) { // parallel to plane XY - points[0] = new Vector3d(-scalar, scalar, p.distance / p.normal.z); - points[1] = new Vector3d(0, 0, p.distance / p.normal.z); - points[2] = new Vector3d(scalar, scalar, p.distance / p.normal.z); - if (p.normal.z > 0) { + points[0] = new Vector3d(-scalar, scalar, (float)p.GetDistance() / p.GetNormal().z); + points[1] = new Vector3d(0, 0, (float)p.GetDistance() / p.GetNormal().z); + points[2] = new Vector3d(scalar, scalar, (float)p.GetDistance() / p.GetNormal().z); + if (p.GetNormal().z > 0) { Array.Reverse(points); } - } else if (p.normal.x == 0) { + } else if (p.GetNormal().x == 0) { // If you reach this point the plane is not parallel to any two-axis plane. // parallel to X axis - points[0] = new Vector3d(-scalar, scalar * scalar, (-(scalar * scalar * p.normal.y - p.distance)) / p.normal.z); - points[1] = new Vector3d(0, 0, p.distance / p.normal.z); - points[2] = new Vector3d(scalar, scalar * scalar, (-(scalar * scalar * p.normal.y - p.distance)) / p.normal.z); - if (p.normal.z > 0) { + points[0] = new Vector3d(-scalar, scalar * scalar, (-(scalar * scalar * p.GetNormal().y - (float)p.GetDistance())) / p.GetNormal().z); + points[1] = new Vector3d(0, 0, (float)p.GetDistance() / p.GetNormal().z); + points[2] = new Vector3d(scalar, scalar * scalar, (-(scalar * scalar * p.GetNormal().y - (float)p.GetDistance())) / p.GetNormal().z); + if (p.GetNormal().z > 0) { Array.Reverse(points); } - } else if (p.normal.y == 0) { + } else if (p.GetNormal().y == 0) { // parallel to Y axis - points[0] = new Vector3d((-(scalar * scalar * p.normal.z - p.distance)) / p.normal.x, -scalar, scalar * scalar); - points[1] = new Vector3d(p.distance / p.normal.x, 0, 0); - points[2] = new Vector3d((-(scalar * scalar * p.normal.z - p.distance)) / p.normal.x, scalar, scalar * scalar); - if (p.normal.x > 0) { + points[0] = new Vector3d((-(scalar * scalar * p.GetNormal().z - (float)p.GetDistance())) / p.GetNormal().x, -scalar, scalar * scalar); + points[1] = new Vector3d((float)p.GetDistance() / p.GetNormal().x, 0, 0); + points[2] = new Vector3d((-(scalar * scalar * p.GetNormal().z - (float)p.GetDistance())) / p.GetNormal().x, scalar, scalar * scalar); + if (p.GetNormal().x > 0) { Array.Reverse(points); } - } else if (p.normal.z == 0) { + } else if (p.GetNormal().z == 0) { // parallel to Z axis - points[0] = new Vector3d(scalar * scalar, (-(scalar * scalar * p.normal.x - p.distance)) / p.normal.y, -scalar); - points[1] = new Vector3d(0, p.distance / p.normal.y, 0); - points[2] = new Vector3d(scalar * scalar, (-(scalar * scalar * p.normal.x - p.distance)) / p.normal.y, scalar); - if (p.normal.y > 0) { + points[0] = new Vector3d(scalar * scalar, (-(scalar * scalar * p.GetNormal().x - (float)p.GetDistance())) / p.GetNormal().y, -scalar); + points[1] = new Vector3d(0, (float)p.GetDistance() / p.GetNormal().y, 0); + points[2] = new Vector3d(scalar * scalar, (-(scalar * scalar * p.GetNormal().x - (float)p.GetDistance())) / p.GetNormal().y, scalar); + if (p.GetNormal().y > 0) { Array.Reverse(points); } } else { // If you reach this point the plane is not parallel to any axis. Therefore, any two coordinates will give a third. - points[0] = new Vector3d(-scalar, scalar * scalar, -(-scalar * p.normal.x + scalar * scalar * p.normal.y - p.distance) / p.normal.z); - points[1] = new Vector3d(0, 0, p.distance / p.normal.z); - points[2] = new Vector3d(scalar, scalar * scalar, -(scalar * p.normal.x + scalar * scalar * p.normal.y - p.distance) / p.normal.z); - if (p.normal.z > 0) { + points[0] = new Vector3d(-scalar, scalar * scalar, -(-scalar * p.GetNormal().x + scalar * scalar * p.GetNormal().y - (float)p.GetDistance()) / p.GetNormal().z); + points[1] = new Vector3d(0, 0, (float)p.GetDistance() / p.GetNormal().z); + points[2] = new Vector3d(scalar, scalar * scalar, -(scalar * p.GetNormal().x + scalar * scalar * p.GetNormal().y - (float)p.GetDistance()) / p.GetNormal().z); + if (p.GetNormal().z > 0) { Array.Reverse(points); } } @@ -231,14 +292,15 @@ namespace LibBSP { /// Unity uses the plane equation "Ax + By + Cz + D = 0" while Quake-based engines /// use "Ax + By + Cz = D". The distance equation needs to be evaluated differently from /// Unity's default implementation to properly apply to planes read from BSPs. -#if UNITY +#if UNITY || GODOT public static float GetBSPDistanceToPoint(this Plane p, Vector3d to) { - return (p.normal.x * to.x + p.normal.y * to.y + p.normal.z * to.z - p.distance) / p.normal.magnitude; + return (p.GetNormal().x * to.x + p.GetNormal().y * to.y + p.GetNormal().z * to.z - (float)p.GetDistance()) / (float)p.GetNormal().GetMagnitude(); + } #else public static double GetBSPDistanceToPoint(this Plane p, Vector3d to) { return p.GetDistanceToPoint(to); -#endif } +#endif /// /// Is on the positive side of this ? @@ -323,8 +385,8 @@ namespace LibBSP { break; } } - p.normal.GetBytes().CopyTo(bytes, 0); - BitConverter.GetBytes(p.distance).CopyTo(bytes, 12); + p.GetNormal().GetBytes().CopyTo(bytes, 0); + BitConverter.GetBytes((float)p.GetDistance()).CopyTo(bytes, 12); return bytes; } @@ -345,7 +407,11 @@ namespace LibBSP { double best = 0; // "Best" dot product so far for (int i = 0; i < 6; ++i) { // For all possible axes, positive and negative - double dot = Vector3d.Dot(p.normal, baseAxes[i * 3]); +#if GODOT + double dot = p.Normal.Dot(baseAxes[i * 3]); +#else + double dot = p.normal.Dot(baseAxes[i * 3]); +#endif if (dot > best) { best = dot; bestaxis = i; @@ -370,17 +436,17 @@ namespace LibBSP { /// This . /// The axial type of this plane. public static int Type(this Plane p) { - double ax = Math.Abs(p.normal.x); + double ax = Math.Abs(p.GetNormal().x); if (ax >= 1.0) { return 0; } - double ay = Math.Abs(p.normal.y); + double ay = Math.Abs(p.GetNormal().y); if (ay >= 1.0) { return 1; } - double az = Math.Abs(p.normal.z); + double az = Math.Abs(p.GetNormal().z); if (az >= 1.0) { return 2; } diff --git a/LibBSP/Source/Extensions/Vector2dExtensions.cs b/LibBSP/Source/Extensions/Vector2dExtensions.cs index b6e3ef6..227177b 100644 --- a/LibBSP/Source/Extensions/Vector2dExtensions.cs +++ b/LibBSP/Source/Extensions/Vector2dExtensions.cs @@ -4,13 +4,12 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -# endif namespace LibBSP { #if UNITY - using Vector2d = Vector2; + using Vector2d = UnityEngine.Vector2; +#elif GODOT + using Vector2d = Godot.Vector2; #endif /// @@ -18,6 +17,31 @@ namespace LibBSP { /// public static class Vector2dExtensions { +#if !GODOT + /// + /// Vector dot product. This operation is commutative. + /// + /// This . + /// The to dot with this . + /// Dot product of this and . + public static double Dot(this Vector2d v1, Vector2d v) { + return Vector2d.Dot(v1, v); + } +#endif + + /// + /// Gets the magnitude of this . + /// + /// This . + /// The magnitude of this . + public static double GetMagnitude(this Vector2d v) { +#if GODOT + return v.Length(); +#else + return v.magnitude; +#endif + } + /// /// Gets a byte array representing the components of this as floats. /// diff --git a/LibBSP/Source/Extensions/Vector3dExtensions.cs b/LibBSP/Source/Extensions/Vector3dExtensions.cs index 98ac3d2..baea8e0 100644 --- a/LibBSP/Source/Extensions/Vector3dExtensions.cs +++ b/LibBSP/Source/Extensions/Vector3dExtensions.cs @@ -4,13 +4,12 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector3d = Vector3; + using Vector3d = UnityEngine.Vector3; +#elif GODOT + using Vector3d = Godot.Vector3; #endif /// @@ -18,6 +17,41 @@ namespace LibBSP { /// public static class Vector3dExtensions { +#if !GODOT + /// + /// Vector dot product. This operation is commutative. + /// + /// This . + /// The to dot with this . + /// Dot product of this and . + public static double Dot(this Vector3d v1, Vector3d v) { + return Vector3d.Dot(v1, v); + } + + /// + /// Vector cross product. This operation is NOT commutative. + /// + /// This . + /// The to have this cross. + /// Cross product of these two vectors. Can be thought of as the normal to the plane defined by these two vectors. + public static Vector3d Cross(this Vector3d v1, Vector3d v) { + return Vector3d.Cross(v1, v); + } +#endif + + /// + /// Gets the magnitude of this . + /// + /// This . + /// The magnitude of this . + public static double GetMagnitude(this Vector3d v) { +#if GODOT + return v.Length(); +#else + return v.magnitude; +#endif + } + /// /// Gets a byte array representing the components of this as floats. /// diff --git a/LibBSP/Source/Extensions/Vector4dExtensions.cs b/LibBSP/Source/Extensions/Vector4dExtensions.cs index a54388f..a4e81fb 100644 --- a/LibBSP/Source/Extensions/Vector4dExtensions.cs +++ b/LibBSP/Source/Extensions/Vector4dExtensions.cs @@ -4,13 +4,12 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector4d = Vector4; + using Vector4d = UnityEngine.Vector4; +#elif GODOT + using Vector4d = Godot.Quat; #endif /// @@ -18,6 +17,31 @@ namespace LibBSP { /// public static class Vector4dExtensions { +#if !GODOT + /// + /// Vector dot product. This operation is commutative. + /// + /// This . + /// The to dot with this . + /// Dot product of this and . + public static double Dot(this Vector4d v1, Vector4d v) { + return Vector4d.Dot(v1, v); + } +#endif + + /// + /// Gets the magnitude of this . + /// + /// This . + /// The magnitude of this . + public static double GetMagnitude(this Vector4d v) { +#if GODOT + return v.Length; +#else + return v.magnitude; +#endif + } + /// /// Gets a byte array representing the components of this as floats. /// diff --git a/LibBSP/Source/Extensions/VertexExtensions.cs b/LibBSP/Source/Extensions/VertexExtensions.cs index 98e788d..6dac1a4 100644 --- a/LibBSP/Source/Extensions/VertexExtensions.cs +++ b/LibBSP/Source/Extensions/VertexExtensions.cs @@ -7,18 +7,19 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector2d = Vector2; - using Vector3d = Vector3; - using Vector4d = Vector4; + using Vector2d = UnityEngine.Vector2; + using Vector3d = UnityEngine.Vector3; + using Vector4d = UnityEngine.Vector4; #if !OLDUNITY - using Vertex = UIVertex; + using Vertex = UnityEngine.UIVertex; #endif +#elif GODOT + using Vector2d = Godot.Vector2; + using Vector3d = Godot.Vector3; + using Vector4d = Godot.Quat; #endif /// diff --git a/LibBSP/Source/Structs/BSP/BSP.cs b/LibBSP/Source/Structs/BSP/BSP.cs index b55aacd..f1fe734 100644 --- a/LibBSP/Source/Structs/BSP/BSP.cs +++ b/LibBSP/Source/Structs/BSP/BSP.cs @@ -9,13 +9,15 @@ using System; using System.IO; using System.Collections.Generic; using System.Reflection; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { -#if UNITY && !OLDUNITY - using Vertex = UIVertex; +#if UNITY + using Plane = UnityEngine.Plane; +#if !OLDUNITY + using Vertex = UnityEngine.UIVertex; +#endif +#elif GODOT + using Plane = Godot.Plane; #endif /// diff --git a/LibBSP/Source/Structs/BSP/Cubemap.cs b/LibBSP/Source/Structs/BSP/Cubemap.cs index 55efcee..d5d5c06 100644 --- a/LibBSP/Source/Structs/BSP/Cubemap.cs +++ b/LibBSP/Source/Structs/BSP/Cubemap.cs @@ -4,13 +4,12 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector3d = Vector3; + using Vector3d = UnityEngine.Vector3; +#elif GODOT + using Vector3d = Godot.Vector3; #endif /// diff --git a/LibBSP/Source/Structs/BSP/DisplacementInfo.cs b/LibBSP/Source/Structs/BSP/DisplacementInfo.cs index f6340a9..e90de47 100644 --- a/LibBSP/Source/Structs/BSP/DisplacementInfo.cs +++ b/LibBSP/Source/Structs/BSP/DisplacementInfo.cs @@ -4,13 +4,12 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector3d = Vector3; + using Vector3d = UnityEngine.Vector3; +#elif GODOT + using Vector3d = Godot.Vector3; #endif /// diff --git a/LibBSP/Source/Structs/BSP/DisplacementVertex.cs b/LibBSP/Source/Structs/BSP/DisplacementVertex.cs index b51d3dd..c86c5de 100644 --- a/LibBSP/Source/Structs/BSP/DisplacementVertex.cs +++ b/LibBSP/Source/Structs/BSP/DisplacementVertex.cs @@ -3,13 +3,12 @@ #endif using System; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector3d = Vector3; + using Vector3d = UnityEngine.Vector3; +#elif GODOT + using Vector3d = Godot.Vector3; #endif /// diff --git a/LibBSP/Source/Structs/BSP/Face.cs b/LibBSP/Source/Structs/BSP/Face.cs index a805f9c..119e249 100644 --- a/LibBSP/Source/Structs/BSP/Face.cs +++ b/LibBSP/Source/Structs/BSP/Face.cs @@ -4,15 +4,14 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector2d = Vector2; + using Vector2d = UnityEngine.Vector2; +#elif GODOT + using Vector2d = Godot.Vector2; #endif - + /// /// Holds all the data for a face in a BSP map. /// diff --git a/LibBSP/Source/Structs/BSP/Patch.cs b/LibBSP/Source/Structs/BSP/Patch.cs index 7184b44..2a09437 100644 --- a/LibBSP/Source/Structs/BSP/Patch.cs +++ b/LibBSP/Source/Structs/BSP/Patch.cs @@ -4,13 +4,12 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector2d = Vector2; + using Vector2d = UnityEngine.Vector2; +#elif GODOT + using Vector2d = Godot.Vector2; #endif /// diff --git a/LibBSP/Source/Structs/BSP/StaticModel.cs b/LibBSP/Source/Structs/BSP/StaticModel.cs index 8707d6c..eaf0bd7 100644 --- a/LibBSP/Source/Structs/BSP/StaticModel.cs +++ b/LibBSP/Source/Structs/BSP/StaticModel.cs @@ -5,13 +5,12 @@ using System; using System.Collections.Generic; using System.Text; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector3d = Vector3; + using Vector3d = UnityEngine.Vector3; +#elif GODOT + using Vector3d = Godot.Vector3; #endif /// diff --git a/LibBSP/Source/Structs/BSP/StaticProp.cs b/LibBSP/Source/Structs/BSP/StaticProp.cs index bda5222..ca3d795 100644 --- a/LibBSP/Source/Structs/BSP/StaticProp.cs +++ b/LibBSP/Source/Structs/BSP/StaticProp.cs @@ -4,14 +4,14 @@ using System; using System.Text; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Color = Color32; - using Vector3d = Vector3; + using Color = UnityEngine.Color32; + using Vector3d = UnityEngine.Vector3; +#elif GODOT + using Color = Godot.Color; + using Vector3d = Godot.Vector3; #else using Color = System.Drawing.Color; #endif diff --git a/LibBSP/Source/Structs/BSP/Texture.cs b/LibBSP/Source/Structs/BSP/Texture.cs index c1f5e12..216700b 100644 --- a/LibBSP/Source/Structs/BSP/Texture.cs +++ b/LibBSP/Source/Structs/BSP/Texture.cs @@ -4,14 +4,14 @@ using System; using System.Text; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector2d = Vector2; - using Vector3d = Vector3; + using Vector2d = UnityEngine.Vector2; + using Vector3d = UnityEngine.Vector3; +#elif GODOT + using Vector2d = Godot.Vector2; + using Vector3d = Godot.Vector3; #endif /// @@ -262,7 +262,7 @@ namespace LibBSP { return new TextureInfo(new Vector3d(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4), BitConverter.ToSingle(data, 8)), new Vector3d(BitConverter.ToSingle(data, 16), BitConverter.ToSingle(data, 20), BitConverter.ToSingle(data, 24)), new Vector2d(BitConverter.ToSingle(data, 12), BitConverter.ToSingle(data, 28)), - Vector2d.one, + new Vector2d(1, 1), -1, -1, 0); } default: { diff --git a/LibBSP/Source/Structs/BSP/TextureData.cs b/LibBSP/Source/Structs/BSP/TextureData.cs index 651b35d..d6976ff 100644 --- a/LibBSP/Source/Structs/BSP/TextureData.cs +++ b/LibBSP/Source/Structs/BSP/TextureData.cs @@ -4,13 +4,12 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector3d = Vector3; + using Vector3d = UnityEngine.Vector3; +#elif GODOT + using Vector3d = Godot.Vector3; #endif /// diff --git a/LibBSP/Source/Structs/Common/Entity.cs b/LibBSP/Source/Structs/Common/Entity.cs index e525cf3..425f9f5 100644 --- a/LibBSP/Source/Structs/Common/Entity.cs +++ b/LibBSP/Source/Structs/Common/Entity.cs @@ -7,14 +7,14 @@ using System.Collections.Generic; using System.Text; using System.Runtime.Serialization; using System.Globalization; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector3d = Vector3; - using Vector4d = Vector4; + using Vector3d = UnityEngine.Vector3; + using Vector4d = UnityEngine.Vector4; +#elif GODOT + using Vector3d = Godot.Vector3; + using Vector4d = Godot.Quat; #endif /// @@ -56,7 +56,10 @@ namespace LibBSP { /// Wrapper for the "origin" attribute. /// public Vector3d origin { - get { return GetVector("origin"); } + get { + Vector4d vec = GetVector("origin"); + return new Vector3d(vec.x, vec.y, vec.z); + } set { this["origin"] = value.x + " " + value.y + " " + value.z; } } @@ -64,7 +67,10 @@ namespace LibBSP { /// Wrapper for the "angles" attribute. /// public Vector3d angles { - get { return GetVector("angles"); } + get { + Vector4d vec = GetVector("angles"); + return new Vector3d(vec.x, vec.y, vec.z); + } set { this["angles"] = value.x + " " + value.y + " " + value.z; } } diff --git a/LibBSP/Source/Structs/Common/Plane.cs b/LibBSP/Source/Structs/Common/Plane.cs index 1c96aef..e242689 100644 --- a/LibBSP/Source/Structs/Common/Plane.cs +++ b/LibBSP/Source/Structs/Common/Plane.cs @@ -1,4 +1,4 @@ -#if !(UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if !(UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER || GODOT) using System; namespace LibBSP { diff --git a/LibBSP/Source/Structs/Common/Ray.cs b/LibBSP/Source/Structs/Common/Ray.cs index 631e8f3..bb64395 100644 --- a/LibBSP/Source/Structs/Common/Ray.cs +++ b/LibBSP/Source/Structs/Common/Ray.cs @@ -2,6 +2,9 @@ using System; namespace LibBSP { +#if GODOT + using Vector3d = Godot.Vector3; +#endif /// /// A struct for a defined by a starting point and a direction vector. @@ -16,7 +19,11 @@ namespace LibBSP { return _direction; } set { +#if GODOT + _direction = value.Normalized(); +#else _direction = value.normalized; +#endif } } @@ -27,7 +34,11 @@ namespace LibBSP { /// Direction vector of this . public Ray(Vector3d origin, Vector3d direction) { this.origin = origin; +#if GODOT + _direction = direction.Normalized(); +#else _direction = direction.normalized; +#endif } /// @@ -36,7 +47,7 @@ namespace LibBSP { /// Distance of the point to get. /// The point at units along this . public Vector3d GetPoint(double distance) { - return origin + (distance * direction); + return origin + ((float)distance * direction); } /// @@ -47,7 +58,7 @@ namespace LibBSP { return string.Format("( {0}, {1} )", origin, direction); } - #region IEquatable +#region IEquatable /// /// Determines whether this is equivalent to another. /// @@ -94,7 +105,7 @@ namespace LibBSP { public override int GetHashCode() { return origin.GetHashCode() ^ direction.GetHashCode(); } - #endregion +#endregion } } diff --git a/LibBSP/Source/Structs/Common/TextureInfo.cs b/LibBSP/Source/Structs/Common/TextureInfo.cs index 5fce0a2..11d2a92 100644 --- a/LibBSP/Source/Structs/Common/TextureInfo.cs +++ b/LibBSP/Source/Structs/Common/TextureInfo.cs @@ -4,14 +4,16 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector2d = Vector2; - using Vector3d = Vector3; + using Vector2d = UnityEngine.Vector2; + using Vector3d = UnityEngine.Vector3; + using Plane = UnityEngine.Plane; +#elif GODOT + using Vector2d = Godot.Vector2; + using Vector3d = Godot.Vector3; + using Plane = Godot.Plane; #endif /// @@ -26,7 +28,7 @@ namespace LibBSP { public int version; // No BSP format uses these so they are fields. - public Vector2d scale = Vector2d.one; + public Vector2d scale = new Vector2d(1, 1); public double rotation = 0; public Vector3d uAxis { @@ -177,13 +179,13 @@ namespace LibBSP { type = MapType.Quake; version = 0; - uAxis = Vector3d.zero; - vAxis = Vector3d.zero; - translation = Vector2d.zero; + uAxis = new Vector3d(0, 0, 0); + vAxis = new Vector3d(0, 0, 0); + translation = new Vector2d(0, 0); flags = 0; texture = -1; - scale = Vector2d.one; + scale = new Vector2d(1, 1); rotation = 0; } @@ -202,7 +204,7 @@ namespace LibBSP { this.type = type; this.version = version; - scale = Vector2d.one; + scale = new Vector2d(1, 1); rotation = 0; } diff --git a/LibBSP/Source/Structs/Common/Vector2d.cs b/LibBSP/Source/Structs/Common/Vector2d.cs index 65975f8..5cc2001 100644 --- a/LibBSP/Source/Structs/Common/Vector2d.cs +++ b/LibBSP/Source/Structs/Common/Vector2d.cs @@ -1,4 +1,4 @@ -#if !(UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if !(UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER || GODOT) using System; using System.Collections; using System.Collections.Generic; diff --git a/LibBSP/Source/Structs/Common/Vector3d.cs b/LibBSP/Source/Structs/Common/Vector3d.cs index ce1d12b..9d9089d 100644 --- a/LibBSP/Source/Structs/Common/Vector3d.cs +++ b/LibBSP/Source/Structs/Common/Vector3d.cs @@ -1,4 +1,4 @@ -#if !(UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if !(UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER || GODOT) using System; using System.Collections; using System.Collections.Generic; diff --git a/LibBSP/Source/Structs/Common/Vector4d.cs b/LibBSP/Source/Structs/Common/Vector4d.cs index 5af020d..47a8455 100644 --- a/LibBSP/Source/Structs/Common/Vector4d.cs +++ b/LibBSP/Source/Structs/Common/Vector4d.cs @@ -1,4 +1,4 @@ -#if !(UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER) +#if !(UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER || GODOT) using System; using System.Collections; using System.Collections.Generic; diff --git a/LibBSP/Source/Structs/Common/Vertex.cs b/LibBSP/Source/Structs/Common/Vertex.cs index 627a2c3..60db14f 100644 --- a/LibBSP/Source/Structs/Common/Vertex.cs +++ b/LibBSP/Source/Structs/Common/Vertex.cs @@ -17,6 +17,11 @@ namespace LibBSP { using Vector2d = UnityEngine.Vector2; using Vector3d = UnityEngine.Vector3; using Vector4d = UnityEngine.Vector4; +#elif GODOT + using Color = Godot.Color; + using Vector2d = Godot.Vector2; + using Vector3d = Godot.Vector3; + using Vector4d = Godot.Quat; #else using Color = System.Drawing.Color; #endif diff --git a/LibBSP/Source/Structs/MAP/MAPBrushSide.cs b/LibBSP/Source/Structs/MAP/MAPBrushSide.cs index e57dd4d..fcb78ab 100644 --- a/LibBSP/Source/Structs/MAP/MAPBrushSide.cs +++ b/LibBSP/Source/Structs/MAP/MAPBrushSide.cs @@ -5,14 +5,16 @@ using System.Collections.Generic; using System; using System.Globalization; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector2d = Vector2; - using Vector3d = Vector3; + using Vector2d = UnityEngine.Vector2; + using Vector3d = UnityEngine.Vector3; + using Plane = UnityEngine.Plane; +#elif GODOT + using Vector2d = Godot.Vector2; + using Vector3d = Godot.Vector3; + using Plane = Godot.Plane; #endif /// @@ -52,8 +54,8 @@ namespace LibBSP { plane = new Plane(new Vector3d(float.Parse(tokens[1], _format), float.Parse(tokens[2], _format), float.Parse(tokens[3], _format)), dist); textureInfo = new TextureInfo(new Vector3d(float.Parse(tokens[8], _format), float.Parse(tokens[9], _format), float.Parse(tokens[10], _format)), new Vector3d(float.Parse(tokens[13], _format), float.Parse(tokens[14], _format), float.Parse(tokens[15], _format)), - Vector2d.zero, - Vector2d.one, + new Vector2d(0, 0), + new Vector2d(1, 1), 0, 0, 0); texture = tokens[18]; } else { diff --git a/LibBSP/Source/Structs/MAP/MAPDisplacement.cs b/LibBSP/Source/Structs/MAP/MAPDisplacement.cs index d982909..a67b644 100644 --- a/LibBSP/Source/Structs/MAP/MAPDisplacement.cs +++ b/LibBSP/Source/Structs/MAP/MAPDisplacement.cs @@ -5,13 +5,12 @@ using System; using System.Collections.Generic; using System.Globalization; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector3d = Vector3; + using Vector3d = UnityEngine.Vector3; +#elif GODOT + using Vector3d = Godot.Vector3; #endif /// diff --git a/LibBSP/Source/Structs/MAP/MAPPatch.cs b/LibBSP/Source/Structs/MAP/MAPPatch.cs index c33616f..812b1c1 100644 --- a/LibBSP/Source/Structs/MAP/MAPPatch.cs +++ b/LibBSP/Source/Structs/MAP/MAPPatch.cs @@ -17,6 +17,10 @@ namespace LibBSP { #if !OLDUNITY using Vertex = UnityEngine.UIVertex; #endif +#elif GODOT + using Vector2d = Godot.Vector2; + using Vector3d = Godot.Vector3; + using Color = Godot.Color; #else using Color = System.Drawing.Color; #endif diff --git a/LibBSP/Source/Structs/MAP/MAPTerrainEF2.cs b/LibBSP/Source/Structs/MAP/MAPTerrainEF2.cs index 01057d2..df7e113 100644 --- a/LibBSP/Source/Structs/MAP/MAPTerrainEF2.cs +++ b/LibBSP/Source/Structs/MAP/MAPTerrainEF2.cs @@ -4,14 +4,14 @@ using System; using System.Globalization; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector4d = Vector4; - using Vector3d = Vector3; + using Vector4d = UnityEngine.Vector4; + using Vector3d = UnityEngine.Vector3; +#elif GODOT + using Vector4d = Godot.Quat; + using Vector3d = Godot.Vector3; #endif /// diff --git a/LibBSP/Source/Structs/MAP/MAPTerrainMoHAA.cs b/LibBSP/Source/Structs/MAP/MAPTerrainMoHAA.cs index 6d9b615..cad514a 100644 --- a/LibBSP/Source/Structs/MAP/MAPTerrainMoHAA.cs +++ b/LibBSP/Source/Structs/MAP/MAPTerrainMoHAA.cs @@ -4,14 +4,14 @@ using System; using System.Collections.Generic; -#if UNITY -using UnityEngine; -#endif namespace LibBSP { #if UNITY - using Vector2d = Vector2; - using Vector3d = Vector3; + using Vector2d = UnityEngine.Vector2; + using Vector3d = UnityEngine.Vector3; +#elif GODOT + using Vector2d = Godot.Vector2; + using Vector3d = Godot.Vector3; #endif ///