Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
Improve ARKit face demo
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo committed Mar 13, 2018
1 parent b650f1f commit b72d055
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
74 changes: 69 additions & 5 deletions ARKit/ARKitXamarinDemo/FaceDemo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using ARKit;
using Urho;
using Urho.iOS;
Expand Down Expand Up @@ -54,22 +55,85 @@ void ArkitComponent_DidRemoveAnchors(ARAnchor[] anchors)
}
}

void UpdateAnchor(Node node, ARAnchor anchor)
unsafe void UpdateAnchor(Node node, ARAnchor anchor)
{
var faceAnchor = anchor as ARFaceAnchor;
if (faceAnchor == null)
return;

StaticModel faceModel;

if (node == null)
{
var id = faceAnchor.Identifier.ToString();
node = anchorsNode.CreateChild(id);
var faceBoxNode = node.CreateChild();
var box = faceBoxNode.CreateComponent<Urho.Shapes.Box>();
faceBoxNode.Scale = new Vector3(0.15f, 0.15f, 0.01f);
faceModel = node.CreateComponent<StaticModel>();
}
else
faceModel = node.GetComponent<StaticModel>();

arkitComponent.ApplyOpenTkTransform(node, faceAnchor.Transform, true);

var faceGeom = faceAnchor.Geometry;
var finalVertices = faceGeom.GetVertices()
.Select(v => new VertexBuffer.PositionNormalColor
{
Color = Color.Green.ToUInt(),
Position = new Vector3(v.X, v.Y, -v.Z)
}).ToArray();

// calculate normals
var indices = faceGeom.GetTriangleIndices();
for (int i = 0; i < indices.Length; i += 3)
{
ref var v1 = ref finalVertices[indices[i]];
ref var v2 = ref finalVertices[indices[i + 1]];
ref var v3 = ref finalVertices[indices[i + 2]];

Vector3 edge1 = v1.Position - v2.Position;
Vector3 edge2 = v1.Position - v3.Position;

var normal = Vector3.Cross(edge1, edge2);
normal = new Vector3(normal.X, -normal.Y, -normal.Z);
normal.Normalize();

v1.Normal = normal;
v2.Normal = normal;
v3.Normal = normal;
}
var model = new Model();
var vertexBuffer = new VertexBuffer(Context, false);
var indexBuffer = new IndexBuffer(Context, false);
var geometry = new Geometry();


vertexBuffer.Shadowed = true;
vertexBuffer.SetSize((uint)faceGeom.VertexCount, ElementMask.Position | ElementMask.Normal | ElementMask.Color, true);

fixed (VertexBuffer.PositionNormalColor* p = &finalVertices[0])
vertexBuffer.SetData((void*)p);

indexBuffer.Shadowed = true;
indexBuffer.SetSize((uint)indices.Length, false, true);
indexBuffer.SetData(indices);

geometry.SetVertexBuffer(0, vertexBuffer);
geometry.IndexBuffer = indexBuffer;
geometry.SetDrawRange(PrimitiveType.TriangleList, 0, (uint)indices.Length, 0, (uint)faceGeom.VertexCount, true);

model.NumGeometries = 1;
model.SetGeometry(0, 0, geometry);
model.BoundingBox = new BoundingBox(-Vector3.One * 0.01f, Vector3.One * 0.01f);
faceModel.Model = model;

if (faceModel.Material == null)
{
var mat = new Material();
mat.SetTechnique(0, CoreAssets.Techniques.NoTextureVCol, 0);
mat.CullMode = CullMode.Cw;
faceModel.SetMaterial(0, mat);
}

arkitComponent.ApplyOpenTkTransform(node, faceAnchor.Transform);
}
}
}
2 changes: 1 addition & 1 deletion ARKit/ARKitXamarinDemo/VerticalSurfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override void Start()
arkitComponent.ARConfiguration = new ARWorldTrackingConfiguration {
PlaneDetection =
ARPlaneDetection.Horizontal
#if false // uncomment once Xamarin.iOS with xcode93 is released:
#if true // requires Xamarin.IOS with xcode93 support
| ARPlaneDetection.Vertical,
#endif
};
Expand Down
Binary file added ARKit/Face.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b72d055

Please sign in to comment.