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)
{
var avatarHeadRequest = avatarProvider.GetHeadMeshAsync(avatarCode, false);
yield return avatarHeadRequest;
TexturedMesh headMesh = avatarHeadRequest.Result;
var avatarObject = new GameObject(AVATAR_OBJECT_NAME);
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)
{
yield return InitializeSdk();
IAvatarProvider avatarProvider = AvatarSdkMgr.GetAvatarProvider();
var initializeRequest = avatarProvider.InitializeAvatarAsync(photoBytes, "name", "description", pipeline, ComputationParameters.Empty);
yield return initializeRequest;
string avatarCode = initializeRequest.Result;
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.