Ejemplo n.º 1
0
        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.CreateFrom(x).ToString()
                        : "null"
                                    ).ToArray();
                SelectedIndex = GUILayout.SelectionGrid(SelectedIndex, array, 4);
            }

            if (GUILayout.Button("Add 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);
                }
            }
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
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);
                 }
             }
         }
     }
 }
Ejemplo n.º 4
0
        public void DuplicateWarn()
        {
            var key = BlendShapeKey.CreateFrom(Selected);

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

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

            m_blendShapeBindingMerger    = new BlendShapeBindingMerger(m_clipMap, root);
            m_materialValueBindingMerger = new MaterialValueBindingMerger(m_clipMap, root);
        }
Ejemplo n.º 6
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()
         ;
     }
 }
Ejemplo n.º 7
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)));
         }
     }
 }
Ejemplo n.º 8
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()
         ;
     }
 }
Ejemplo n.º 9
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();
                    }
                }
            }
        }
Ejemplo n.º 10
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);
                        }
                    }
                }
            }
        }
Ejemplo n.º 11
0
 public override string GetInfoString()
 {
     return(BlendShapeKey.CreateFrom((BlendShapeClip)target).ToString());
 }
Ejemplo n.º 12
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>();

            foreach (var kv in m_clipMap)
            {
                foreach (var binding in kv.Value.Values)
                {
                    if (!m_blendShapeSetterMap.ContainsKey(binding))
                    {
                        var _target = root.Find(binding.RelativePath);
                        SkinnedMeshRenderer target = null;
                        if (_target != null)
                        {
                            target = _target.GetComponent <SkinnedMeshRenderer>();
                        }
                        if (target != null)
                        {
                            if (binding.Index >= 0 && binding.Index < target.sharedMesh.blendShapeCount)
                            {
                                m_blendShapeSetterMap.Add(binding, x =>
                                {
                                    target.SetBlendShapeWeight(binding.Index, x);
                                });
                            }
                            else
                            {
                                Debug.LogWarningFormat("Invalid blendshape binding: {0}: {1}", target.name, binding);
                            }
                        }
                        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))
                        {
                            if (binding.ValueName.EndsWith("_ST_S"))
                            {
                                var            valueName = binding.ValueName.Substring(0, binding.ValueName.Length - 2);
                                Action <float> setter    = value =>
                                {
                                    var propValue = binding.BaseValue + (binding.TargetValue - binding.BaseValue) * value;
                                    var src       = target.GetVector(valueName);
                                    src.x = propValue.x; // horizontal only
                                    src.z = propValue.z; // horizontal only
                                    target.SetVector(valueName, src);
                                };
                                m_materialSetterMap.Add(binding, setter);
                            }
                            else if (binding.ValueName.EndsWith("_ST_T"))
                            {
                                var            valueName = binding.ValueName.Substring(0, binding.ValueName.Length - 2);
                                Action <float> setter    = value =>
                                {
                                    var propValue = binding.BaseValue + (binding.TargetValue - binding.BaseValue) * value;
                                    var src       = target.GetVector(valueName);
                                    src.y = propValue.y; // vertical only
                                    src.w = propValue.w; // vertical only
                                    target.SetVector(valueName, src);
                                };
                                m_materialSetterMap.Add(binding, setter);
                            }
                            else
                            {
                                Action <float> vec4Setter = x =>
                                {
                                    var propValue = binding.BaseValue + (binding.TargetValue - binding.BaseValue) * x;
                                    target.SetColor(binding.ValueName, propValue);
                                };
                                m_materialSetterMap.Add(binding, vec4Setter);
                            }
                        }
                        else
                        {
                            Debug.LogWarningFormat("material: {0} not found", binding.MaterialName);
                        }
                    }
                }
            }
        }