示例#1
0
 private void StopRecording()
 {
     avatarAnimator.FinishRecording();
     playingAnimationClip = recordedAnimationClip;
 }
示例#2
0
        void OnGUI()
        {
            InitUI();

            GUILayout.Label("Facial Motion Capture", titleStyle);
            GUILayout.Space(20);

            if (!AvatarMakerInitializer.IsPlatformSupported())
            {
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                GUILayout.Label("Avatar plugin supports only Windows platform and works in the Editor mode.");
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
                return;
            }

            if (AvatarMakerInitializer.IsInitializationInProgress)
            {
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                GUILayout.Label(EditorApplication.isPlaying ? "Exit play mode to load SDK" : "Loading...");
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
                return;
            }

            if (!AvatarMakerInitializer.IsMotionCaptureSupported)
            {
                EditorGUILayout.HelpBox("Your CPU doesn't have AVX extension required for Motion Tracking.", MessageType.Error);
                return;
            }

            if (avatarInfo == null)
            {
                if (isCapturing)
                {
                    avatarAnimator.StopCapturing();
                    avatarAnimator = null;
                    isCapturing    = false;
                }

                if (AnimationMode.InAnimationMode())
                {
                    ToggleAnimationMode();
                }

                EditorGUILayout.HelpBox(avatarErrorLabel, MessageType.Info);
                return;
            }

            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

            WebCamDevice[] cameraDevices = WebCamTexture.devices;
            if (cameraDevices != null && cameraDevices.Length > 0)
            {
                if (!isCapturing)
                {
                    if (cameraDevices.Length > 1)
                    {
                        string[] cameraNames = cameraDevices.Select(d => { return(d.name); }).ToArray();
                        cameraId = GUILayout.SelectionGrid(cameraId, cameraNames, 1, "toggle");
                    }
                    else
                    {
                        cameraId = 0;
                    }
                }

                EditorGUI.BeginDisabledGroup(avatarInfo == null);
                capturingButtonLabel = isCapturing ? "Stop capturing" : "Start capturing";
                isCapturing          = GUILayout.Toggle(isCapturing, capturingButtonLabel, "Button");
                EditorGUI.EndDisabledGroup();
            }
            else
            {
                EditorGUILayout.HelpBox("There is no available web camera.", MessageType.Info);
            }

            if (isCapturing)
            {
                capturingErrorLabel = string.Empty;
                if (avatarAnimator == null)
                {
                    avatarAnimator = new AvatarAnimator(avatarInfo.transform, avatarInfo.headMeshRenderer, cameraOffset);
                    isCapturing    = avatarAnimator.StartCapturing(WebCamTexture.devices[cameraId].name, avatarInfo.code);
                    if (!isCapturing)
                    {
                        capturingErrorLabel = "Unable to start motion capture.";
                        Debug.LogError(capturingErrorLabel);
                        avatarAnimator = null;
                        return;
                    }
                    ApplyBlendshapesImpact();

                    if (AvatarAnimator.RecordAtStart)
                    {
                        StartRecording();
                    }

                    if (AnimationMode.InAnimationMode())
                    {
                        ToggleAnimationMode();
                    }
                }
                Texture2D tex = avatarAnimator.HandleCapturedFrame();
                DisplayFrameTexture(tex);
            }
            else
            {
                if (avatarAnimator != null)
                {
                    avatarAnimator.StopCapturing();
                    avatarAnimator = null;
                }
            }

            if (!string.IsNullOrEmpty(capturingErrorLabel))
            {
                EditorGUILayout.HelpBox(capturingErrorLabel, MessageType.Error);
            }
            GUILayout.Space(20);

            EditorGUILayout.BeginVertical("Box");
            {
                EditorGUILayout.LabelField("Recording options", titleStyle);
                GUILayout.Space(5);
                if (isCapturing)
                {
                    recordingButtonLabel = avatarAnimator.IsRecording ? "Stop recording" : "Start recording";
                    if (avatarAnimator.IsRecording != GUILayout.Toggle(avatarAnimator.IsRecording, recordingButtonLabel, "Button"))
                    {
                        if (avatarAnimator.IsRecording)
                        {
                            avatarAnimator.FinishRecording();
                        }
                        else
                        {
                            StartRecording();
                        }
                    }
                    GUILayout.Space(5);
                }

                AvatarAnimator.RecordAtStart = GUILayout.Toggle(AvatarAnimator.RecordAtStart, "Record at start");

                animationClip = (AnimationClip)EditorGUILayout.ObjectField("Animation file: ", animationClip, typeof(AnimationClip), false);

                AvatarAnimator.ApplyTranslation = GUILayout.Toggle(AvatarAnimator.ApplyTranslation, "Capture translation");
                AvatarAnimator.ApplyRotation    = GUILayout.Toggle(AvatarAnimator.ApplyRotation, "Capture rotation");
            }
            EditorGUILayout.EndVertical();
            GUILayout.Space(10);

            if (animationClip != null && !isCapturing)
            {
                EditorGUILayout.BeginVertical("Box");
                {
                    EditorGUILayout.LabelField("Playback", titleStyle);
                    GUILayout.Space(5);

                    EditorGUI.BeginChangeCheck();
                    GUILayout.Toggle(AnimationMode.InAnimationMode(), "Play recorded animation");
                    if (EditorGUI.EndChangeCheck())
                    {
                        ToggleAnimationMode();
                    }

                    if (AnimationMode.InAnimationMode())
                    {
                        isAutoPlayAnimation = GUILayout.Toggle(isAutoPlayAnimation, "Automatically play in loop");

                        animationTime = EditorGUILayout.Slider(animationTime, 0.0f, animationClip.length);
                    }
                }
                EditorGUILayout.EndVertical();
                GUILayout.Space(10);
            }

            if (!string.IsNullOrEmpty(recordingErrorLabel))
            {
                EditorGUILayout.HelpBox(recordingErrorLabel, MessageType.Error);
                ShowAvatarMakerProLink();
            }
            GUILayout.Space(10);

            showBlendshapeCoefficients = EditorGUILayout.Foldout(showBlendshapeCoefficients, "Blendshapes Impact");
            if (showBlendshapeCoefficients)
            {
                if (blendshapesImpactControls.Count == 0)
                {
                    List <string> blendshapeNames = MotionCapturer.GetBlendshapesNames();
                    for (int i = 0; i < blendshapeNames.Count; i++)
                    {
                        blendshapesImpactControls.Add(blendshapeNames[i], new SliderWithTextValues(1.0f));
                        blendshapesImpactValues.Add(blendshapeNames[i], 1f);
                    }
                }

                blendshapesScrollPosition = GUILayout.BeginScrollView(blendshapesScrollPosition, GUILayout.Height(Mathf.Max(200f, position.height - 200f)));
                var blendshapesNames = blendshapesImpactControls.Keys.ToList <string>();
                for (int i = 0; i < blendshapesNames.Count; i++)
                {
                    SliderWithTextValues controlsValues = blendshapesImpactControls[blendshapesNames[i]];

                    GUILayout.BeginHorizontal();
                    GUILayout.Label(blendshapesNames[i] + ":", GUILayout.MaxWidth(100));
                    float blendshapeImpactVal = GUILayout.HorizontalSlider(controlsValues.sliderValue, 0, maxImpactValue);
                    if (blendshapeImpactVal != controlsValues.sliderValue)
                    {
                        controlsValues.SetValue(blendshapeImpactVal);
                        blendshapesImpactValues[blendshapesNames[i]] = blendshapeImpactVal;
                        ApplyBlendshapesImpact();
                    }

                    string modifiedValueStr = GUILayout.TextField(controlsValues.textValue, GUILayout.Width(100));
                    if (modifiedValueStr != blendshapeImpactVal.ToString())
                    {
                        controlsValues.textValue = modifiedValueStr;
                        if (float.TryParse(modifiedValueStr, out blendshapeImpactVal) && blendshapeImpactVal >= 0 && blendshapeImpactVal <= maxImpactValue)
                        {
                            controlsValues.sliderValue = blendshapeImpactVal;
                            blendshapesImpactValues[blendshapesNames[i]] = blendshapeImpactVal;
                            ApplyBlendshapesImpact();
                        }
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.Space(10);
                GUILayout.BeginHorizontal();
                if (GUILayout.Button("Save", buttonSkin))
                {
                    SaveBlendshapesImpactValues();
                }
                if (GUILayout.Button("Load", buttonSkin))
                {
                    LoadBlendshapesImpactValues();
                }
                GUILayout.EndHorizontal();
                GUILayout.Space(10);
                GUILayout.EndScrollView();
            }

            EditorGUILayout.EndScrollView();

            if (isCapturing)
            {
                Repaint();
            }
        }