private IEnumerator ShowAvatarByCode(string sCode) { // with known avatar code we can get TexturedMesh for head in order to show it further var avatarHeadRequest = avatarProvider.GetHeadMeshAsync(sCode, true); yield return(AwaitRoutine(avatarHeadRequest)); TexturedMesh headTexturedMesh = avatarHeadRequest.Result; TexturedMesh haircutTexturedMesh = null; // get identities of all haircuts available for the generated avatar var haircutsIdRequest = avatarProvider.GetHaircutsIdAsync(sCode); yield return(AwaitRoutine(haircutsIdRequest)); // randomly select a haircut var haircuts = haircutsIdRequest.Result; var haircutIdx = 0; if (haircuts != null && haircuts.Length > 0) { haircutIdx = UnityEngine.Random.Range(0, haircuts.Length); var haircut = haircuts[haircutIdx]; // load TexturedMesh for the chosen haircut var haircutRequest = avatarProvider.GetHaircutMeshAsync(sCode, haircut); yield return(AwaitRoutine(haircutRequest)); haircutTexturedMesh = haircutRequest.Result; } var avatarInfo = CreateHead(sCode, headTexturedMesh, haircutTexturedMesh); avatarInfo.code = sCode; avatarInfo.name = ReadAvatarNameByCode(sCode); avatarInfo.haircuts = haircuts; avatarInfo.selectedHairstyle = haircutIdx; avatarInfo.transform.position = Vector3.zero; avatarInfo.transform.localRotation = avatarInitialRotation; Selection.activeGameObject = avatarInfo.gameObject; SceneView sceneView = SceneView.lastActiveSceneView; if (sceneView == null) { Type GameViewType = System.Type.GetType("UnityEditor.GameView,UnityEditor"); sceneView = EditorWindow.GetWindow <SceneView>(new Type[] { GameViewType }); } if (sceneView != null) { Camera sceneCam = sceneView.camera; sceneView.pivot = Vector3.zero; sceneView.size = cameraOffset; sceneView.LookAt(Vector3.zero, Quaternion.identity); } Tools.current = Tool.Rotate; }
private IEnumerator DownloadAvatar(BodyAttachment bodyAttachment, string avatarCode, string haircutIdentity, string hairColor) { // download head var headRequest = avatarProvider.GetHeadMeshAsync(avatarCode, true); yield return(Await(headRequest)); TexturedMesh headTexturedMesh = headRequest.Result; // download haircut TexturedMesh haircutTexturedMesh = null; if (haircutIdentity != null) { var haircutRequest = avatarProvider.GetHaircutMeshAsync(avatarCode, haircutIdentity); yield return(Await(haircutRequest)); haircutTexturedMesh = haircutRequest.Result; } // display head DisplayHead(bodyAttachment, headTexturedMesh, haircutTexturedMesh, hairColor); // finish! FinishAvatar(); }
/// <summary> /// Actually load the haircut model and texture and display it in the scene (aligned with the head). /// </summary> /// <param name="newIdx">Index of the haircut.</param> private IEnumerator ChangeHaircutFunc(int newIdx) { if (newIdx < 0 || newIdx >= avatarHaircuts.Length) { yield break; } currentHaircut = newIdx; string haircutName = avatarHaircuts[currentHaircut]; var haircutObj = GameObject.Find(HAIRCUT_OBJECT_NAME); // bald head is just absence of haircut if (string.Compare(haircutName, BALD_HAIRCUT_NAME) == 0) { Destroy(haircutObj); yield break; } var haircurtMeshRequest = avatarProvider.GetHaircutMeshAsync(currentAvatarCode, haircutName); yield return(Await(haircurtMeshRequest)); if (haircurtMeshRequest.IsError) { yield break; } Destroy(haircutObj); var texturedMesh = haircurtMeshRequest.Result; var meshObject = new GameObject(HAIRCUT_OBJECT_NAME); meshObject.AddComponent <MeshFilter>().mesh = texturedMesh.mesh; var meshRenderer = meshObject.AddComponent <MeshRenderer>(); meshRenderer.material = IsUnlitMode ? haircutUnlitMaterial : haircutLitMaterial; meshRenderer.material.mainTexture = texturedMesh.texture; // Some haircuts looks better with enabled culling if (haircutName.Contains("_NewSea_")) { meshRenderer.material.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Back); } // ensure that haircut is rotated just like the head var avatarObject = GameObject.Find(AVATAR_OBJECT_NAME); if (avatarObject != null) { meshObject.transform.SetParent(avatarObject.transform); meshObject.transform.localRotation = Quaternion.identity; meshObject.transform.localScale = Vector3.one; } yield return(null); // only after the next frame the textures and materials are actually updated in the scene }
/// <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); }
private IEnumerator ShowAvatarByCode(string avatarCode) { // with known avatar code we can get TexturedMesh for head in order to show it further var avatarHeadRequest = avatarProvider.GetHeadMeshAsync(avatarCode, true); yield return(AwaitRoutine(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(AwaitRoutine(haircutsIdRequest)); ModelInfo modelInfo = CoreTools.GetAvatarModelInfo(avatarCode); // select predicted haircut var haircuts = haircutsIdRequest.Result.ToList(); var haircutIdx = 0; if (haircuts != null && haircuts.Count > 0 && !string.IsNullOrEmpty(modelInfo.haircut_name)) { haircutIdx = haircuts.FindIndex(h => h.Contains(modelInfo.haircut_name)); if (haircutIdx >= 0) { // load TexturedMesh for the chosen haircut var haircutRequest = avatarProvider.GetHaircutMeshAsync(avatarCode, haircuts[haircutIdx]); yield return(AwaitRoutine(haircutRequest)); haircutTexturedMesh = haircutRequest.Result; } } var avatarInfo = CreateHead(avatarCode, headTexturedMesh, haircutTexturedMesh, modelInfo); avatarInfo.code = avatarCode; avatarInfo.name = ReadAvatarNameByCode(avatarCode); avatarInfo.haircuts = haircutsIdRequest.Result; avatarInfo.selectedHairstyle = haircutIdx; avatarInfo.transform.position = Vector3.zero; avatarInfo.transform.localRotation = Quaternion.identity; Selection.activeGameObject = avatarInfo.gameObject; SceneView sceneView = SceneView.lastActiveSceneView; if (sceneView == null) { Type GameViewType = System.Type.GetType("UnityEditor.GameView,UnityEditor"); sceneView = EditorWindow.GetWindow <SceneView>(new Type[] { GameViewType }); } if (sceneView != null) { Camera sceneCam = sceneView.camera; sceneView.pivot = Vector3.zero; sceneView.size = cameraOffset; sceneView.LookAt(Vector3.zero, Quaternion.identity); } Tools.current = Tool.Rotate; }