Skip to content

Commit fec94ce

Browse files
Bounding box 3d visualizer (#175)
* 3D bounding box visualizer * Updated changelog
1 parent 020b91e commit fec94ce

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

com.unity.perception/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Added new capture options to Perception Camera:
3535
* Can now render intermediate frames between captures.
3636
* Capture can now be triggered manually using a function call, instead of automatic capturing on a schedule.
3737

38+
Added 3D bounding box visualizer
39+
3840
Categorical Parameters will now validate that their specified options are unique at runtime.
3941

4042
### Changed

com.unity.perception/Runtime/GroundTruth/Labelers/BoundingBox3DLabeler.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,15 @@ public struct BoxData
8888

8989
int m_CurrentFrame;
9090

91+
92+
/// <summary>
93+
/// Color to use for 3D visualization box
94+
/// </summary>
95+
// ReSharper disable once MemberCanBePrivate.Global
96+
public Color visualizationColor = Color.green;
97+
9198
/// <inheritdoc/>
92-
protected override bool supportsVisualization => false;
99+
protected override bool supportsVisualization => true;
93100

94101
/// <summary>
95102
/// Fired when the bounding boxes are computed for a frame.
@@ -306,5 +313,54 @@ void ProcessEntity(Labeling labeledEntity)
306313
}
307314
}
308315
}
316+
317+
static Vector3 CalculateRotatedPoint(Camera cam, Vector3 start, Vector3 xDirection, Vector3 yDirection, Vector3 zDirection, float xScalar, float yScalar, float zScalar)
318+
{
319+
var rotatedPoint = start + xDirection * xScalar + yDirection * yScalar + zDirection * zScalar;
320+
var worldPoint = cam.transform.TransformPoint(rotatedPoint);
321+
return VisualizationHelper.ConvertToScreenSpace(cam, worldPoint);
322+
}
323+
324+
protected override void OnVisualize()
325+
{
326+
if (m_ToReport == null) return;
327+
328+
var cam = perceptionCamera.attachedCamera;
329+
330+
foreach (var box in m_ToReport)
331+
{
332+
var t = box.translation;
333+
334+
var right = box.rotation * Vector3.right;
335+
var up = box.rotation * Vector3.up;
336+
var forward = box.rotation * Vector3.forward;
337+
338+
var s = box.size * 0.5f;
339+
var bbl = CalculateRotatedPoint(cam, t,right, up, forward,-s.x,-s.y, -s.z);
340+
var btl = CalculateRotatedPoint(cam, t,right, up, forward,-s.x, s.y, -s.z);
341+
var btr = CalculateRotatedPoint(cam, t,right, up, forward,s.x, s.y, -s.z);
342+
var bbr = CalculateRotatedPoint(cam, t,right, up, forward,s.x, -s.y, -s.z);
343+
344+
VisualizationHelper.DrawLine(bbl, btl, visualizationColor);
345+
VisualizationHelper.DrawLine(bbl, bbr, visualizationColor);
346+
VisualizationHelper.DrawLine(btr, btl, visualizationColor);
347+
VisualizationHelper.DrawLine(btr, bbr, visualizationColor);
348+
349+
var fbl = CalculateRotatedPoint(cam, t,right, up, forward,-s.x,-s.y, s.z);
350+
var ftl = CalculateRotatedPoint(cam, t,right, up, forward,-s.x, s.y, s.z);
351+
var ftr = CalculateRotatedPoint(cam, t,right, up, forward,s.x, s.y, s.z);
352+
var fbr = CalculateRotatedPoint(cam, t,right, up, forward,s.x, -s.y, s.z);
353+
354+
VisualizationHelper.DrawLine(fbl, ftl, visualizationColor);
355+
VisualizationHelper.DrawLine(fbl, fbr, visualizationColor);
356+
VisualizationHelper.DrawLine(ftr, ftl, visualizationColor);
357+
VisualizationHelper.DrawLine(ftr, fbr, visualizationColor);
358+
359+
VisualizationHelper.DrawLine(fbl, bbl, visualizationColor);
360+
VisualizationHelper.DrawLine(fbr, bbr, visualizationColor);
361+
VisualizationHelper.DrawLine(ftl, btl, visualizationColor);
362+
VisualizationHelper.DrawLine(ftr, btr, visualizationColor);
363+
}
364+
}
309365
}
310366
}

0 commit comments

Comments
 (0)