/// <summary> /// To make Getting Started sample as simple as possible all code required for creating and /// displaying an avatar is placed here in a single function. This function is also a good example of how to /// chain asynchronous requests, just like in traditional sequential code. /// </summary> protected virtual IEnumerator GenerateAndDisplayHeadRoutine(byte[] photoBytes, PipelineType pipeline, Action <AsyncRequest> callback = null) { if (avatarProvider == null) { Debug.LogError("avatarProvider null"); yield break; } // Choose all available parameters var parametersRequest = avatarProvider.GetParametersAsync(ComputationParametersSubset.ALL, pipeline); yield return(AwaitRoutine(parametersRequest)); ComputationParameters allParameters = parametersRequest.Result; ComputationParameters parameters = ComputationParameters.Empty; parameters.haircuts = allParameters.haircuts; parameters.blendshapes = allParameters.blendshapes; parameters.modelInfo = allParameters.modelInfo; parameters.modelInfo.hairColor.Value = true; parameters.modelInfo.predictHaircut.Value = true; // generate avatar from the photo and get its code in the Result of request var initializeRequest = avatarProvider.InitializeAvatarAsync(photoBytes, "name", "description", pipeline, parameters); yield return(AwaitRoutine(initializeRequest)); var avatarCode = initializeRequest.Result; var calculateRequest = avatarProvider.StartAndAwaitAvatarCalculationAsync(avatarCode); yield return(AwaitRoutine(calculateRequest)); string avatarName = "Avatar " + System.DateTime.Now.ToString("dd/MM/yy HH:mm:ss"); WriteAvatarNameByCode(avatarCode, avatarName); if (callback != null) { callback.Invoke(calculateRequest); } UpdateAvatarList(); if (!calculateRequest.IsError) { yield return(ShowAvatarByCode(avatarCode)); } else { Debug.LogErrorFormat("Unable to calculate avatar: {0}", avatarName); } }
/// <summary> /// Create avatar and save photo to disk. /// </summary> private IEnumerator CreateNewAvatar(byte[] photoBytes) { PipelineType pipeline = pipelineType; // Choose default set of resources to generate var resourcesRequest = avatarProvider.ResourceManager.GetResourcesAsync(AvatarResourcesSubset.DEFAULT, pipelineType); yield return(resourcesRequest); if (resourcesRequest.IsError) { yield break; } var initializeAvatar = avatarProvider.InitializeAvatarAsync(photoBytes, "name", "description", pipeline, resourcesRequest.Result); yield return(Await(initializeAvatar, null)); string avatarCode = initializeAvatar.Result; if (initializeAvatar.IsError) { UpdateAvatarState(avatarCode, GalleryAvatarState.FAILED); yield break; } yield return(UpdateAvatarList()); UpdateAvatarState(avatarCode, GalleryAvatarState.GENERATING); var calculateAvatar = avatarProvider.StartAndAwaitAvatarCalculationAsync(avatarCode); yield return(Await(calculateAvatar, avatarCode)); if (calculateAvatar.IsError) { UpdateAvatarState(avatarCode, GalleryAvatarState.FAILED); yield break; } var downloadAvatar = avatarProvider.MoveAvatarModelToLocalStorageAsync(avatarCode, pipeline == PipelineType.FACE, true); yield return(Await(downloadAvatar, avatarCode)); if (downloadAvatar.IsError) { UpdateAvatarState(avatarCode, GalleryAvatarState.FAILED); yield break; } UpdateAvatarState(avatarCode, GalleryAvatarState.COMPLETED); }
/// <summary> /// To make Getting Started sample as simple as possible all code required for creating and /// displaying an avatar is placed here in a single function. This function is also a good example of how to /// chain asynchronous requests, just like in traditional sequential code. /// </summary> protected virtual IEnumerator GenerateAndDisplayHead(byte[] photoBytes, PipelineType pipeline) { // Choose default set of resources to generate var resourcesRequest = avatarProvider.ResourceManager.GetResourcesAsync(AvatarResourcesSubset.DEFAULT, pipelineType); yield return(Await(resourcesRequest)); // generate avatar from the photo and get its code in the Result of request var initializeRequest = avatarProvider.InitializeAvatarAsync(photoBytes, "name", "description", pipeline, resourcesRequest.Result); yield return(Await(initializeRequest)); string avatarCode = initializeRequest.Result; StartCoroutine(SampleUtils.DisplayPhotoPreview(avatarCode, photoPreview)); var calculateRequest = avatarProvider.StartAndAwaitAvatarCalculationAsync(avatarCode); yield return(Await(calculateRequest)); // with known avatar code we can get TexturedMesh for head in order to show it further var avatarHeadRequest = avatarProvider.GetHeadMeshAsync(avatarCode, false); yield return(Await(avatarHeadRequest)); TexturedMesh headTexturedMesh = avatarHeadRequest.Result; TexturedMesh haircutTexturedMesh = null; // get identities of all haircuts available for the generated avatar var haircutsIdRequest = avatarProvider.GetHaircutsIdAsync(avatarCode); yield return(Await(haircutsIdRequest)); // randomly select a haircut var haircuts = haircutsIdRequest.Result; if (haircuts != null && haircuts.Length > 0) { var haircutIdx = UnityEngine.Random.Range(0, haircuts.Length); var haircut = haircuts[haircutIdx]; // load TexturedMesh for the chosen haircut var haircutRequest = avatarProvider.GetHaircutMeshAsync(avatarCode, haircut); yield return(Await(haircutRequest)); haircutTexturedMesh = haircutRequest.Result; } DisplayHead(headTexturedMesh, haircutTexturedMesh); }
/// <summary> /// To make Getting Started sample as simple as possible all code required for creating and /// displaying an avatar is placed here in a single function. This function is also a good example of how to /// chain asynchronous requests, just like in traditional sequential code. /// </summary> protected virtual IEnumerator GenerateAndDisplayHeadRoutine(byte[] photoBytes, PipelineType pipeline, Action <AsyncRequest> callback = null) { if (avatarProvider == null) { Debug.LogError("avatarProvider null"); yield break; } // Choose default set of resources to generate var resourcesRequest = avatarProvider.ResourceManager.GetResourcesAsync(AvatarResourcesSubset.ALL, pipeline); yield return(AwaitRoutine(resourcesRequest)); // generate avatar from the photo and get its code in the Result of request var initializeRequest = avatarProvider.InitializeAvatarAsync(photoBytes, "name", "description", pipeline, resourcesRequest.Result); yield return(AwaitRoutine(initializeRequest)); var avatarCode = initializeRequest.Result; //EditorCoroutine.start(SampleUtils.DisplayPhotoPreview(avatarCode, photoPreview)); var calculateRequest = avatarProvider.StartAndAwaitAvatarCalculationAsync(avatarCode); yield return(AwaitRoutine(calculateRequest)); string avatarName = "Avatar " + System.DateTime.Now.ToString("dd/MM/yy HH:mm:ss"); //(loadedAvatars.Count+1); //SystemInfo.deviceName + " "+ System.DateTime.Now.ToString("dd/MM/yy HH:mm:ss"); WriteAvatarNameByCode(avatarCode, avatarName); if (callback != null) { callback.Invoke(calculateRequest); } UpdateAvatarList(); if (!calculateRequest.IsError) { yield return(ShowAvatarByCode(avatarCode)); } else { Debug.LogErrorFormat("Unable to calculate avatar: {0}", avatarName); } }