Video Chat with Unity3D and AR Foundation — Chapter 3: Remote Assistant App
Tags: ar, unity3d, agora, videochat
Published: 2020-04-07 05:35:00

![ar-blog-cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/an7elid2258j1eg7hs4m.png)
 
# Video Chat with Unity3D and AR Foundation — Chapter 3: Remote Assistant App



Previously, we had shown two relatively simple sample applications on how to do video chat with AR Foundation on Unity. Here are the links to the blogs:

* [Chapter 1: Video Chat with Unity3D, the AR Foundation Version]([https://dev.to/icywind/video-chat-with-unity3d-the-arfoundation-version-51b8](https://dev.to/icywind/video-chat-with-unity3d-the-arfoundation-version-51b8))

* [Chapter 2: Video Chat with Unity3D and AR Foundation — ScreenSharing]([https://dev.to/icywind/video-chat-with-unity3d-and-ar-foundation-chapter-2-screensharing-m5i](https://dev.to/icywind/video-chat-with-unity3d-and-ar-foundation-chapter-2-screensharing-m5i))

Questions continue to come up:

*“How do I share AR objects from the sender’s AR camera?”*

*“Also, I don’t want to share my HUD UIs, how do I do that with screen sharing?”*

To address these technical challenges, we will make a more sophisticated application on the basis of knowing how to utilize AgoraIO’s Video SDK. Some of you may have already seen from [this blog about a Remote Assistant App on iOS](https://www.agora.io/en/blog/how-to-build-an-augmented-reality-remote-assistance-app). We will make a similar App on Unity3D with AR-Foundation.

## Prerequisites

* [Unity Editor](https://store.unity.com/) (Version 2019.2 or above)

* 2 devices to test on (one to broadcast, one to view)

* Broadcast device will be a mobile device to run AR scene: Apple device of iOS 11 or above; Android device with API level Android 7 or above.

* Viewing device does not need AR capability — pretty much any device with Windows, Mac, Android, or iOS operating systems will work

* A [developer account](https://sso.agora.io/en/signup) with Agora.io

## Getting Started

To start, we will need to integrate the Agora Video SDK for Unity3D into our project by searching for it in the Unity Asset Store or click this [link](https://assetstore.unity.com/packages/tools/video/agora-video-sdk-for-unity-134502) to begin the download. 

![Screen Shot 2021-04-30 at 11.26.17 AM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lkom1iae9ogukisigo4s.png)
 
*Figure 0. Video SDK on Asset Store*

This project should be compatible with latest SDK. In case this tutorial could not keep to date, you may fall back to the current SDK archive [here](https://apprtcio-my.sharepoint.com/:u:/g/personal/rick_agora_io/EfSQ7gv4JMtBhLPrfHBYsDgBxPM4gwy3iYQbj7ORX61utQ?e=xrqosx).


After you finish downloading and importing the SDK into your project, you should be able to see the README files for the different platforms the SDK supports. You should be familiar with the set up for the demo by following the previous tutorials.

## Unity AR Packages

On UnityEditor (2019), open Package Manager from the Window tab. Install the following packages:

* AR Foundation 3.0.1

* ARCore XR Plugin 3.0.1

* ARKit XR Plugin 3.0.1

If you use other versions of Unity Editor please check with the README file on GitHub for the verified setups. Or you may want to try different versions, since AR Foundation is changing a lot between versions.

## Project Set Up

Please follow this [link to download the completed project “RemoteAssistantAR” on Github](https://github.com/icywind/RemoteAssistantAR).

Open the project in Unity Editor. Go to the Unity Asset Store page to import Agora Video SDK. When importing the Agora Video SDK, unselect “demo” from the list since those file names are modified in the Github repo. Switch to iOS or Android platform and follow the README instruction on how to set up the build environment for AR Foundation.

Open up the Main scene, and your project should look like this:

![Figure 1. Main Scene](https://cdn-images-1.medium.com/max/3362/1*ai1cBEJZxFt7PxA9Ov2P_w.png)*Figure 1. Main Scene*

You should fill your App ID into the field of the GameController object on this screen. You may then build the project for iOS or Android to run on device. Just like the previous chapter, you will need a mobile device to run as the broadcaster and another mobile or desktop device (include Unity Editor) to run as the audience.

## Project Architecture

The RemoteAssistantAR project consists of three scenes. Here are their descriptions:

* **Main** — the entry screen with buttons to open the two screens for different purposes.

* **AudPlay** —audience client, in which the user sees the the remote client’s video stream on the main screen; the user can choose color and draw things on the screen.

* **CastAR** — the AR camera client, in which the user sees the real world on the back camera, mixed with AR objects; the user casts what he/she sees to the audience client.

This picture shows relationship among the scenes:

![Figure 2. Scenes](https://cdn-images-1.medium.com/max/2000/1*5Gz_a5uKktJFIGIYhWqbGA.png)*Figure 2. Scenes*

### Shared Interfaces

The AudPlay client and the CastAR client share the programming interface *IVideoChatClient*. As you can see from Figure 2, the two clients also share a common user interface in the prefab *ChatCanvasUI*, as depicted below:

![Figure 3. ChatCanvasUI Prefab](https://cdn-images-1.medium.com/max/2000/1*7-ibHlU01HbGC1hy-UmzBQ.png)*Figure 3. ChatCanvasUI Prefab*

### Color Controller

A color controller is the additional UI controller that the AudPlay client contains. User may tap the ink droplet and get a list of color to pick. The follow picture shows the construct of this prefab:

![Figure 4.1 Color Controller](https://cdn-images-1.medium.com/max/2490/1*14nfErUVM1kb9g4t-hZLYA.png)*Figure 4.1 Color Controller*

### BroadCast View Controller

The BroadCast View Controller (or “BroadcastVC”), in most sense, is the equivalent client to the Chapter 2’s “ARClient”. You may go back to read about how to use external video source to share the video frames to the receiver. However, in this controller, we capture the AR camera image in a different way. We are actually getting the raw image data from a *RenderTexture*, in this method:

{% gist https://gist.github.com/icywind/597d1b6bfbcb5036645a18ffbe04fbea %}

**RenderTexture** is the key to answer those questions at the beginning of this tutorial.

## An Approach to Share AR Screen

So far, we’ve discovered two ways to share screens. The first way, is to share the entire screen as if you are continuously making screen capture of your devices and send it. This is shown in the blog [“How to Broadcast Your Screen with Unity3D and Agora.io”](https://www.agora.io/en/blog/how-to-broadcast-your-screen-with-unity3d-and-agora). The second way was discussed in my previous tutorial. The view from the AR Camera was shared without AR objects. What we want for this application is in between — we want the AR camera and AR objects (called it “AR Screen”) but not the HUD/UI elements. There could be different ways to achieve this. One way is the use of **RenderTexture**. In summary, it will follow these steps:

* Create multiple cameras that uses different culling masks.

* Assign everything under the Canvas UI to the “UI” layer.

* Label/assign in code the AR Objects to another layer, e.g., “ARLayer” (anyhow, I picked the built-in “*Ignored Raycast*” for convenience, see Figure 4.2 and the DrawDot function in Figure 7.3).

* Assign “Target Texture” field in the AR Camera to a Rendered Texture.

* Assign the same Rendered Texture to another Camera (“Render Camera”) that will render the AR Objects.

* Create a Material to use the Render Texture.

* Create a Quad to use the Material from the last step.

* Create a camera (“View Camera”) to render the quad texture.

![Figure 4.2 Layer](https://cdn-images-1.medium.com/max/2000/1*-ixo4yuDA1FlMy08Yjkaeg.png)*Figure 4.2 Layer*

### Cameras

So far, there are three cameras being used in the CastAR scene. They are laid out in the following wireframe:

![Figure 5. Wireframe of the Cameras](https://cdn-images-1.medium.com/max/2000/1*PAr9Cf93nhmVAteGPb9H9A.png)*Figure 5. Wireframe of the Cameras*

**AR Camera**: Takes the actual physical back camera from a device as input, sets each frame as output to a RenderTexture, e.g., *CasterRenderTexture*. Figure 6.1 illustrates the important parameters for this camera:

![Figure 6.1 — AR Camera](https://cdn-images-1.medium.com/max/2000/1*XFEV4BmFllt2Thx9iURZpw.png)*Figure 6.1 — AR Camera*

**Render Camera: **Renders** **3D AR objects. One may ask, why can’t AR Camera be used for 3D object rendering? The reason being, is that the AR Camera is a physical camera. As we can see from the last tutorial, the 3D objects are not captured in this camera. Our Render Camera is placed on the same world position as the AR Camera. And its output is also being sent to the same RenderTexture — *CasterRenderTexture*. See figure 6.2:

![Figure 6.2 — Render Camera](https://cdn-images-1.medium.com/max/2000/1*gg9l-7v0bIR5OfvEvChYVw.png)*Figure 6.2 — Render Camera*

**View Camera**: Looks at the quad, which uses a material that “reads” from the shared RenderTexture *CasterRenderTexture*. This camera provides the actual “view” for the user using this device. Note that since both *AR Camera* and *Render Camera* are rendering to the RenderTexture, the image is not rendered to the screen itself. That’s why this *View Camera* came into place. Moreover, the positions of *View Camera* and *Quad* are separated from *AR Camera* and *Render Camera*, so that the *Quad* object is not taken into the *Render Camera*’s view.

From the Hierarchy view in Figure 6.2, we may also see that *View Camera *parents *Quad*, so *Quad* turns and changes position with the camera’s movement. Same parent-child relationship applies to *AR Camera* and both *View Camera *and *Render Camera*. Their view angles and positions are kept in-sync this way.

![Figure 6.3 — View Camera](https://cdn-images-1.medium.com/max/2000/1*S7vovQmKxuxXZ6tUqbjSvg.png)*Figure 6.3 — View Camera*

The follow diagram illustrates the relationships between the cameras and the client views:

![Figure 6.4 — Relationship Diagram](https://cdn-images-1.medium.com/max/2000/1*wKI8Bvndx4opcfgLL7iqkQ.png)*Figure 6.4 — Relationship Diagram*

Next we will discuss the details about the remote assistant Part.

## Remote Assistant

The idea about the remote assistant app, is that a field operator may be out to an area to work on something, but he needs some guidance on where the things to look at from a help elsewhere. Our *CastAR* client will be used the field operator in this scenario; and a help operator uses the *AudPlay* client to draw an outline of the important part on the screen. Technically, we will need to do this in the following steps:

1. Capture the finger motion on *AudPlay* client’s screen and record the touch points.

1. Send the touch points to *CastAR* client.

1. *CastAR* client receives the data points, and renders the outline in its 3D space.

### Data model

We will use a small sphere to represent a dot, and the user’s dragging motion will create a series of small colored spheres. We will establish the model for our remote drawing data:

{% gist https://gist.github.com/icywind/50ca2f71d8a71a14afa6e731bdbdf4bc %}

Unlike the iOS version of this App, we don’t need to worry about the offset of screen points for different devices. We will convert the points from the *Screen Space* to *ViewPort* *Space* for the transport. [This page](https://answers.unity.com/questions/168156/screen-vs-viewport-what-is-the-difference.html) will help you to refresh your memory about the different point systems in Unity. A point from screen touch is “normalized” in this line:

    Vector3 vp = Camera.main.ScreenToViewportPoint(screenPos)

We use a buffer list to accumulate the touch points to limit the number of the data stream call, which can be a bit of overhead adding to the performance measure.

### Data Stream

On the *AudPlay* client side, one of the initialization steps in *AudienceVC* involves the creation of the data stream, in the following line:

    dataStreamId = rtcEngine.CreateDataStream(reliable: true, ordered: true);

The *dataStreamId* is later used in the send stream message method as following:

{% gist https://gist.github.com/icywind/9dcdbb09b52b88d8afce63864d2b791e %}

On the *CastAR* client side, we dedicated a game object “DrawListener” to listen to the data stream and process the request.

![Figure 7.1 — DrawListener](https://cdn-images-1.medium.com/max/2000/1*CUTyNhcrXzVCcwBJ1q-8WQ.png)*Figure 7.1 — DrawListener*

The *DrawListener’s* controller script *RemoteDrawer* registers a callback to handle the data stream event:

    rtcEngine.OnStreamMessage += HandleStreamMessage;

The data passing between the two ends is in character string, which is a JSON formatted string. And we invoke this method to draw the dots in the *CastAR* client’s 3D world:

{% gist https://gist.github.com/icywind/f84b5faa3c60d8efdcaa3339920f6e17 %}

Here the *referenceObject* is the sphere. It shares the same parent of the container of the dots to be drawn. The *DeNormalizedPosition*() function on line 19 does the opposite of what we did previously in *Normalize*():

    camera.ScreenToWorldPoint(pos);

The important thing here, is to use the correct camera for the viewport space. Since the Render Camera is responsible to render the AR 3D objects, it is used for the conversion here.

## Conclusion

Linking all the information above should provide you a better understanding of the design and the technical implementation of the RemoteAssistantAR project in Unity.

Some known issues here:

* We skipped the “Undo” feature that exists in the iOS sample project, instead, we have the “Clear” button to clear all the drawing once.

* The drawn outline may not “stay” on the object if the camera keeps moving around. This is due to the hardcoded z-space for the drawing object. A possible fix may be using RayCast to determine where to place the Z position at.

* There is a lag after both devices joining a channel to start the video streaming.

* Video recording of the app (through iOS controls or desktop Quicktime Player) causes longer lags.

There are definitely plenty room of improvement here. And the multi camera solution for AR screen sharing is not the only valid approach. Please let me know if there is any suggestion or even better — make a Pull Request of your modification on Github will be very welcomed!
> *Other Resources*

* The complete API documentation is available in the [Document Center](https://docs.agora.io/en/).

* For technical support, submit a ticket using the [Agora Dashboard](https://dashboard-v1.agora.io/show-ticket-submission) or reach out directly to our Developer Relations team [devrel@agora.io](mailto:devrel@agora.io)

* Come join the Slack community: [https://agoraiodev.slack.com/messages/unity-help-me](https://agoraiodev.slack.com/messages/unity-help-me)