Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6540360
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Android平台

2017-11-01 20:59:16

现在,您已经有机会通过ARKit在Xamarin iOS应用程序中增加现实,现在是探索Google在Xamarin Android应用程序中对AR的影响力的时候了。
新的ARCore SDK为增强现实功能提供API,例如运动跟踪,平面检测和光估计。 这些是您将用于向您的Android应用添加AR体验的构建块。

入门

ARCore目前只能在精选设备上使用,例如Google Pixel,Google Pixel 2和Samsung Galaxy S8。
为了使用ARCore,您需要通过下载并安装arcore-preview.apk来准备设备。
在您设置ARCore开发设备后,您需要安装ARCore预发行NuGet软件包。

ARCore API基础知识

为了帮助您检测表面放置对象并计算其相对于相机的空间位置,ARCore使用几种基本类型。

  • 会话对象:您与ARCore的主要交互点。它将通过跟踪您添加的任何锚点,引擎检测到的表面以及设备的当前快照来帮助管理AR状态。
  • 平面:SDK检测到的表面,您可以在其上放置锚点来描述物体的固定现实位置(包括其方向)。当前面向上和向下的表面可以单独检测(考虑地板和天花板)。
  • 会话:当您调用.Update()返回一个帧对象时,AR状态的当前快照。
  • 框架:方便的HitTest(..)方法,可以帮助确定显示屏上的抽头坐标是否与任何平面相交。每个框架还包含有关摄像机方向和与现实世界的关系的信息,并帮助计算投影矩阵,以向用户显示视觉表示。
  • SDK的另一个有趣的功能是从给定的帧获取LightEstimate的能力。该估计值包括摄像机视图的像素强度。

基本演练

我们已将HelloAR样本移植到Xamarin,您可以在GitHub上查看! 现在我们来看一下这个例子中的一些基本事情。
首先,在您的活动中,您需要在OnCreate中创建一个会话,并确保运行时在设备上支持ARCore:

点击(此处)折叠或打开

  1. var config = Config.CreateDefaultConfig();
  2. session = new Session(this);
  3.  
  4. // Make sure ARCore is supported on this device
  5. if (!session.IsSupported(config)) {
  6.     Toast.MakeText(this, "ARCore unsupported!", ToastLength.Long).Show();
  7.     Finish();
  8. }



请记住,您还需要请求Android.Manifest.Permission.Camera权限才能向用户显示实况相机馈送/增强现实视图。 在“HelloAR”示例中,我们使用GLSurfaceView向用户呈现相机和增强。 确保您设置了GL Surface,或者查看示例代码中的操作。
在会话运行中,我们可以在GL表面的OnDrawFrame实现中获取AR系统状态的快照。 在我们检查以确保帧处于跟踪状态之后,我们可以检查任何命中结果,并且假设它们与飞机相交,在飞机上添加锚点。

点击(此处)折叠或打开

  1. // See the PlaneAttachment class from the sample
  2. // This helps associate Anchors with Planes they are attached to
  3. List<PlaneAttachment> planeAttachments = new List<PlaneAttachment>();
  4. void OnDrawFrame (IGL10 gl)
  5. {
  6.     var frame = session.Update();
  7.  
  8.     // You could keep track of taps by queueing up
  9.     // MotionEvent's from a tap gesture recognizer
  10.     var tap = motionEventsQueue.Dequeue();
  11.  
  12.     // Make sure we've got a tap and are in a tracking state for our frame
  13.     if (tap != null && frame.GetTrackingState() == Frame.TrackingState.Tracking) {
  14.         // Look at each hittest result
  15.         foreach (var hit in frame.HitTest(tap)) {
  16.             // We could get PointCloudsHitResult as well, check for Plane
  17.             var planeHit = hit as PlaneHitResult;
  18.             if (planeHit != null && planeHit.IsHitInPolygon) {
  19.                 // Create a new anchor
  20.                 var anchor = session.AddAnchor(hit.HitPose);
  21.                 // Keep track of our anchors and the planes they are attached to
  22.                 planeAttachments.Add(new PlaneAttachment(planeHit, anchor))
  23.             }
  24.         }
  25.     }
  26. }



我们还想在我们的绘图方法中渲染我们场景中的各种对象。 HelloAR示例具有各种渲染器,可以根据从框架计算出的投影进行重度OpenGL提升,并实现此目的:

点击(此处)折叠或打开  

  1. // Get projection matrix.
  2. float[] projectionMatrix = new float[16];
  3. session.GetProjectionMatrix(projectionMatrix, 0, 0.1f, 100.0f);
  4.  
  5. // Get camera matrix and draw.
  6. float[] viewMatrix = new float[16];
  7. frame.GetViewMatrix(viewMatrix, 0);
  8.  
  9. // Draw the detected planes
  10. planeRenderer.DrawPlanes(session.AllPlanes, frame.Pose, projectionMatrix);
  11.  
  12. // Get lighting from avg intensity of the image
  13. var lightIntensity = frame.LightEstimate.PixelIntensity;
  14.  
  15. // Draw all of our anchors attached to planes
  16. float scaleFactor = 1.0f;
  17. float[] anchorMatrix = new float[16];
  18.  
  19. foreach (var planeAttachment in planeAttachments) {
  20.     // Only draw attachments currently tracking
  21.     if (!planeAttachment.IsTracking)
  22.         continue;
  23.  
  24.     // Get the current combined pose of an Anchor and Plane in world space
  25.     planeAttachment.GetPose().ToMatrix(anchorMatrix, 0);
  26.  
  27.     // Update and draw the model
  28.     objectRenderer.UpdateModelMatrix(anchorMatrix, scaleFactor);
  29.     objectRenderer.Draw(viewMatrix, projectionMatrix, lightIntensity);
  30. }


解剖样本后,您可以看到实际的ARCore代码相对简单,大部分示例代码是关于OpenGL渲染的。
再次,请确保在GitHub上查看完整的HelloAR示例! 我们期待着您的Xamarin Android应用程序中使用ARCore创建的增强体验体验。

ARCore.zip

阅读(2916) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~