Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to new ARKit (Fixes compilation issues for non IOS devices) #1462

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions sdkproject/Assets/Resources.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking.PlayerConnection;
using Utils;
using UnityEngine.XR.iOS.Utils;

// Runs on the remote device. Talks to the Editor.
namespace UnityARInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking.PlayerConnection;
using Utils;
using UnityEngine.XR.iOS.Utils;
using System.Collections;
using UnityEngine.XR;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Utils;
using UnityEngine.XR.iOS.Utils;

namespace UnityARInterface
{
Expand Down
16 changes: 9 additions & 7 deletions sdkproject/Assets/UnityARInterface/Scripts/ARKitInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace UnityARInterface
{
#if !UNITY_EDITOR && UNITY_IOS
public class ARKitInterface : ARInterface
{
private Material m_ClearMaterial;
Expand Down Expand Up @@ -137,7 +138,7 @@ void UpdateFrame(UnityARCamera camera)
m_TexturesInitialized = true;
}

m_PointCloudData = camera.pointCloudData;
m_PointCloudData = camera.pointCloud.Points;
m_LightEstimate.capabilities = LightEstimateCapabilities.AmbientColorTemperature | LightEstimateCapabilities.AmbientIntensity;
m_LightEstimate.ambientColorTemperature = camera.lightData.arLightEstimate.ambientColorTemperature;

Expand Down Expand Up @@ -240,7 +241,7 @@ public override bool TryGetUnscaledPose(ref Pose pose)
public override bool TryGetCameraImage(ref CameraImage cameraImage)
{
ARTextureHandles handles = nativeInterface.GetARVideoTextureHandles();
if (handles.textureY == System.IntPtr.Zero || handles.textureCbCr == System.IntPtr.Zero)
if (handles.TextureY == System.IntPtr.Zero || handles.TextureCbCr == System.IntPtr.Zero)
return false;

if (!m_TexturesInitialized)
Expand Down Expand Up @@ -300,7 +301,7 @@ public override void UpdateCamera(Camera camera)
return;

ARTextureHandles handles = UnityARSessionNativeInterface.GetARSessionNativeInterface().GetARVideoTextureHandles();
if (handles.textureY == System.IntPtr.Zero || handles.textureCbCr == System.IntPtr.Zero)
if (handles.TextureY == System.IntPtr.Zero || handles.TextureCbCr == System.IntPtr.Zero)
{
m_CanRenderBackground = false;
return;
Expand All @@ -315,7 +316,7 @@ public override void UpdateCamera(Camera camera)
if (_videoTextureY == null)
{
_videoTextureY = Texture2D.CreateExternalTexture(currentResolution.width, currentResolution.height,
TextureFormat.R8, false, false, (System.IntPtr)handles.textureY);
TextureFormat.R8, false, false, (System.IntPtr)handles.TextureY);
_videoTextureY.filterMode = FilterMode.Bilinear;
_videoTextureY.wrapMode = TextureWrapMode.Repeat;
m_ClearMaterial.SetTexture("_textureY", _videoTextureY);
Expand All @@ -325,14 +326,14 @@ public override void UpdateCamera(Camera camera)
if (_videoTextureCbCr == null)
{
_videoTextureCbCr = Texture2D.CreateExternalTexture(currentResolution.width, currentResolution.height,
TextureFormat.RG16, false, false, (System.IntPtr)handles.textureCbCr);
TextureFormat.RG16, false, false, (System.IntPtr)handles.TextureCbCr);
_videoTextureCbCr.filterMode = FilterMode.Bilinear;
_videoTextureCbCr.wrapMode = TextureWrapMode.Repeat;
m_ClearMaterial.SetTexture("_textureCbCr", _videoTextureCbCr);
}

_videoTextureY.UpdateExternalTexture(handles.textureY);
_videoTextureCbCr.UpdateExternalTexture(handles.textureCbCr);
_videoTextureY.UpdateExternalTexture(handles.TextureY);
_videoTextureCbCr.UpdateExternalTexture(handles.TextureCbCr);

m_ClearMaterial.SetMatrix("_DisplayTransform", m_DisplayTransform);
}
Expand Down Expand Up @@ -377,4 +378,5 @@ public override void DestroyAnchor(ARAnchor arAnchor)
}
}
}
#endif
}
5 changes: 2 additions & 3 deletions sdkproject/Assets/UnityARKitPlugin.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
using UnityEngine;
using UnityEngine.Networking.PlayerConnection;
using System.Text;
using UnityEngine.XR.iOS.Utils;

#if UNITY_EDITOR

using UnityEditor.Networking.PlayerConnection;

namespace UnityEngine.XR.iOS
{
public class ARKitFaceTrackingRemoteConnection : MonoBehaviour
{
[Header("AR FaceTracking Config Options")]
public bool enableLightEstimation = true;

[Header("Run Options")]
public bool resetTracking = true;
public bool removeExistingAnchors = true;

EditorConnection editorConnection ;

int currentPlayerID = -1;
string guimessage = "none";

Texture2D remoteScreenYTex;
Texture2D remoteScreenUVTex;

bool bTexturesInitialized;

// Use this for initialization
void Start () {

bTexturesInitialized = false;


editorConnection = EditorConnection.instance;
editorConnection.Initialize ();
editorConnection.RegisterConnection (PlayerConnected);
editorConnection.RegisterDisconnection (PlayerDisconnected);
editorConnection.Register (ConnectionMessageIds.updateCameraFrameMsgId, UpdateCameraFrame);
editorConnection.Register (ConnectionMessageIds.addFaceAnchorMsgeId, AddFaceAnchor);
editorConnection.Register (ConnectionMessageIds.updateFaceAnchorMsgeId, UpdateFaceAnchor);
editorConnection.Register (ConnectionMessageIds.removePlaneAnchorMsgeId, RemoveFaceAnchor);
editorConnection.Register (ConnectionMessageIds.screenCaptureYMsgId, ReceiveRemoteScreenYTex);
editorConnection.Register (ConnectionMessageIds.screenCaptureUVMsgId, ReceiveRemoteScreenUVTex);

}

void PlayerConnected(int playerID)
{
currentPlayerID = playerID;

}

void OnGUI()
{

if (!bTexturesInitialized)
{
if (currentPlayerID != -1) {
guimessage = "Connected to ARKit Remote device : " + currentPlayerID.ToString ();

if (GUI.Button (new Rect ((Screen.width / 2) - 200, (Screen.height / 2) - 200, 400, 100), "Start Remote ARKit FaceTracking Session"))
{
SendInitToPlayer ();
}
}
else
{
guimessage = "Please connect to player in the console menu";
}

GUI.Box (new Rect ((Screen.width / 2) - 200, (Screen.height / 2) + 100, 400, 50), guimessage);
}

}

void PlayerDisconnected(int playerID)
{
if (currentPlayerID == playerID) {
currentPlayerID = -1;
}
}

void OnDestroy()
{
#if UNITY_2017_1_OR_NEWER
if(editorConnection != null) {
editorConnection.DisconnectAll ();
}
#endif
}


void InitializeTextures(UnityARCamera camera)
{
int yWidth = camera.videoParams.yWidth;
int yHeight = camera.videoParams.yHeight;
int uvWidth = yWidth / 2;
int uvHeight = yHeight / 2;
if (remoteScreenYTex == null || remoteScreenYTex.width != yWidth || remoteScreenYTex.height != yHeight) {
if (remoteScreenYTex) {
Destroy (remoteScreenYTex);
}
remoteScreenYTex = new Texture2D (yWidth, yHeight, TextureFormat.R8, false, true);
}
if (remoteScreenUVTex == null || remoteScreenUVTex.width != uvWidth || remoteScreenUVTex.height != uvHeight) {
if (remoteScreenUVTex) {
Destroy (remoteScreenUVTex);
}
remoteScreenUVTex = new Texture2D (uvWidth, uvHeight, TextureFormat.RG16, false, true);
}

bTexturesInitialized = true;
}

void UpdateCameraFrame(MessageEventArgs mea)
{
serializableUnityARCamera serCamera = mea.data.Deserialize<serializableUnityARCamera> ();

UnityARCamera scamera = new UnityARCamera ();
scamera = serCamera;

InitializeTextures (scamera);

UnityARSessionNativeInterface.SetStaticCamera (scamera);
UnityARSessionNativeInterface.RunFrameUpdateCallbacks ();
}

void AddFaceAnchor(MessageEventArgs mea)
{
serializableUnityARFaceAnchor serFaceAnchor = mea.data.Deserialize<serializableUnityARFaceAnchor> ();

ARFaceAnchor arFaceAnchor = serFaceAnchor;
UnityARSessionNativeInterface.RunAddAnchorCallbacks (arFaceAnchor);
}

void UpdateFaceAnchor(MessageEventArgs mea)
{
serializableUnityARFaceAnchor serFaceAnchor = mea.data.Deserialize<serializableUnityARFaceAnchor> ();

ARFaceAnchor arFaceAnchor = serFaceAnchor;
UnityARSessionNativeInterface.RunUpdateAnchorCallbacks (arFaceAnchor);
}

void RemoveFaceAnchor(MessageEventArgs mea)
{
serializableUnityARFaceAnchor serFaceAnchor = mea.data.Deserialize<serializableUnityARFaceAnchor> ();

ARFaceAnchor arFaceAnchor = serFaceAnchor;
UnityARSessionNativeInterface.RunRemoveAnchorCallbacks (arFaceAnchor);
}

void ReceiveRemoteScreenYTex(MessageEventArgs mea)
{
if (!bTexturesInitialized)
return;
remoteScreenYTex.LoadRawTextureData(CompressionHelper.ByteArrayDecompress(mea.data));
remoteScreenYTex.Apply ();
UnityARVideo arVideo = Camera.main.GetComponent<UnityARVideo>();
if (arVideo) {
arVideo.SetYTexure(remoteScreenYTex);
}

}

void ReceiveRemoteScreenUVTex(MessageEventArgs mea)
{
if (!bTexturesInitialized)
return;
remoteScreenUVTex.LoadRawTextureData(CompressionHelper.ByteArrayDecompress(mea.data));
remoteScreenUVTex.Apply ();
UnityARVideo arVideo = Camera.main.GetComponent<UnityARVideo>();
if (arVideo) {
arVideo.SetUVTexure(remoteScreenUVTex);
}

}


void SendInitToPlayer()
{

//we're going to reuse ARSessionConfiguration and only use its lightestimation field.

serializableFromEditorMessage sfem = new serializableFromEditorMessage ();
sfem.subMessageId = SubMessageIds.editorInitARKitFaceTracking;
serializableARSessionConfiguration ssc = new serializableARSessionConfiguration (UnityARAlignment.UnityARAlignmentCamera, UnityARPlaneDetection.None, false, enableLightEstimation, true);
UnityARSessionRunOption roTracking = resetTracking ? UnityARSessionRunOption.ARSessionRunOptionResetTracking : 0;
UnityARSessionRunOption roAnchors = removeExistingAnchors ? UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors : 0;
sfem.arkitConfigMsg = new serializableARKitInit (ssc, roTracking | roAnchors);
SendToPlayer (ConnectionMessageIds.fromEditorARKitSessionMsgId, sfem);
}

void SendToPlayer(System.Guid msgId, byte[] data)
{
editorConnection.Send (msgId, data);
}

public void SendToPlayer(System.Guid msgId, object serializableObject)
{
byte[] arrayToSend = serializableObject.SerializeToByteArray ();
SendToPlayer (msgId, arrayToSend);
}


// Update is called once per frame
void Update () {

}

}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using UnityEngine;
using UnityEngine.Networking.PlayerConnection;
using System.Text;
using Utils;
using UnityEngine.XR.iOS.Utils;

#if UNITY_EDITOR

Expand All @@ -16,6 +16,7 @@ public class ARKitRemoteConnection : MonoBehaviour
public UnityARPlaneDetection planeDetection = UnityARPlaneDetection.Horizontal;
public bool getPointCloud = true;
public bool enableLightEstimation = true;
public bool enableAutoFocus = true;

[Header("Run Options")]
public bool resetTracking = true;
Expand Down Expand Up @@ -159,7 +160,7 @@ void ReceiveRemoteScreenYTex(MessageEventArgs mea)
{
if (!bTexturesInitialized)
return;
remoteScreenYTex.LoadRawTextureData(mea.data);
remoteScreenYTex.LoadRawTextureData(CompressionHelper.ByteArrayDecompress(mea.data));
remoteScreenYTex.Apply ();
UnityARVideo arVideo = Camera.main.GetComponent<UnityARVideo>();
if (arVideo) {
Expand All @@ -172,7 +173,7 @@ void ReceiveRemoteScreenUVTex(MessageEventArgs mea)
{
if (!bTexturesInitialized)
return;
remoteScreenUVTex.LoadRawTextureData(mea.data);
remoteScreenUVTex.LoadRawTextureData(CompressionHelper.ByteArrayDecompress(mea.data));
remoteScreenUVTex.Apply ();
UnityARVideo arVideo = Camera.main.GetComponent<UnityARVideo>();
if (arVideo) {
Expand All @@ -186,7 +187,7 @@ void SendInitToPlayer()
{
serializableFromEditorMessage sfem = new serializableFromEditorMessage ();
sfem.subMessageId = SubMessageIds.editorInitARKit;
serializableARSessionConfiguration ssc = new serializableARSessionConfiguration (startAlignment, planeDetection, getPointCloud, enableLightEstimation);
serializableARSessionConfiguration ssc = new serializableARSessionConfiguration (startAlignment, planeDetection, getPointCloud, enableLightEstimation, enableAutoFocus);
UnityARSessionRunOption roTracking = resetTracking ? UnityARSessionRunOption.ARSessionRunOptionResetTracking : 0;
UnityARSessionRunOption roAnchors = removeExistingAnchors ? UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors : 0;
sfem.arkitConfigMsg = new serializableARKitInit (ssc, roTracking | roAnchors);
Expand Down
Loading