< Summary

Class:GDX.Experimental.DebugDrawShapes
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Experimental/DebugDrawShapes.cs
Covered lines:0
Uncovered lines:197
Coverable lines:197
Total lines:343
Line coverage:0% (0 of 197)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:12
Method coverage:0% (0 of 12)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DebugDrawShapes()0%2100%
GetCircleVertices(...)0%6200%
GetCubeSegmentOffset(...)0%2100%
GetCubeSegmentOffsetNonAlloc(...)0%2100%
GetCubeVertices(...)0%2100%
GetCubeVerticesNonAlloc(...)0%2100%
DrawDottedCube(...)0%2100%
DrawWireCapsule(...)0%56700%
DrawWireArc(...)0%12300%
DrawWireCircle(...)0%12300%
DrawWireCube(...)0%2100%
DrawWireSphere(...)0%12300%

File(s)

./Packages/com.dotbunny.gdx/GDX/Experimental/DebugDrawShapes.cs

#LineLine coverage
 1// Copyright (c) 2020-2023 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using UnityEngine;
 6using Unity.Mathematics;
 7
 8namespace GDX.Experimental
 9{
 10    public static class DebugDrawShapes
 11    {
 12        const float PI = 3.1415927f;
 13        const float Deg2Rad = 0.017453292f;
 14        const int DefaultCircleVertexCount = 32;
 15
 16        /// <summary>
 17        ///     The ordered segment index pairs used to describe a cube.
 18        /// </summary>
 19        /// <remarks>
 20        ///     This effectively wraps the left side, then the right, then connects the two sides.
 21        /// </remarks>
 022        public static int[] CubeSegmentIndices =
 23        {
 24            0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7
 25        };
 26
 27
 28        static void GetCircleVertices(ref Vector3[] vertices, int startIndex, Vector3 center, Quaternion rotation, float
 029        {
 030            float radiansInterval = PI * 2f / circleVertexCount;
 31
 32            // Loop through and figure out the points
 033            for (int i = 0; i < circleVertexCount; i++)
 034            {
 035                float angle = i * radiansInterval;
 36
 37                // Create base point
 038                vertices[i+startIndex] = (rotation * new Vector3(0, math.sin(angle) * radius, math.cos(angle) * radius))
 039            }
 040        }
 41
 42        static int[] GetCubeSegmentOffset(int offset)
 043        {
 044            int[] newIndices = new int[24]
 45            {
 46                CubeSegmentIndices[0] + offset, CubeSegmentIndices[1] + offset, CubeSegmentIndices[2] + offset,
 47                CubeSegmentIndices[3] + offset, CubeSegmentIndices[4] + offset, CubeSegmentIndices[5] + offset,
 48                CubeSegmentIndices[6] + offset, CubeSegmentIndices[7] + offset, CubeSegmentIndices[8] + offset,
 49                CubeSegmentIndices[9] + offset, CubeSegmentIndices[10] + offset, CubeSegmentIndices[11] + offset,
 50                CubeSegmentIndices[12] + offset, CubeSegmentIndices[13] + offset, CubeSegmentIndices[14] + offset,
 51                CubeSegmentIndices[15] + offset, CubeSegmentIndices[16] + offset, CubeSegmentIndices[17] + offset,
 52                CubeSegmentIndices[18] + offset, CubeSegmentIndices[19] + offset, CubeSegmentIndices[20] + offset,
 53                CubeSegmentIndices[21] + offset, CubeSegmentIndices[22] + offset, CubeSegmentIndices[23] + offset
 54            };
 055            return newIndices;
 056        }
 57
 58        static void GetCubeSegmentOffsetNonAlloc(int[] indices, int startIndex, int offset)
 059        {
 060            indices[startIndex] = CubeSegmentIndices[0] + offset;
 061            indices[startIndex+1] = CubeSegmentIndices[1] + offset;
 062            indices[startIndex+2] = CubeSegmentIndices[2] + offset;
 063            indices[startIndex+3] = CubeSegmentIndices[3] + offset;
 064            indices[startIndex+4] = CubeSegmentIndices[4] + offset;
 065            indices[startIndex+5] = CubeSegmentIndices[5] + offset;
 066            indices[startIndex+6] = CubeSegmentIndices[6] + offset;
 067            indices[startIndex+7] = CubeSegmentIndices[7] + offset;
 068            indices[startIndex+8] = CubeSegmentIndices[8] + offset;
 069            indices[startIndex+9] = CubeSegmentIndices[9] + offset;
 070            indices[startIndex+10] = CubeSegmentIndices[10] + offset;
 071            indices[startIndex+11] = CubeSegmentIndices[11] + offset;
 072            indices[startIndex+12] = CubeSegmentIndices[12] + offset;
 073            indices[startIndex+13] = CubeSegmentIndices[13] + offset;
 074            indices[startIndex+14] = CubeSegmentIndices[14] + offset;
 075            indices[startIndex+15] = CubeSegmentIndices[15] + offset;
 076            indices[startIndex+16] = CubeSegmentIndices[16] + offset;
 077            indices[startIndex+17] = CubeSegmentIndices[17] + offset;
 078            indices[startIndex+18] = CubeSegmentIndices[18] + offset;
 079            indices[startIndex+19] = CubeSegmentIndices[19] + offset;
 080            indices[startIndex+20] = CubeSegmentIndices[20] + offset;
 081            indices[startIndex+21] = CubeSegmentIndices[21] + offset;
 082            indices[startIndex+22] = CubeSegmentIndices[22] + offset;
 083            indices[startIndex+23] = CubeSegmentIndices[23] + offset;
 084        }
 85
 86        /// <summary>
 87        ///     Get the vertices that make up a cube.
 88        /// </summary>
 89        /// <remarks>
 90        ///     Ordered based on <see cref="CubeSegmentIndices"/>.
 91        /// </remarks>
 92        /// <param name="center">The world space center location of the cube.</param>
 93        /// <param name="size">The size of the cube.</param>
 94        /// <returns>An array of ordered vertices.</returns>
 95        static Vector3[] GetCubeVertices(Vector3 center, Quaternion rotation, Vector3 size)
 096        {
 097            Vector3 half = size / 2f;
 98
 099            float centerMinusHalfX = center.x - half.x;
 0100            float centerMinusHalfY = center.y - half.y;
 0101            float centerMinusHalfZ = center.z - half.z;
 0102            float centerPlusHalfX = center.x + half.x;
 0103            float centerPlusHalfY = center.y + half.y;
 0104            float centerPlusHalfZ = center.z + half.z;
 105
 0106            Vector3[] points =
 107            {
 108                rotation * new Vector3(centerMinusHalfX, centerMinusHalfY, centerMinusHalfZ), // Front Bottom Left (0)
 109                rotation * new Vector3(centerMinusHalfX, centerMinusHalfY, centerPlusHalfZ), // Back Bottom Left (1)
 110                rotation * new Vector3(centerMinusHalfX, centerPlusHalfY, centerPlusHalfZ), // Back Top Left (2)
 111                rotation * new Vector3(centerMinusHalfX, centerPlusHalfY, centerMinusHalfZ), // Front Top Left (3)
 112                rotation * new Vector3(centerPlusHalfX, centerMinusHalfY, centerMinusHalfZ), // Front Bottom Right (4)
 113                rotation * new Vector3(centerPlusHalfX, centerMinusHalfY, centerPlusHalfZ), // Back Bottom Right (5)
 114                rotation * new Vector3(centerPlusHalfX, centerPlusHalfY, centerPlusHalfZ), // Back Top Right (6)
 115                rotation * new Vector3(centerPlusHalfX, centerPlusHalfY, centerMinusHalfZ), // Front Top Right (7)
 116            };
 117
 0118            return points;
 0119        }
 120
 121        static void GetCubeVerticesNonAlloc(ref Vector3[] points, int startIndex, Vector3 center, Quaternion rotation, V
 0122        {
 0123            Vector3 half = size / 2f;
 124
 0125            float centerMinusHalfX = center.x - half.x;
 0126            float centerMinusHalfY = center.y - half.y;
 0127            float centerMinusHalfZ = center.z - half.z;
 0128            float centerPlusHalfX = center.x + half.x;
 0129            float centerPlusHalfY = center.y + half.y;
 0130            float centerPlusHalfZ = center.z + half.z;
 131
 0132            points[startIndex] = rotation * new Vector3(centerMinusHalfX, centerMinusHalfY, centerMinusHalfZ); // Front 
 0133            points[startIndex + 1] = rotation * new Vector3(centerMinusHalfX, centerMinusHalfY, centerPlusHalfZ); // Bac
 0134            points[startIndex + 2] = rotation * new Vector3(centerMinusHalfX, centerPlusHalfY, centerPlusHalfZ); // Back
 0135            points[startIndex + 3] = rotation * new Vector3(centerMinusHalfX, centerPlusHalfY, centerMinusHalfZ); // Fro
 0136            points[startIndex + 4] = rotation * new Vector3(centerPlusHalfX, centerMinusHalfY, centerMinusHalfZ); // Fro
 0137            points[startIndex + 5] = rotation * new Vector3(centerPlusHalfX, centerMinusHalfY, centerPlusHalfZ); // Back
 0138            points[startIndex + 6] = rotation * new Vector3(centerPlusHalfX, centerPlusHalfY, centerPlusHalfZ); // Back 
 0139            points[startIndex + 7] = rotation * new Vector3(centerPlusHalfX, centerPlusHalfY, centerMinusHalfZ); // Fron
 0140        }
 141
 142        /// <summary>
 143        ///     Draw a dotted line cube of a specific color to the buffer.
 144        /// </summary>
 145        /// <param name="color">The color which to draw the dotted line cube with.</param>
 146        /// <param name="center">The center world position of the cube.</param>
 147        /// <param name="size">The unit size of the cube</param>
 148        /// <returns>The created cube's invalidation token.</returns>
 149        public static int DrawDottedCube(this DebugDrawBuffer buffer, Color color, Vector3 center, Quaternion rotation, 
 0150        {
 0151            Vector3[] vertices = GetCubeVertices(center, rotation, size);
 0152            return buffer.DrawDottedLines(color, ref vertices, ref CubeSegmentIndices);
 0153        }
 154
 0155        static readonly Quaternion k_RotationPrimaryTopLoop = Quaternion.Euler(0, 90, 0);
 0156        static readonly Quaternion k_RotationPrimaryBottomLoop = Quaternion.Euler(0, -90, 180);
 157
 0158        static readonly Quaternion k_RotationSecondaryTopLoop = Quaternion.Euler(0, 180, 0);
 0159        static readonly Quaternion k_RotationSecondaryBottomLoop = Quaternion.Euler(0, 0, 180);
 160
 161
 162        public static int DrawWireCapsule(this DebugDrawBuffer buffer, Color color, Vector3 bottomSpherePosition, Vector
 0163        {
 164            // Calculate total vertices
 0165            int totalVertices = arcVertexCount * 4;
 166
 167            // TODO: add circles?
 0168            Vector3[] vertices = new Vector3[totalVertices];
 0169            float baseAngle = 0f;
 0170            float arcLength = 180f;
 0171            int lineCount = arcVertexCount - 1;
 172
 0173            int bottomPrimaryStartIndex = arcVertexCount;
 0174            int topSecondaryStartIndex = bottomPrimaryStartIndex * 2;
 0175            int bottomSecondaryStartIndex = bottomPrimaryStartIndex * 3;
 176
 0177            Quaternion primaryTopRotation = rotation * k_RotationPrimaryTopLoop;
 0178            Quaternion primaryBottomRotation = rotation * k_RotationPrimaryBottomLoop;
 0179            Quaternion secondaryTopRotation = rotation * k_RotationSecondaryTopLoop;
 0180            Quaternion secondaryBottomRotation = rotation * k_RotationSecondaryBottomLoop;
 181
 0182            for (int i = 0; i < arcVertexCount; i++)
 0183            {
 0184                float currentAngle = Deg2Rad * baseAngle;
 185
 0186                Vector3 basePosition = new Vector3(0, Mathf.Sin(currentAngle) * radius, Mathf.Cos(currentAngle) * radius
 187
 0188                vertices[i] = primaryTopRotation * basePosition + topSpherePosition;
 0189                vertices[i+bottomPrimaryStartIndex] = primaryBottomRotation * basePosition + bottomSpherePosition;
 190
 0191                vertices[i+topSecondaryStartIndex] = secondaryTopRotation * basePosition + topSpherePosition;
 0192                vertices[i+bottomSecondaryStartIndex] = secondaryBottomRotation * basePosition + bottomSpherePosition;
 193
 0194                baseAngle += arcLength / lineCount;
 0195            }
 196
 197
 198            // Create segment connections
 0199            int blockSize = (lineCount * 2) + 2;
 0200            int[] segments = new int[blockSize * 4];
 201
 0202            int primaryTopBottomConnectionIndex = blockSize - 2;
 0203            int primaryBottomTopConnectionIndex = (blockSize * 2) - 2;
 0204            int secondaryTopBottomConnectionIndex = (blockSize * 3) - 2;
 0205            int secondaryBottomTopConnectionIndex = (blockSize * 4) - 2;
 206
 0207            int segmentCount = segments.Length;
 0208            int baseCount = 0;
 209
 0210            for (int i = 0; i < segmentCount; i+=2)
 0211            {
 0212                if (i == primaryTopBottomConnectionIndex || i == primaryBottomTopConnectionIndex || i == secondaryTopBot
 0213                {
 0214                    baseCount++;
 0215                    continue;
 216                }
 217
 0218                segments[i] = baseCount;
 0219                baseCount++;
 0220                segments[i + 1] = baseCount;
 0221            }
 222
 0223            segments[primaryTopBottomConnectionIndex] = segments[primaryTopBottomConnectionIndex - 1];
 0224            segments[primaryTopBottomConnectionIndex+1] = segments[primaryTopBottomConnectionIndex + 2];
 225
 0226            segments[primaryBottomTopConnectionIndex] = segments[primaryBottomTopConnectionIndex - 1];
 0227            segments[primaryBottomTopConnectionIndex+1] = segments[0];
 228
 0229            segments[secondaryTopBottomConnectionIndex] = segments[secondaryTopBottomConnectionIndex - 1];
 0230            segments[secondaryTopBottomConnectionIndex+1] = segments[secondaryTopBottomConnectionIndex + 2];
 231
 0232            segments[secondaryBottomTopConnectionIndex] = segments[secondaryBottomTopConnectionIndex - 1];
 0233            segments[secondaryBottomTopConnectionIndex+1] = segments[primaryBottomTopConnectionIndex + 2];
 234
 235            // Link top to bottom
 0236            return buffer.DrawLines(color, ref vertices, ref segments);
 0237        }
 238
 239        public static int DrawWireArc(this DebugDrawBuffer buffer, Color color, Vector3 center, Quaternion rotation, flo
 0240        {
 241            // We do the plus one to complete the full arc segment, otherwise it would not be every peice
 0242            Vector3[] vertices = new Vector3[arcVertexCount];
 0243            float baseAngle = startAngle;
 0244            float arcLength = endAngle - startAngle;
 0245            int lineCount = arcVertexCount - 1;
 0246            for (int i = 0; i < arcVertexCount; i++)
 0247            {
 0248                float currentAngle = Deg2Rad * baseAngle;
 0249                vertices[i] = (rotation * new Vector3(0, Mathf.Sin(currentAngle) * radius,Mathf.Cos(currentAngle) * radi
 0250                baseAngle += (arcLength / lineCount);
 0251            }
 252
 253            // Create segment connections
 0254            int[] segments = new int[lineCount * 2];
 0255            int segmentCount = segments.Length;
 0256            int baseCount = 0;
 0257            for (int i = 0; i < segmentCount; i+=2)
 0258            {
 0259                segments[i] = baseCount;
 0260                baseCount++;
 0261                segments[i + 1] = baseCount;
 0262            }
 0263            return buffer.DrawLines(color, ref vertices, ref segments);
 0264        }
 265
 266        public static int DrawWireCircle(this DebugDrawBuffer buffer, Color color, Vector3 center, Quaternion rotation, 
 0267        {
 0268            Vector3[] vertices = new Vector3[circleVertexCount];
 0269            float radiansInterval = PI * 2f / circleVertexCount;
 270
 271            // Loop through and figure out the points
 0272            for (int i = 0; i < circleVertexCount; i++)
 0273            {
 0274                float angle = i * radiansInterval;
 275
 276                // Create base point
 0277                vertices[i] = (rotation * new Vector3(0, math.sin(angle) * radius, math.cos(angle) * radius)) + center;
 0278            }
 279
 0280            int[] segments = new int[circleVertexCount * 2];
 0281            int segmentCount = segments.Length;
 0282            int baseCount = 0;
 283
 0284            for (int i = 0; i < segmentCount; i+=2)
 0285            {
 0286                segments[i] = baseCount;
 0287                baseCount++;
 0288                segments[i + 1] = baseCount;
 0289            }
 0290            segments[segmentCount - 1] = 0;
 291
 0292            return buffer.DrawLines(color, ref vertices, ref segments);
 0293        }
 294
 295        /// <summary>
 296        ///     Draw a wireframe cube of a specific color to the buffer.
 297        /// </summary>
 298        /// <param name="color">The color which to draw the wire cube with.</param>
 299        /// <param name="center">The center world position of the cube.</param>
 300        /// <param name="size">The unit size of the cube</param>
 301        /// <returns>The created cube's invalidation token.</returns>
 302        public static int DrawWireCube(this DebugDrawBuffer buffer, Color color, Vector3 center, Quaternion rotation, Ve
 0303        {
 0304            Vector3[] vertices = GetCubeVertices(center, rotation, size);
 0305            return buffer.DrawLines(color, ref vertices, ref CubeSegmentIndices);
 0306        }
 307
 308        public static int DrawWireSphere(this DebugDrawBuffer buffer, Color color, Vector3 center, Quaternion rotation, 
 0309        {
 0310            int pointCount = circleVertexCount * 2;
 0311            Vector3[] vertices = new Vector3[pointCount];
 312
 0313            float radiansInterval = PI * 2f / circleVertexCount;
 0314            Quaternion xRotation = Space.Axis.X.ToRotation() * rotation;
 0315            Quaternion yRotation = Space.Axis.Y.ToRotation() * rotation;
 316
 317            // Loop through and figure out the points
 0318            for (int i = 0; i < circleVertexCount; i++)
 0319            {
 0320                float angle = i * radiansInterval;
 321
 322                // Create base points
 0323                vertices[i] = (xRotation * new Vector3(0, math.sin(angle) * radius, math.cos(angle) * radius)) + center;
 0324                vertices[i+circleVertexCount] = (yRotation * new Vector3(0, math.sin(angle) * radius, math.cos(angle) * 
 0325            }
 326
 327            // Create segment connections
 0328            int[] segments = new int[pointCount * 2];
 0329            int segmentCount = segments.Length;
 0330            int baseCount = 0;
 0331            for (int i = 0; i < segmentCount; i+=2)
 0332            {
 0333                segments[i] = baseCount;
 0334                baseCount++;
 0335                segments[i + 1] = baseCount;
 0336            }
 0337            segments[pointCount - 1] = 0;
 0338            segments[segmentCount - 1] = circleVertexCount;
 339
 0340            return buffer.DrawLines(color, ref vertices, ref segments);
 0341        }
 342    }
 343}