可以看随时查Unity-ARKit-Plugin的项目进展:
https://bitbucket.org/Unity-Technologies/unity-arkit-plugin/src/aa4933787df366c2d3ab15f65a8ceed57f23f1a6?at=default
首先看Unity-ARKit-Plugin的三个文档:
1.README.md:
两个核心文件:
/Assets/Plugins/iOS/UnityARKit/NativeInterface/ARSessionNative.mm
/Assets/Plugins/iOS/UnityARKit/NativeInterface/UnityARSessionNativeInterface.cs
包含下面重要API:
public void RunWithConfigAndOptions(ARKitWorldTackingSessionConfiguration config,UnityARSessionRunOption runOptions)
public void RunWithConfig(ARKitWorldTackingSessionConfiguration config)
public void Pause()
public List HitTest(ARPoint point,ARHitTestResultType types)
public ARTextureHandles GetARVideoTextureHandles()
public float GetARAmbientIntensity()
public int GetARTrackingQuality()
还有delegate:
public delegate void ARFrameUpdate(UnityARCamera camera)
public delegate void ARAnchorAdded(ARPlaneAnchor anchorData)
public delegate void ARAnchorUpdated(ARPlaneAnchor anchorData)
public delegate void ARAnchorRemoved(ARPlaneAnchor anchorData)
public delegate void ARSessionFailed(string error)
一些回调:
public static event ARFrameUpdate ARFrameUpdatedEvent;
public static event ARAnchorAdded ARAnchorAddedEvent;
public static event ARAnchorUpdated ARAnchorUpdatedEvent;
public static event ARAnchorRemoved ARAnchorRemovedEvent;
public static event ARAnchorAdded ARUserAnchorAddedEvent;
public static event ARAnchorUpdated ARUserAnchorUpdatedEvent;
public static event ARAnchorRemoved ARUserAnchorRemovedEvent;
public static event ARSessionCallback ARSessionInterruptedEvent;
public static event ARSessionCallback ARSessioninterruptionEndedEvent;
public static event ARSessionTrackingChanged ARSessionTrackingChangedEvent;
2. TUTORIAL.txt
一些要点作如下总结:
1>一切都从session开始,在文件UnityARCameraManager.cs中有
m_session = UnityARSessionNativeInterface.GetARSessionNativeInterface();
2> ARSession的config根据精度不同分为
ARKitWorldTrackingSessionConfiguration(6DOF)和ARKitSessionConfiguration(3DOF),
这也是UnityARCameraManager.cs和AR3DOFCameraManager.cs两个文件的原因。
3>想要获取相机的位置,可以使用
Matrix4x4 matrix = m_session.GetCameraPose();
camera.transform.localPosition = UnityARMatrixOps.GetPosition(matrix);
camera.transform.localRotation = UnityARMatrixOps.GetRotation(matrix);
同样代码在UnityARCameraManager.cs中。
4> UnityARVideo.cs相机上组件,使用YUVMaterial shader渲染视频纹理。
5> UnityARHitTestExample处理Hit事件,获得Hit与接触平面的相交坐标,可以关联任何GameObject,Hit中的坐标可以控制相关GameObject.
6>可用使用UnityARAnchorManager执行AddAnchor,RemoveAnchor,UpdateAnchor操作。
7> PointCloudParticleExample,显示PointCloud的小黄点。
8> UnityARAmbient,ARKit光照亮度向Unity亮度的转化,Light的组件。
9> UnityARUserAnchorComponent用来移除用户自己添加的ARUserAnchor。
10>重要事件的回调还得去看UnityARSessionNativeInterface.cs
3. SCENES.txt
对Example中给出了Scene进行了介绍
UnityARKitScene.unity:最小场景的ARkit功能演示;
UnityParticlePainter.unity:空间粒子系统画点;
UnityARBallz.unity:平面小球移动与碰撞;
UnityARShadows.unity:在平面显示物体的阴影;
UnityAROcclusion.unity:遮光材料遮挡物体;;
UnityARUserAnchorScene.unity:通过Anchor添加删除物体;
### UnityARKitScene.unity
### UnityParticlePainter.unity
### UnityAROcclusion.unity
效果描述:出现立方体,点击出现小人,场景出现Cloud黄点,但没有显示平面
### UnityARShadows.unity
### UnityARBallz.unity
### AddRemoveAnchorScene.unity
效果描述:点击创建黑白网格立方体,4秒钟后消失
## 重要文件分析
Directional light
类型是Directional,绑定UnityARAmbient.cs
void UpdateAmbientIntensity(UnityARCamera camera)
{
// Convert ARKit intensity to Unity intensity
// ARKit ambient intensity ranges 0-2000
// Unity ambient intensity ranges 0-8(for over-bright lights)
float newai = camera.ambientIntensity;
l.intensity = newai / 1000.0f;
}
Main Camera
因为YUVMaterial是下面这样,所以在Unity中运行的背景是绿色
UnityARCameraManager
将Unity的Camera转成UnityARCamera,需要与MainCamera关联
其中公有成员public Camera m_camera;
公有方法public void SetCamera(Camera newCamera)
GeneratePlanes
与之相关联的UnityARGeneratePlane.cs
公有成员public GameObject planePrefab;
私有成员private UnityARAnchorManager unityARAnchorManager;
在Start()方法中会创建unityARAnchorManager和planePrefab
显示的平面是下面的样子
其中包含UnityARAnchorManager
顾名思义,是ARAnchor的管理类,
初始化时将以下方法绑定UnityARSessionNativeInterface中的Event:
AddAnchor(ARPlaneAnchor arPlaneAnchor)
RemoveAnchor(ARPlaneAnchor arPlaneAnchor)
UpdateAnchor(ARPlaneAnchor arPlaneAnchor)
该类本身存储了对象
Dictionary planeAnchorMap;
其中key string对应arPlaneAnchor.identifier
可通过下面函数获得
public List GetCurrentPlaneAnchors() {
return planeAnchorMap.Values.ToList();
}
ARPlaneAnchorGameObject有个专门的类,代码如下:
namespace UnityEngine.XR.iOS
{
public class ARPlaneAnchorGameObject
{
public GameObject gameObject;
public ARPlaneAnchor planeAnchor;
}
}
还是用到了UnityARUtility
用来对应ARPlaneAnchor生成GameObject,如下面这种GameObject
三个重要方法:
void InitializePlanePrefab(GameObject go)
GameObject CreatePlaneInScene(ARPlaneAnchor arPlaneAnchor)
GameObject UpdatePlaneWithAnchorTransform(GameObject plane,ARPlaneAnchor arPlaneAnchor)
UnityARHitTestExample
点击屏幕,出现灰色立方体
通过hitResult来获取点击位置
PointCloudParticleExample
生成Cloud小黄点的类
其他:
Unity最初在论坛里发布的ARkit Plugin以及介绍
https://forum.unity3d.com/threads/arkit-support-for-ios-via-unity-arkit-plugin.474385/
Unity ARKit Remote
https://forum.unity3d.com/threads/arkit-support-for-ios-via-unity-arkit-plugin.474385/page-13#post-3153415