Exemplo n.º 1
0
 public void SetValue(BlendShapeKey key, float value, bool apply)
 {
     if (m_merger != null)
     {
         m_merger.SetValue(key, value, apply);
     }
 }
        public void SelectGUI()
        {
            if (m_avatar != null && m_avatar.Clips != null)
            {
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Select BlendShapeClip", EditorStyles.boldLabel);
                var array = m_avatar.Clips
                            .Select(x => x != null
                        ? BlendShapeKey.CreateFromClip(x).ToString()
                        : "null"
                                    ).ToArray();
                SelectedIndex = GUILayout.SelectionGrid(SelectedIndex, array, 4);
            }

            if (GUILayout.Button("Create BlendShapeClip"))
            {
                var dir  = Path.GetDirectoryName(AssetDatabase.GetAssetPath(m_avatar));
                var path = EditorUtility.SaveFilePanel(
                    "Create BlendShapeClip",
                    dir,
                    string.Format("BlendShapeClip#{0}.asset", m_avatar.Clips.Count),
                    "asset");
                if (!string.IsNullOrEmpty(path))
                {
                    var clip = BlendShapeAvatar.CreateBlendShapeClip(path.ToUnityRelativePath());
                    //clip.Prefab = AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GetAssetPath(target));

                    m_avatar.Clips.Add(clip);

                    // save clips
                    EditorUtility.SetDirty(m_avatar);
                }
            }
        }
Exemplo n.º 3
0
        public void KeyTest()
        {
            var key = CreateBlendShapeKey("Blink", BlendShapePreset.Blink);

            Assert.AreEqual(key, CreateBlendShapeKey("Blink", BlendShapePreset.Blink));
            Assert.AreEqual(key, BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink));
            Assert.AreEqual(key, CreateBlendShapeKey("xxx", BlendShapePreset.Blink));

            var dict = new Dictionary <BlendShapeKey, float>();

            dict[key] = 1.0f;

            Assert.IsTrue(dict.ContainsKey(CreateBlendShapeKey("Blink", BlendShapePreset.Blink)));
            Assert.IsTrue(dict.ContainsKey(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink)));
            Assert.IsTrue(dict.ContainsKey(CreateBlendShapeKey("xxx", BlendShapePreset.Blink)));

            dict.Clear();

            var key2 = BlendShapeKey.CreateUnknown("Blink"); // name: Blink, Preset: Unknown

            dict[key2] = 1.0f;

            Assert.AreEqual(key2, CreateBlendShapeKey("Blink", BlendShapePreset.Unknown));
            Assert.AreNotEqual(key2, BlendShapeKey.CreateUnknown("blink"));
            Assert.AreNotEqual(key2, CreateBlendShapeKey("Blink", BlendShapePreset.Blink));
            Assert.AreNotEqual(key2, BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink));

            Assert.IsFalse(dict.ContainsKey(BlendShapeKey.CreateUnknown("blink")));
            Assert.IsFalse(dict.ContainsKey(CreateBlendShapeKey("Blink", BlendShapePreset.Blink)));
            Assert.IsFalse(dict.ContainsKey(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink)));
        }
Exemplo n.º 4
0
 /// <summary>
 /// Set a blendShape value immediate
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 public void SetValue(BlendShapeKey key, float value)
 {
     if (m_merger != null)
     {
         m_merger.SetValue(key, value, true);
     }
 }
Exemplo n.º 5
0
        void Restore()
        {
            var assetPath = UnityPath.FromAsset(this);

            if (assetPath.IsNull)
            {
                return;
            }


            foreach (var x in assetPath.Parent.ChildFiles)
            {
                var clip = UnityEditor.AssetDatabase.LoadAssetAtPath <BlendShapeClip>(x.Value);
                if (clip == null)
                {
                    continue;
                }

                if (!Clips.Contains(clip))
                {
                    Clips.Add(clip);
                }

                Debug.LogFormat("{0}", clip.name);
            }
            Clips = Clips.OrderBy(x => BlendShapeKey.CreateFrom(x)).ToList();
        }
Exemplo n.º 6
0
 /// <summary>
 /// AccumulateValue. After, Should call Apply
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 public void AccumulateValue(BlendShapeKey key, float value)
 {
     if (m_merger != null)
     {
         m_merger.AccumulateValue(key, value);
     }
 }
Exemplo n.º 7
0
 public void SetValue(BlendShapeKey key, float value, bool apply = true)
 {
     if (m_merger != null)
     {
         m_merger.SetValue(key, value, apply, m_materialMap);
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Immediately SetValue
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 public void ImmediatelySetValue(BlendShapeKey key, float value)
 {
     if (m_merger != null)
     {
         m_merger.ImmediatelySetValue(key, value);
     }
 }
Exemplo n.º 9
0
 public BlendShapeMerger(IEnumerable <BlendShapeClip> clips, Transform root)
 {
     m_clipMap  = clips.ToDictionary(x => BlendShapeKey.CreateFrom(x), x => x);
     m_valueMap = new Dictionary <BlendShapeKey, float>();
     foreach (var kv in m_clipMap)
     {
         foreach (var binding in kv.Value.Values)
         {
             if (!m_setterMap.ContainsKey(binding))
             {
                 var _target = root.Find(binding.RelativePath);
                 SkinnedMeshRenderer target = null;
                 if (_target != null)
                 {
                     target = _target.GetComponent <SkinnedMeshRenderer>();
                 }
                 if (target != null)
                 {
                     m_setterMap.Add(binding, new BlendShapePathHandler
                     {
                         Setter = x =>
                         {
                             target.SetBlendShapeWeight(binding.Index, x);
                         }
                     });
                 }
                 else
                 {
                     Debug.LogWarningFormat("{0} not found", binding.RelativePath);
                 }
             }
         }
     }
 }
Exemplo n.º 10
0
 public float GetValue(BlendShapeKey key)
 {
     if (m_merger == null)
     {
         return(0);
     }
     return(m_merger.GetValue(key));
 }
Exemplo n.º 11
0
 public BlendShapeClip GetClip(BlendShapeKey key)
 {
     if (Clips == null)
     {
         return(null);
     }
     return(Clips.FirstOrDefault(x => key.Match(x)));
 }
        public void DuplicateWarn()
        {
            var key = BlendShapeKey.CreateFromClip(Selected);

            if (m_avatar.Clips.Where(x => key.Match(x)).Count() > 1)
            {
                EditorGUILayout.HelpBox("duplicate clip: " + key, MessageType.Error);
            }
        }
Exemplo n.º 13
0
        public BlendShapeMerger(IEnumerable <BlendShapeClip> clips, Transform root)
        {
            m_clipMap = clips.ToDictionary(x => BlendShapeKey.CreateFromClip(x), x => x);

            m_valueMap = new Dictionary <BlendShapeKey, float>();

            m_blendShapeBindingMerger    = new BlendShapeBindingMerger(m_clipMap, root);
            m_materialValueBindingMerger = new MaterialValueBindingMerger(m_clipMap, root);
        }
Exemplo n.º 14
0
        public void SetValue(BlendShapeKey key, float value, bool replace)
        {
            m_valueMap[key] = value;

            BlendShapeClip clip;

            if (!m_clipMap.TryGetValue(key, out clip))
            {
                return;
            }

            foreach (var binding in clip.Values)
            {
                AccumulatingSetter handler;
                if (m_setterMap.TryGetValue(binding, out handler))
                {
                    if (replace)
                    {
                        // 値置き換え
                        handler.Apply(binding.Weight * value);
                    }
                    else
                    {
                        // 積算
                        handler.AddValue(binding.Weight * value);
                    }
                }
                else
                {
                    Debug.LogWarningFormat("'{0}' not found", binding);
                }
            }

            // materialの更新
            foreach (var binding in clip.MaterialValues)
            {
                AccumulatingSetter handler;
                if (m_materialSetterMap.TryGetValue(binding, out handler))
                {
                    if (replace)
                    {
                        // 値置き換え
                        handler.Apply(value);
                    }
                    else
                    {
                        // 積算
                        handler.AddValue(value);
                    }
                }
                else
                {
                    Debug.LogWarningFormat("'{0}' not found", binding);
                }
            }
        }
Exemplo n.º 15
0
        public float GetValue(BlendShapeKey key)
        {
            float value;

            if (!m_valueMap.TryGetValue(key, out value))
            {
                return(0);
            }
            return(value);
        }
Exemplo n.º 16
0
 private void SetupSliders()
 {
     if (BlendShapeAvatar != null && BlendShapeAvatar.Clips != null)
     {
         m_sliders = BlendShapeAvatar.Clips
                     .Where(x => x != null)
                     .Select(x => new BlendShapeSlider(this, BlendShapeKey.CreateFrom(x)))
                     .ToList()
         ;
     }
 }
Exemplo n.º 17
0
 public static void SetValue(this VRMBlendShapeProxy proxy, BlendShapeKey key, float value, bool apply)
 {
     if (apply)
     {
         proxy.ImmediatelySetValue(key, value);
     }
     else
     {
         proxy.AccumulateValue(key, value);
     }
 }
Exemplo n.º 18
0
 public IEnumerable <KeyValuePair <BlendShapeKey, float> > GetValues()
 {
     if (m_merger != null && BlendShapeAvatar != null)
     {
         foreach (var clip in BlendShapeAvatar.Clips)
         {
             var key = BlendShapeKey.CreateFrom(clip);
             yield return(new KeyValuePair <BlendShapeKey, float>(key, m_merger.GetValue(key)));
         }
     }
 }
Exemplo n.º 19
0
 public static void SetValue(this VRMBlendShapeProxy proxy, String key, float value, bool apply)
 {
     if (apply)
     {
         proxy.ImmediatelySetValue(BlendShapeKey.CreateUnknown(key), value);
     }
     else
     {
         proxy.AccumulateValue(BlendShapeKey.CreateUnknown(key), value);
     }
 }
Exemplo n.º 20
0
 public void SetValue(BlendShapeKey key, float value, bool immediately)
 {
     if (immediately)
     {
         ImmediatelySetValue(key, value);
     }
     else
     {
         AccumulateValue(key, value);
     }
 }
Exemplo n.º 21
0
 void OnEnable()
 {
     m_target = (VRMBlendShapeProxy)target;
     if (m_target.BlendShapeAvatar != null && m_target.BlendShapeAvatar.Clips != null)
     {
         m_sliders = m_target.BlendShapeAvatar.Clips
                     .Where(x => x != null)
                     .Select(x => new BlendShapeSlider(m_target, BlendShapeKey.CreateFrom(x)))
                     .ToList()
         ;
     }
 }
Exemplo n.º 22
0
        IEnumerator BlinkRoutine()
        {
            while (true)
            {
                var waitTime = Time.time + Random.value * Interval;
                while (waitTime > Time.time)
                {
                    if (Request)
                    {
                        m_request = false;
                        break;
                    }
                    yield return(null);
                }

                // close
                var value      = 0.0f;
                var closeSpeed = 1.0f / CloseSeconds;
                while (true)
                {
                    value += Time.deltaTime * closeSpeed;
                    if (value >= 1.0f)
                    {
                        break;
                    }

                    m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), value);
                    yield return(null);
                }
                m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), 1.0f);

                // wait...
                yield return(new WaitForSeconds(ClosingTime));

                // open
                value = 1.0f;
                var openSpeed = 1.0f / OpeningSeconds;
                while (true)
                {
                    value -= Time.deltaTime * openSpeed;
                    if (value < 0)
                    {
                        break;
                    }

                    m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), value);
                    yield return(null);
                }
                m_blendShapes.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), 0);
            }
        }
        public void KeyTest()
        {
            var key = new BlendShapeKey("Blink", BlendShapePreset.Blink);

            Assert.AreEqual(key, new BlendShapeKey("blink"));
            Assert.AreEqual(key, new BlendShapeKey(BlendShapePreset.Blink));
            Assert.AreEqual(key, new BlendShapeKey("xxx", BlendShapePreset.Blink));

            var dict = new Dictionary <BlendShapeKey, float>();

            dict[new BlendShapeKey("xxx", BlendShapePreset.Blink)] = 1.0f;

            Assert.IsTrue(dict.ContainsKey(new BlendShapeKey("blink")));
            Assert.IsTrue(dict.ContainsKey(new BlendShapeKey(BlendShapePreset.Blink)));
            Assert.IsTrue(dict.ContainsKey(new BlendShapeKey("xxx", BlendShapePreset.Blink)));
        }
Exemplo n.º 24
0
        /// <summary>
        /// 即時に反映しない。後にApplyによって反映する
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AccumulateValue(BlendShapeKey key, float value)
        {
            m_valueMap[key] = value;

            BlendShapeClip clip;

            if (!m_clipMap.TryGetValue(key, out clip))
            {
                return;
            }

            if (clip.IsBinary)
            {
                value = Mathf.Round(value);
            }

            m_blendShapeBindingMerger.AccumulateValue(clip, value);
            m_materialValueBindingMerger.AccumulateValue(clip, value);
        }
Exemplo n.º 25
0
        public void SetClip(BlendShapeKey key, BlendShapeClip clip)
        {
            int index = -1;

            try
            {
                index = Clips.FindIndex(x => key.Match(x));
            }
            catch (Exception)
            {
            }
            if (index == -1)
            {
                Clips.Add(clip);
            }
            else
            {
                Clips[index] = clip;
            }
        }
Exemplo n.º 26
0
            public void SetValue(BlendShapeKey key, float value, bool replace, Dictionary <string, Material> materialMap)
            {
                m_valueMap[key] = value;

                BlendShapeClip clip;

                if (!m_clipMap.TryGetValue(key, out clip))
                {
                    return;
                }

                foreach (var binding in clip.Values)
                {
                    BlendShapePathHandler handler;
                    if (m_setterMap.TryGetValue(binding, out handler))
                    {
                        if (replace)
                        {
                            // 値置き換え
                            //handler.ReplaceValue();
                            handler.Apply(binding.Weight * value);
                        }
                        else
                        {
                            // 積算
                            handler.AddValue(binding.Weight * value);
                        }
                    }
                    else
                    {
                        Debug.LogWarningFormat("'{0}' not found", binding);
                    }
                }

                // materialの更新
                foreach (var binding in clip.MaterialValues)
                {
                    var propValue = binding.BaseValue + (binding.TargetValue - binding.BaseValue) * value;
                    materialMap[binding.MaterialName].SetColor(binding.ValueName, propValue);
                }
            }
Exemplo n.º 27
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            // buttons
            if (m_target.Clips != null)
            {
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Select BlendShapeClip", EditorStyles.boldLabel);
                var array = m_target.Clips
                            .Select(x => x != null
                        ? BlendShapeKey.CreateFrom(x).ToString()
                        : "null"
                                    ).ToArray();
                var preset = GUILayout.SelectionGrid(m_preset, array, 4);
                if (preset != m_preset)
                {
                    CurrentClip = m_target.Clips[preset];
                    m_preset    = preset;
                }
            }

            // Add
            if (GUILayout.Button("Add BlendShapeClip"))
            {
                AddBlendShapeClip();
            }

            if (CurrentClip != null)
            {
                // clip
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("CurrentClip", EditorStyles.boldLabel);

                /*var loadClip = (BlendShapeClip)*/
                GUI.enabled = false;
                EditorGUILayout.ObjectField("Current clip",
                                            CurrentClip, typeof(BlendShapeClip), false);
                GUI.enabled = true;

                CurrentClip.Preset = (BlendShapePreset)EditorGUILayout.Popup("Preset", (int)CurrentClip.Preset, Presets);

                GUI.enabled = false;
                CurrentClip.BlendShapeName = EditorGUILayout.TextField("BlendShapeName", CurrentClip.BlendShapeName);
                GUI.enabled = true;

                var key = BlendShapeKey.CreateFrom(CurrentClip);
                if (m_target.Clips.Where(x => key.Match(x)).Count() > 1)
                {
                    EditorGUILayout.HelpBox("duplicate clip", MessageType.Error);
                }

                EditorGUILayout.Space();
                EditorGUILayout.LabelField("BlendShapeValues", EditorStyles.boldLabel);

                EditorGUILayout.BeginHorizontal();
                if (GUILayout.Button("Clear"))
                {
                    ClearBlendShape();
                }

                if (CurrentClip != null && GUILayout.Button("Apply"))
                {
                    string maxWeightString;
                    CurrentClip.Values = GetBindings(out maxWeightString);
                    EditorUtility.SetDirty(CurrentClip);
                }
                EditorGUILayout.EndHorizontal();

                // sliders
                bool changed   = false;
                int  foldIndex = 0;
                if (PreviewSceneManager != null)
                {
                    foreach (var item in PreviewSceneManager.EnumRenderItems.Where(x => x.SkinnedMeshRenderer != null))
                    {
                        var mesh = item.SkinnedMeshRenderer.sharedMesh;
                        if (mesh != null && mesh.blendShapeCount > 0)
                        {
                            //var relativePath = UniGLTF.UnityExtensions.RelativePathFrom(renderer.transform, m_target.transform);
                            //EditorGUILayout.LabelField(m_target.name + "/" + item.Path);

                            if (foldIndex >= m_meshFolds.Count)
                            {
                                m_meshFolds.Add(false);
                            }
                            m_meshFolds[foldIndex] = EditorGUILayout.Foldout(m_meshFolds[foldIndex], item.SkinnedMeshRenderer.name);
                            if (m_meshFolds[foldIndex])
                            {
                                //EditorGUI.indentLevel += 1;
                                for (int i = 0; i < mesh.blendShapeCount; ++i)
                                {
                                    var src = item.SkinnedMeshRenderer.GetBlendShapeWeight(i);
                                    var dst = EditorGUILayout.Slider(mesh.GetBlendShapeName(i), src, 0, 100.0f);
                                    if (dst != src)
                                    {
                                        item.SkinnedMeshRenderer.SetBlendShapeWeight(i, dst);
                                        changed = true;
                                    }
                                }
                                //EditorGUI.indentLevel -= 1;
                            }
                            ++foldIndex;
                        }
                    }

                    if (changed)
                    {
                        PreviewSceneManager.Bake();
                    }
                }
            }
        }
Exemplo n.º 28
0
        public BlendShapeMerger(IEnumerable <BlendShapeClip> clips, Transform root)
        {
            m_materialMap = new Dictionary <string, Material>();
            foreach (var x in root.Traverse())
            {
                var renderer = x.GetComponent <Renderer>();
                if (renderer != null)
                {
                    foreach (var y in renderer.sharedMaterials.Where(y => y != null))
                    {
                        if (!string.IsNullOrEmpty(y.name))
                        {
                            if (!m_materialMap.ContainsKey(y.name))
                            {
                                m_materialMap.Add(y.name, y);
                            }
                        }
                    }
                }
            }

            m_clipMap = clips.ToDictionary(x => BlendShapeKey.CreateFrom(x), x => x);

            m_valueMap          = new Dictionary <BlendShapeKey, float>();
            m_setterMap         = new Dictionary <BlendShapeBinding, AccumulatingSetter>();
            m_materialSetterMap = new Dictionary <MaterialValueBinding, AccumulatingSetter>();
            foreach (var kv in m_clipMap)
            {
                foreach (var binding in kv.Value.Values)
                {
                    if (!m_setterMap.ContainsKey(binding))
                    {
                        var _target = root.Find(binding.RelativePath);
                        SkinnedMeshRenderer target = null;
                        if (_target != null)
                        {
                            target = _target.GetComponent <SkinnedMeshRenderer>();
                        }
                        if (target != null)
                        {
                            m_setterMap.Add(binding, new AccumulatingSetter
                            {
                                Setter = x =>
                                {
                                    target.SetBlendShapeWeight(binding.Index, x);
                                }
                            });
                        }
                        else
                        {
                            Debug.LogWarningFormat("SkinnedMeshRenderer: {0} not found", binding.RelativePath);
                        }
                    }
                }

                foreach (var binding in kv.Value.MaterialValues)
                {
                    if (!m_materialSetterMap.ContainsKey(binding))
                    {
                        Material target;
                        if (m_materialMap.TryGetValue(binding.MaterialName, out target))
                        {
                            m_materialSetterMap.Add(binding, new AccumulatingSetter
                            {
                                Setter = x =>
                                {
                                    //target.SetBlendShapeWeight(binding.Index, x);
                                    var propValue = binding.BaseValue + (binding.TargetValue - binding.BaseValue) * x;
                                    target.SetColor(binding.ValueName, propValue);
                                }
                            });
                        }
                        else
                        {
                            Debug.LogWarningFormat("material: {0} not found", binding.MaterialName);
                        }
                    }
                }
            }
        }
Exemplo n.º 29
0
 public BlendShapeClip GetClip(BlendShapeKey key)
 {
     return(m_merger?.GetClip(key));
 }
Exemplo n.º 30
0
 public BlendShapeSlider(VRMBlendShapeProxy target, BlendShapeKey key)
 {
     m_target = target;
     m_key    = key;
 }