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)
{
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 = MaterialAdjuster.GetHeadMaterial(avatarCode, headMesh.texture, AvatarShaderType.UnlitShader);
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;
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.
Implementation
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 getting started instructions on the Main Page.
Location: Assets/itseez3d/avatar_sdk/samples_cloud/01_getting_started_sample_cloud/scenes/01_getting_started_sample_cloud.unity.