Skip to content

Instantly share code, notes, and snippets.

@dpid
Created September 16, 2017 20:49
Show Gist options
  • Save dpid/f83693a0fae1a1dc3f4f4398d4b71697 to your computer and use it in GitHub Desktop.
Save dpid/f83693a0fae1a1dc3f4f4398d4b71697 to your computer and use it in GitHub Desktop.
ARCore tracked plane rigidbody class. An experiment for adding tracked ground collision.
using System.Collections.Generic;
using UnityEngine;
using GoogleARCore;
using GoogleARCoreInternal;
public class TrackedPlaneRigidbody : MonoBehaviour
{
public bool isRenderingMesh = false;
private TrackedPlane trackedPlane;
private List<Vector3> meshVertices = new List<Vector3>();
private List<int> meshIndices = new List<int>();
private Mesh mesh;
private MeshRenderer meshRenderer;
private MeshCollider meshCollider;
private void Awake()
{
mesh = GetComponent<MeshFilter>().mesh;
meshRenderer = GetComponent<UnityEngine.MeshRenderer>();
meshCollider = GetComponent<MeshCollider>();
}
private void Update()
{
if (trackedPlane == null)
{
return;
}
else if (trackedPlane.SubsumedBy != null)
{
Destroy(gameObject);
return;
}
else if (!trackedPlane.IsValid || Frame.TrackingState != FrameTrackingState.Tracking)
{
meshRenderer.enabled = false;
return;
}
meshRenderer.enabled = isRenderingMesh;
if (trackedPlane.IsUpdated)
{
_UpdateMeshWithCurrentTrackedPlane();
}
}
public void SetTrackedPlane(TrackedPlane plane)
{
trackedPlane = plane;
_UpdateMeshWithCurrentTrackedPlane();
}
private void _UpdateMeshWithCurrentTrackedPlane()
{
// Note that GetBoundaryPolygon returns points in clockwise order.
trackedPlane.GetBoundaryPolygon(ref meshVertices);
Vector3 planeCenter = trackedPlane.Position;
int planePolygonCount = meshVertices.Count;
// The following code convert a flat plane polygon to a 3D mesh with two polygons.
// The indices shown in the diagram are used in comments below.
// _______________ 0_______________1
// | | |4___________5|
// | | | | | |
// | | => | | | |
// | | | | | |
// | | |7-----------6|
// --------------- 3---------------2
// Add vertex 4 to 7.
for (int i = 0; i < planePolygonCount; ++i)
{
Vector3 vItem = meshVertices[i];
vItem += Vector3.down;
meshVertices.Add(vItem);
}
meshIndices.Clear();
int verticeLength = meshVertices.Count;
int verticeLengthHalf = verticeLength / 2;
// Generate triangle (4, 5, 6) and (4, 6, 7).
for (int i = verticeLengthHalf + 1; i < verticeLength - 1; ++i)
{
meshIndices.Add(verticeLengthHalf);
meshIndices.Add(i);
meshIndices.Add(i + 1);
}
// Generate triangle (0, 1, 4), (4, 1, 5), (5, 1, 2), (5, 2, 6), (6, 2, 3), (6, 3, 7)
// (7, 3, 0), (7, 0, 4)
for (int i = 0; i < verticeLengthHalf; ++i)
{
meshIndices.Add(i);
meshIndices.Add((i + 1) % verticeLengthHalf);
meshIndices.Add(i + verticeLengthHalf);
meshIndices.Add(i + verticeLengthHalf);
meshIndices.Add((i + 1) % verticeLengthHalf);
meshIndices.Add((i + verticeLengthHalf + 1) % verticeLengthHalf + verticeLengthHalf);
}
mesh.Clear();
mesh.SetVertices(meshVertices);
mesh.SetIndices(meshIndices.ToArray(), MeshTopology.Triangles, 0);
if (meshCollider != null)
{
meshCollider.sharedMesh = null;
meshCollider.sharedMesh = mesh;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment