Beispiel #1
0
        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>
        /// 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>
        /// Show avatar in the scene. Also load haircut information to allow further haircut change.
        /// </summary>
        private IEnumerator ShowAvatar(string avatarCode)
        {
            ChangeControlsInteractability(false);
            yield return(new WaitForSeconds(0.05f));

            StartCoroutine(SampleUtils.DisplayPhotoPreview(avatarCode, photoPreview));

            progressText.text = string.Empty;
            currentHaircut    = 0;

            var currentAvatar = GameObject.Find(AVATAR_OBJECT_NAME);

            if (currentAvatar != null)
            {
                Destroy(currentAvatar);
            }

            var avatarObject = new GameObject(AVATAR_OBJECT_NAME);

            var headMeshRequest = avatarProvider.GetHeadMeshAsync(avatarCode, true);

            yield return(Await(headMeshRequest));

            if (headMeshRequest.IsError)
            {
                Debug.LogError("Could not load avatar from disk!");
            }
            else
            {
                TexturedMesh texturedMesh = headMeshRequest.Result;

                // game object can be deleted if we opened another avatar
                if (avatarObject != null && avatarObject.activeSelf)
                {
                    avatarObject.AddComponent <RotateByMouse>();

                    headObject = new GameObject(HEAD_OBJECT_NAME);
                    var meshRenderer = headObject.AddComponent <SkinnedMeshRenderer>();
                    meshRenderer.sharedMesh           = texturedMesh.mesh;
                    meshRenderer.material             = IsUnlitMode ? unlitMaterial : litMaterial;
                    meshRenderer.material.mainTexture = texturedMesh.texture;
                    headObject.transform.SetParent(avatarObject.transform);
                    SetAvatarScale(avatarCode, avatarObject.transform);

                    if (useAnimations)
                    {
                        if (isWebGLDemo)
                        {
                            // don't show all animations in webgl demo
                            GetComponent <AnimationManager>().CreateAnimator(headObject, animations.GetRange(0, 5));
                        }
                        else
                        {
                            GetComponent <AnimationManager>().CreateAnimator(headObject, animations);
                        }
                    }
                    else
                    {
                        //add an empty blendshape with index -1
                        availableBlendshapes.Add(-1, "None");

                        var mesh = headObject.GetComponentInChildren <SkinnedMeshRenderer>().sharedMesh;
                        for (int i = 0; i < mesh.blendShapeCount; i++)
                        {
                            availableBlendshapes.Add(i, mesh.GetBlendShapeName(i));
                        }
                        ChangeCurrentBlendshape(-1);
                        blendshapesSelectingView.InitItems(availableBlendshapes.Values.ToList());

                        if (availableBlendshapes.Count == 1)
                        {
                            blendshapesPanel.SetActive(false);
                        }
                    }
                }
            }

            var haircutsIdsRequest = GetHaircutsIdsAsync(avatarCode);

            yield return(haircutsIdsRequest);

            string[] haircuts = haircutsIdsRequest.Result;
            if (haircuts != null && haircuts.Length > 0)
            {
                //Add fake "bald" haircut
                var haircutsList = haircuts.ToList();
                haircutsList.Insert(0, BALD_HAIRCUT_NAME);
                avatarHaircuts = haircutsList.ToArray();
                OnDisplayedHaircutChanged(avatarHaircuts[currentHaircut]);
                haircutsSelectingView.InitItems(avatarCode, avatarHaircuts.ToList(), avatarProvider);
                haircutsPanel.SetActive(true);
            }
            else
            {
                haircutsPanel.SetActive(false);
                OnDisplayedHaircutChanged(BALD_HAIRCUT_NAME);
            }

            ChangeControlsInteractability(true);
            avatarControls.SetActive(true);
        }
Beispiel #5
0
        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;
        }