Skip to content

Commit 54df0c8

Browse files
committed
Combined the split API for upcoming features
1 parent 43b33f7 commit 54df0c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1491
-1820
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Raycast Visualization
22
This asset allows users to view raycasts as the user fires them.
33

4-
## Usage
5-
Replace `Physics.` with `VisualPhysics.` when firing a raycast to get a visual.
4+
Currently only supports the 3d physics API (The 2D api will be coming soon).
65

7-
This class is housed within the `Nomnom.RaycastVisualization` namespace.
6+
## Usage
7+
Replace `Physics.` with `VisualPhysics.` when firing a raycast to get a visual.
88

99
## Installation
1010
#### Using Unity Package Manager

Runtime/Shapes.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Shapes/Arrow.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Runtime.CompilerServices;
2+
using UnityEngine;
3+
4+
namespace Nomnom.RaycastVisualization.Shapes {
5+
internal struct Arrow {
6+
public static Arrow Default => new Arrow {
7+
headLength = 0.1f,
8+
headAngle = 20f,
9+
position = 1
10+
};
11+
12+
public Vector3 origin;
13+
public Vector3 direction;
14+
public float headLength;
15+
public float headAngle;
16+
public float position;
17+
public bool overrideLength;
18+
19+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
20+
public void Draw(Color color) {
21+
float headLength = this.headLength;
22+
23+
if (!overrideLength) {
24+
headLength = VisualPhysicsSettingsHandler.GetEditorSettings().RegularArrowLength;
25+
}
26+
27+
Quaternion rot = direction == Vector3.zero ? Quaternion.identity : Quaternion.LookRotation(direction);
28+
Vector3 backDir = Vector3.back * headLength;
29+
Vector3 right = rot * Quaternion.Euler(headAngle, 0, 0) * backDir;
30+
Vector3 left = rot * Quaternion.Euler(-headAngle, 0, 0) * backDir;
31+
Vector3 up = rot * Quaternion.Euler(0, headAngle, 0) * backDir;
32+
Vector3 down = rot * Quaternion.Euler(0, -headAngle, 0) * backDir;
33+
34+
Vector3 arrowTip = origin + direction * position;
35+
36+
Debug.DrawRay(origin, direction, color, 0, true);
37+
Debug.DrawRay(arrowTip, right, color, 0, true);
38+
Debug.DrawRay(arrowTip, left, color, 0, true);
39+
Debug.DrawRay(arrowTip, up, color, 0, true);
40+
Debug.DrawRay(arrowTip, down, color, 0, true);
41+
}
42+
}
43+
}

Runtime/Shapes/Arrow.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Shapes/Capsule.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Runtime.CompilerServices;
2+
using UnityEngine;
3+
4+
namespace Nomnom.RaycastVisualization.Shapes {
5+
internal struct Capsule {
6+
public Vector3 from;
7+
public Vector3 to;
8+
public Vector3 direction;
9+
public float radius;
10+
public float maxDistance;
11+
public RaycastHit hit;
12+
public bool didHit;
13+
14+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
15+
public void Draw(Color color, float length) {
16+
Vector3 dir = direction * length;
17+
Vector3 point1Pos = from + dir;
18+
Vector3 point2Pos = to + dir;
19+
20+
Sphere sphere1 = new Sphere {
21+
origin = point1Pos,
22+
radius = radius
23+
};
24+
25+
Sphere sphere2 = new Sphere {
26+
origin = point2Pos,
27+
radius = radius
28+
};
29+
30+
Debug.DrawLine(point1Pos + Vector3.forward * radius, point2Pos + Vector3.forward * radius, color, 0, true);
31+
Debug.DrawLine(point1Pos + Vector3.back * radius, point2Pos + Vector3.back * radius, color, 0, true);
32+
Debug.DrawLine(point1Pos + Vector3.right * radius, point2Pos + Vector3.right * radius, color, 0, true);
33+
Debug.DrawLine(point1Pos + Vector3.left * radius, point2Pos + Vector3.left * radius, color, 0, true);
34+
35+
sphere1.Draw(color);
36+
sphere2.Draw(color);
37+
}
38+
}
39+
}

Runtime/Shapes/Capsule.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Shapes/Circle.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Runtime.CompilerServices;
2+
using UnityEngine;
3+
4+
namespace Nomnom.RaycastVisualization.Shapes {
5+
internal struct Circle {
6+
public static Circle Default => new Circle {
7+
radius = 0.025f
8+
};
9+
10+
private static uint Iterations => VisualPhysicsSettingsHandler.GetEditorSettings().CircleResolution;
11+
12+
public Vector3 origin;
13+
public Vector3 upDirection;
14+
public float radius;
15+
16+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17+
public void Draw(Color color) {
18+
float[] sinCache = Utils.Sin;
19+
float[] cosCache = Utils.Cos;
20+
21+
Vector3 lastPosition = Vector3.zero;
22+
Vector3 cachePosition = Vector3.zero;
23+
24+
uint iterations = Iterations;
25+
for (int i = 0; i <= iterations; i++) {
26+
float sin = sinCache[i] * radius;
27+
float cos = cosCache[i] * radius;
28+
29+
cachePosition.x = cos;
30+
cachePosition.y = sin;
31+
cachePosition.z = 0;
32+
33+
Quaternion rot = upDirection == Vector3.zero ? Quaternion.identity : Quaternion.LookRotation(upDirection);
34+
35+
cachePosition = rot * cachePosition;
36+
cachePosition += origin;
37+
38+
if (i != 0) {
39+
Debug.DrawLine(lastPosition, cachePosition, color, 0, true);
40+
}
41+
42+
lastPosition = cachePosition;
43+
}
44+
}
45+
}
46+
}

Runtime/Shapes/Circle.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Shapes/Cube.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Runtime.CompilerServices;
2+
using UnityEngine;
3+
4+
namespace Nomnom.RaycastVisualization.Shapes {
5+
internal struct Cube {
6+
public Vector3 origin;
7+
public Vector3 size;
8+
public Quaternion rotation;
9+
10+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11+
public void Draw(Color color) {
12+
Matrix4x4 matrix4X4 = Matrix4x4.TRS(origin, rotation, size);
13+
14+
Vector3 blF = matrix4X4.MultiplyPoint(new Vector3(-1, -1, 1));
15+
Vector3 brF = matrix4X4.MultiplyPoint(new Vector3(1, -1, 1));
16+
Vector3 tlF = matrix4X4.MultiplyPoint(new Vector3(-1, 1, 1));
17+
Vector3 trF = matrix4X4.MultiplyPoint(new Vector3(1, 1, 1));
18+
19+
Vector3 blB = matrix4X4.MultiplyPoint(new Vector3(-1, -1, -1));
20+
Vector3 brB = matrix4X4.MultiplyPoint(new Vector3(1, -1, -1));
21+
Vector3 tlB = matrix4X4.MultiplyPoint(new Vector3(-1, 1, -1));
22+
Vector3 trB = matrix4X4.MultiplyPoint(new Vector3(1, 1, -1));
23+
24+
Debug.DrawLine(blF, brF, color, 0, true);
25+
Debug.DrawLine(brF, trF, color, 0, true);
26+
Debug.DrawLine(trF, tlF, color, 0, true);
27+
Debug.DrawLine(tlF, blF, color, 0, true);
28+
29+
Debug.DrawLine(blB, brB, color, 0, true);
30+
Debug.DrawLine(brB, trB, color, 0, true);
31+
Debug.DrawLine(trB, tlB, color, 0, true);
32+
Debug.DrawLine(tlB, blB, color, 0, true);
33+
34+
Debug.DrawLine(blB, blF, color, 0, true);
35+
Debug.DrawLine(brB, brF, color, 0, true);
36+
Debug.DrawLine(trB, trF, color, 0, true);
37+
Debug.DrawLine(tlB, tlF, color, 0, true);
38+
}
39+
}
40+
}

Runtime/Shapes/Cube.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Shapes/Line.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using UnityEngine;
2+
3+
namespace Nomnom.RaycastVisualization.Shapes {
4+
internal struct Line {
5+
public Vector3 from;
6+
public Vector3 to;
7+
8+
public void Draw(Color color) {
9+
Debug.DrawLine(from, to, color, 0, true);
10+
}
11+
}
12+
}

Runtime/Shapes/Line.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Shapes/NormalCircle.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Runtime.CompilerServices;
2+
using UnityEngine;
3+
4+
namespace Nomnom.RaycastVisualization.Shapes {
5+
internal struct NormalCircle {
6+
public static NormalCircle Default => new NormalCircle {
7+
radius = 0.025f,
8+
distance = 0.025f
9+
};
10+
11+
private static uint Iterations => VisualPhysicsSettingsHandler.GetEditorSettings().CircleResolution;
12+
private static float CircleDistance => VisualPhysicsSettingsHandler.GetEditorSettings().CircleDistance;
13+
private static float CircleLength => VisualPhysicsSettingsHandler.GetEditorSettings().ImpactCircleNormalArrowLength;
14+
15+
public Vector3 origin;
16+
public Vector3 upDirection;
17+
public float radius;
18+
public float distance;
19+
20+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
21+
public void Draw(Color color) {
22+
float[] sinCache = Utils.Sin;
23+
float[] cosCache = Utils.Cos;
24+
25+
Vector3 lastPosition = Vector3.zero;
26+
Vector3 cachePosition = Vector3.zero;
27+
28+
uint iterations = Iterations;
29+
float distance = CircleDistance;
30+
31+
for (int i = 0; i <= iterations; i++) {
32+
float sin = sinCache[i] * radius;
33+
float cos = cosCache[i] * radius;
34+
35+
cachePosition.x = cos;
36+
cachePosition.y = sin;
37+
cachePosition.z = 0;
38+
39+
Quaternion rot = upDirection == Vector3.zero ? Quaternion.identity : Quaternion.LookRotation(upDirection);
40+
41+
cachePosition = rot * cachePosition;
42+
cachePosition += origin + upDirection * distance;
43+
44+
if (i != 0) {
45+
Debug.DrawLine(lastPosition, cachePosition, color, 0, true);
46+
}
47+
48+
lastPosition = cachePosition;
49+
}
50+
51+
Arrow arrow = Arrow.Default;
52+
53+
arrow.origin = origin;
54+
arrow.direction = upDirection.normalized * this.distance;
55+
arrow.overrideLength = true;
56+
arrow.headLength = CircleLength;
57+
arrow.Draw(VisualUtils.GetDefaultColor());
58+
}
59+
}
60+
}

Runtime/Shapes/NormalCircle.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Shapes/Ray.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Runtime.CompilerServices;
2+
using UnityEngine;
3+
4+
namespace Nomnom.RaycastVisualization.Shapes {
5+
internal struct Ray {
6+
public Vector3 from;
7+
public Vector3 direction;
8+
9+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
10+
public void Draw(Color color) {
11+
Debug.DrawRay(from, direction, color, 0, true);
12+
}
13+
}
14+
}

Runtime/Shapes/Ray.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Shapes/Sphere.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Runtime.CompilerServices;
2+
using UnityEngine;
3+
4+
namespace Nomnom.RaycastVisualization.Shapes {
5+
internal struct Sphere {
6+
private static uint Iterations => VisualPhysicsSettingsHandler.GetEditorSettings().CircleResolution;
7+
8+
public Vector3 origin;
9+
public float radius;
10+
11+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12+
public void Draw(Color color) {
13+
float[] sinCache = Utils.Sin;
14+
float[] cosCache = Utils.Cos;
15+
16+
Vector3 lastPositionHorizontal = default;
17+
Vector3 lastPositionVertical = default;
18+
Vector3 lastPositionVertical2 = default;
19+
20+
uint iterations = Iterations;
21+
for (int i = 0; i <= iterations; i++) {
22+
float sin = sinCache[i] * radius;
23+
float cos = cosCache[i] * radius;
24+
Vector3 horizontal = new Vector3(origin.x + cos, origin.y + sin, origin.z);
25+
Vector3 vertical = new Vector3(origin.x + cos, origin.y, origin.z + sin);
26+
Vector3 vertical2 = new Vector3(origin.x, origin.y + cos, origin.z + sin);
27+
28+
if (i != 0) {
29+
Debug.DrawLine(lastPositionHorizontal, horizontal, color, 0, true);
30+
Debug.DrawLine(lastPositionVertical, vertical, color, 0, true);
31+
Debug.DrawLine(lastPositionVertical2, vertical2, color, 0, true);
32+
}
33+
34+
lastPositionHorizontal = horizontal;
35+
lastPositionVertical = vertical;
36+
lastPositionVertical2 = vertical2;
37+
}
38+
}
39+
}
40+
}

Runtime/Shapes/Sphere.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)