Avatar SDK Local Compute Unity plugin  2.2.2
Toolset for Head 2.0 avatars generation in Unity3D
Scene #01: Getting Started Sample

This is the simplest scene designed to showcase the most basic functionality of the SDK: generation of the 3D model from a photo of a person and showing the generated model on the scene.

Code usage

Before executing any SDK methods you should initialize AvatarSdkMgr.

After that, you can get an object that implements IAvatarProvider interface. This object should be initialized before usage.
IAvatarProvider provides all common methods to work with avatars: generation, loading from the local storage.
In the case of the Local Compute SDK it is LocalComputeAvatarProvider.

IEnumerator InitializeSdk()
{
if (!AvatarSdkMgr.IsInitialized)
AvatarSdkMgr.Init();
IAvatarProvider avatarProvider = AvatarSdkMgr.GetAvatarProvider();
if (!avatarProvider.IsInitialized)
yield return avatarProvider.InitializeAsync();
}
IEnumerator DisplayHead(string avatarCode)
{
// Get head mesh from the local drive. If some files are missed, they will be downloaded from the cloud.
var avatarHeadRequest = avatarProvider.GetHeadMeshAsync(avatarCode, false);
yield return avatarHeadRequest;
TexturedMesh headMesh = avatarHeadRequest.Result;
// create parent avatar object in a scene, attach a script to it to allow rotation by mouse
var avatarObject = new GameObject(AVATAR_OBJECT_NAME);
// create head object in the scene
var headObject = new GameObject(HEAD_OBJECT_NAME);
var headMeshRenderer = headObject.AddComponent<SkinnedMeshRenderer>();
headMeshRenderer.sharedMesh = headMesh.mesh;
var headMaterial = new Material(ShadersUtils.GetHeadShader(false));
headMaterial.mainTexture = headMesh.texture;
headMeshRenderer.material = headMaterial;
headObject.transform.SetParent(avatarObject.transform);
}
IEnumerator GenerateAvatar(byte[] photoBytes)
{
// SDK initialization should be performed only once
yield return InitializeSdk();
PipelineType pipeline = PipelineType.HEAD_2_0_HEAD_MOBILE;
IAvatarProvider avatarProvider = AvatarSdkMgr.GetAvatarProvider();
// Initialize avatar by sendng photo to cloud and get avatar code
var initializeRequest = avatarProvider.InitializeAvatarAsync(photoBytes, "name", "description", pipeline, ComputationParameters.Empty);
yield return initializeRequest;
string avatarCode = initializeRequest.Result;
// Wait till the avatar is calculated
var calculateRequest = avatarProvider.StartAndAwaitAvatarCalculationAsync(avatarCode);
yield return calculateRequest;
yield return DisplayHead(avatarCode);
}

User interface on the scene

Buttons for avatar generation:

  • Press on the button Random Photo to create an avatar from one of the predefined photos embedded into the SDK. The SDK should generate the 3D model in the scene which you can rotate with your mouse.
  • Press on the Camera Photo button to take a photo from the web camera or from the default camera app on mobile devices.
  • Press the User Photo button to open file dialog and upload your own photo. This button works only in the editor because Unity does provide a file browser only in the editor.

Drop-down list with available pipelines:

  • Head 2.0 | head/mobile. A head with the detachable generated haircut.
  • Head 2.0 | bust/mobile. A bust with the detachable generated haircut.

Inner workings

The implementation of this scene is concentrated in one file for simplicity: GettingStartedSample.cs which is a MonoBehaviour and is attached to the SampleSceneHandler game objects.

See also FAQ and the getting started instructions on the main page: Main Page.