Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Optimize DebugDraw.DrawCircle(), DebugDraw.DrawSolidCircle(), and Pri…
Browse files Browse the repository at this point in the history
…mitiveBatch.AddVertex() (#67)

* reuse previews value of  (center + v1) and (center + v2)

* ref position at AddVertex()

* Use faster ref methods.
  • Loading branch information
nkast authored Dec 22, 2018
1 parent 61d5f18 commit da9f84e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
54 changes: 30 additions & 24 deletions Physics2D.Diagnostics/Diagnostics/DebugView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,13 @@ public override void DrawPolygon(Vector2[] vertices, int count, Color color, boo

for (int i = 0; i < count - 1; i++)
{
_primitiveBatch.AddVertex(vertices[i], color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(vertices[i + 1], color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref vertices[i], color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref vertices[i + 1], color, PrimitiveType.LineList);
}
if (closed)
{
_primitiveBatch.AddVertex(vertices[count - 1], color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(vertices[0], color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref vertices[count - 1], color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref vertices[0], color, PrimitiveType.LineList);
}
}

Expand All @@ -546,9 +546,9 @@ public void DrawSolidPolygon(Vector2[] vertices, int count, Color color, bool ou

for (int i = 1; i < count - 1; i++)
{
_primitiveBatch.AddVertex(vertices[0], colorFill, PrimitiveType.TriangleList);
_primitiveBatch.AddVertex(vertices[i], colorFill, PrimitiveType.TriangleList);
_primitiveBatch.AddVertex(vertices[i + 1], colorFill, PrimitiveType.TriangleList);
_primitiveBatch.AddVertex(ref vertices[0], colorFill, PrimitiveType.TriangleList);
_primitiveBatch.AddVertex(ref vertices[i], colorFill, PrimitiveType.TriangleList);
_primitiveBatch.AddVertex(ref vertices[i + 1], colorFill, PrimitiveType.TriangleList);
}

if (outline)
Expand All @@ -561,19 +561,22 @@ public override void DrawCircle(Vector2 center, float radius, Color color)
throw new InvalidOperationException("BeginCustomDraw must be called before drawing anything.");

Vector2 v2 = new Vector2(radius, 0);
var center_vS = center + v2;
var center_v2 = center + v2;
var center_vS = center_v2;

for (int i = 0; i < CircleSegments - 1; i++)
{
Vector2 v1 = v2;
v2 = Complex.Multiply(ref v1, ref circleSegmentRotation);
var center_v1 = center_v2;
Complex.Multiply(ref v1, ref circleSegmentRotation, out v2);
Vector2.Add(ref center, ref v2, out center_v2);

_primitiveBatch.AddVertex(center + v1, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(center + v2, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref center_v1, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref center_v2, color, PrimitiveType.LineList);
}
// Close Circle
_primitiveBatch.AddVertex(center+v2, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(center_vS, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref center_v2, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref center_vS, color, PrimitiveType.LineList);
}

public override void DrawSolidCircle(Vector2 center, float radius, Vector2 axis, Color color)
Expand All @@ -582,30 +585,33 @@ public override void DrawSolidCircle(Vector2 center, float radius, Vector2 axis,
throw new InvalidOperationException("BeginCustomDraw must be called before drawing anything.");

Vector2 v2 = new Vector2(radius, 0);
var center_vS = center + v2;
var center_v2 = center + v2;
var center_vS = center_v2;

Color colorFill = color * 0.5f;

for (int i = 0; i < CircleSegments-1; i++)
{
Vector2 v1 = v2;
v2 = Complex.Multiply(ref v1, ref circleSegmentRotation);
var center_v1 = center_v2;
Complex.Multiply(ref v1, ref circleSegmentRotation, out v2);
Vector2.Add(ref center, ref v2, out center_v2);

// Draw Circle
_primitiveBatch.AddVertex(center + v1, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(center + v2, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref center_v1, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref center_v2, color, PrimitiveType.LineList);

// Draw Solid Circle
if (i > 0)
{
_primitiveBatch.AddVertex(center_vS, colorFill, PrimitiveType.TriangleList);
_primitiveBatch.AddVertex(center+v1, colorFill, PrimitiveType.TriangleList);
_primitiveBatch.AddVertex(center+v2, colorFill, PrimitiveType.TriangleList);
_primitiveBatch.AddVertex(ref center_vS, colorFill, PrimitiveType.TriangleList);
_primitiveBatch.AddVertex(ref center_v1, colorFill, PrimitiveType.TriangleList);
_primitiveBatch.AddVertex(ref center_v2, colorFill, PrimitiveType.TriangleList);
}
}
// Close Circle
_primitiveBatch.AddVertex(center+v2, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(center_vS, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref center_v2, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref center_vS, color, PrimitiveType.LineList);

DrawSegment(center, center + axis * radius, color);
}
Expand All @@ -615,8 +621,8 @@ public override void DrawSegment(Vector2 start, Vector2 end, Color color)
if (!_primitiveBatch.IsReady())
throw new InvalidOperationException("BeginCustomDraw must be called before drawing anything.");

_primitiveBatch.AddVertex(start, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(end, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref start, color, PrimitiveType.LineList);
_primitiveBatch.AddVertex(ref end, color, PrimitiveType.LineList);
}

public override void DrawTransform(ref Transform transform)
Expand Down
2 changes: 2 additions & 0 deletions Physics2D.Diagnostics/Diagnostics/IPrimitiveBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface IPrimitiveBatch
bool IsReady();
int AddVertex(Vector3 position, Color color, PrimitiveType primitiveType);
int AddVertex(Vector2 position, Color color, PrimitiveType primitiveType);
int AddVertex(ref Vector2 position, Color color, PrimitiveType primitiveType);
int AddVertex(ref Vector3 position, Color color, PrimitiveType primitiveType);
}
}
21 changes: 18 additions & 3 deletions Physics2D.Diagnostics/Diagnostics/PrimitiveBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public bool IsReady()
}

public int AddVertex(Vector3 position, Color color, PrimitiveType primitiveType)
{
return AddVertex(ref position, color, primitiveType);
}

public int AddVertex(ref Vector3 position, Color color, PrimitiveType primitiveType)
{
if (!_hasBegun)
throw new InvalidOperationException("Begin must be called before AddVertex can be called.");
Expand All @@ -144,8 +149,14 @@ public int AddVertex(Vector3 position, Color color, PrimitiveType primitiveType)
throw new NotSupportedException("The specified primitiveType is not supported by PrimitiveBatch.");
}
}



public int AddVertex(Vector2 position, Color color, PrimitiveType primitiveType)
{
return AddVertex(ref position, color, primitiveType);
}

public int AddVertex(ref Vector2 position, Color color, PrimitiveType primitiveType)
{
if (!_hasBegun)
throw new InvalidOperationException("Begin must be called before AddVertex can be called.");
Expand All @@ -156,15 +167,19 @@ public int AddVertex(Vector2 position, Color color, PrimitiveType primitiveType)
if (_triangleVertsCount >= _triangleVertices.Length)
FlushTriangles();

_triangleVertices[_triangleVertsCount].Position = new Vector3(position,DefaultTriangleListDepth);
_triangleVertices[_triangleVertsCount].Position.X = position.X;
_triangleVertices[_triangleVertsCount].Position.Y = position.Y;
_triangleVertices[_triangleVertsCount].Position.Z = DefaultTriangleListDepth;
_triangleVertices[_triangleVertsCount].Color = color;
return _triangleVertsCount++;

case PrimitiveType.LineList:
if (_lineVertsCount >= _lineVertices.Length)
FlushLines();

_lineVertices[_lineVertsCount].Position = new Vector3(position,DefaultLineListDepth);
_lineVertices[_lineVertsCount].Position.X = position.X;
_lineVertices[_lineVertsCount].Position.Y = position.Y;
_lineVertices[_lineVertsCount].Position.Z = DefaultLineListDepth;
_lineVertices[_lineVertsCount].Color = color;
return _lineVertsCount++;

Expand Down

0 comments on commit da9f84e

Please sign in to comment.