Avatar SDK  2.2.1
Realistic avatar generation toolset for 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, downloading, loading from the local storage.
In the case of the Cloud SDK it is CloudAvatarProvider. If you need more specific methods to interact with the Cloud API, use the Connection object which is the member of the CloudAvatarProvider.

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.FACE;
IAvatarProvider avatarProvider = AvatarSdkMgr.GetAvatarProvider();
// You can cast the IAvatarProvider to the CloudAvatarProvider if it is required
//CloudAvatarProvider cloudAvatarProvider = (AvatarSdkMgr.GetAvatarProvider() as CloudAvatarProvider);
// 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;
// Download avatar data and store it in local drive
var downloadRequest = avatarProvider.MoveAvatarModelToLocalStorageAsync(avatarCode, true, false);
yield return downloadRequest;
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:

  • Animated Face. The bald head is displayed with the attached random haircut.
  • Head 1.2. A bust with an inseparable haircut.
  • 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.