SetBoneWeight() public method

public SetBoneWeight ( Node node, BoneWeight boneWeight ) : void
node Node
boneWeight BoneWeight
return void
Beispiel #1
0
        protected override void DoWindow(int windowId)
        {
            float labelWidth = EditorGUIUtility.labelWidth;
            bool  wideMode   = EditorGUIUtility.wideMode;

            EditorGUIUtility.wideMode = true;

            EditorGUILayout.BeginHorizontal();

            string[] names = spriteMeshCache.GetBoneNames("None");
            int      index = spriteMeshCache.bindPoses.IndexOf(spriteMeshCache.selectedBindPose);

            index = EditorGUILayout.Popup(index + 1, names, GUILayout.Width(75f)) - 1;

            if (index >= 0 && index < spriteMeshCache.bindPoses.Count)
            {
                spriteMeshCache.selectedBindPose = spriteMeshCache.bindPoses[index];
            }
            else
            {
                spriteMeshCache.selectedBindPose = null;
            }

            EditorGUI.BeginChangeCheck();

            EditorGUI.BeginDisabledGroup(spriteMeshCache.selectedBindPose == null);

            if (Event.current.type == EventType.MouseUp ||
                Event.current.type == EventType.MouseDown)
            {
                m_Weight = 0f;

                m_TempWeights.Clear();

                if (spriteMeshCache.selection.Count == 0)
                {
                    m_TempWeights = spriteMeshCache.boneWeights.ToList();
                }
                else
                {
                    m_TempWeights = spriteMeshCache.selectedNodes.ConvertAll(n => spriteMeshCache.GetBoneWeight(n));
                }
            }

            EditorGUIUtility.fieldWidth = 35f;

            m_Weight = EditorGUILayout.Slider(m_Weight, -1f, 1f);

            EditorGUIUtility.fieldWidth = 0f;

            if (EditorGUI.EndChangeCheck())
            {
                spriteMeshCache.RegisterUndo("modify weights");

                List <Node> nodes = null;

                if (spriteMeshCache.selection.Count == 0)
                {
                    nodes = spriteMeshCache.nodes;
                }
                else
                {
                    nodes = spriteMeshCache.selectedNodes;
                }

                for (int i = 0; i < nodes.Count; i++)
                {
                    Node       node       = nodes[i];
                    BoneWeight tempWeight = m_TempWeights[i];
                    tempWeight.SetBoneIndexWeight(index, tempWeight.GetBoneWeight(index) + m_Weight, !EditorGUI.actionKey, true);
                    spriteMeshCache.SetBoneWeight(node, tempWeight);
                }
            }

            EditorGUI.EndDisabledGroup();

            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (GUILayout.Button(new GUIContent("Smooth", "Smooth weights")))
            {
                spriteMeshCache.RegisterUndo("smooth weights");

                List <Node> targetNodes = spriteMeshCache.nodes;

                if (spriteMeshCache.selection.Count > 0)
                {
                    targetNodes = spriteMeshCache.selectedNodes;
                }

                spriteMeshCache.SmoothWeights(targetNodes);
            }

            if (GUILayout.Button(new GUIContent("Auto", "Calculate automatic weights")))
            {
                spriteMeshCache.RegisterUndo("calculate weights");

                List <Node> targetNodes = spriteMeshCache.nodes;

                if (spriteMeshCache.selection.Count > 0)
                {
                    targetNodes = spriteMeshCache.selectedNodes;
                }

                spriteMeshCache.CalculateAutomaticWeights(targetNodes);
            }

            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();

            EditorGUIUtility.labelWidth = 50f;

            overlayColors = EditorGUILayout.Toggle("Overlay", overlayColors);

            EditorGUIUtility.labelWidth = 30f;

            showPie = EditorGUILayout.Toggle("Pies", showPie);

            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();

            EditorGUIUtility.labelWidth = labelWidth;
            EditorGUIUtility.wideMode   = wideMode;
        }
        void DoVerticesInspector()
        {
            if (spriteMeshCache.selection.Count > 0)
            {
                string[] names = spriteMeshCache.GetBoneNames("Unassigned");

                BoneWeight boneWeight = BoneWeight.Create();

                EditorGUI.BeginChangeCheck();

                bool mixedBoneIndex0 = false;
                bool mixedBoneIndex1 = false;
                bool mixedBoneIndex2 = false;
                bool mixedBoneIndex3 = false;
                bool changedIndex0   = false;
                bool changedIndex1   = false;
                bool changedIndex2   = false;
                bool changedIndex3   = false;
                bool mixedWeight     = false;

                if (spriteMeshCache.multiselection)
                {
                    mixedWeight = true;

                    int boneIndex = -1;
                    mixedBoneIndex0 = IsMixedBoneIndex(0, out boneIndex);
                    if (!mixedBoneIndex0)
                    {
                        boneWeight.boneIndex0 = boneIndex;
                    }
                    mixedBoneIndex1 = IsMixedBoneIndex(1, out boneIndex);
                    if (!mixedBoneIndex1)
                    {
                        boneWeight.boneIndex1 = boneIndex;
                    }
                    mixedBoneIndex2 = IsMixedBoneIndex(2, out boneIndex);
                    if (!mixedBoneIndex2)
                    {
                        boneWeight.boneIndex2 = boneIndex;
                    }
                    mixedBoneIndex3 = IsMixedBoneIndex(3, out boneIndex);
                    if (!mixedBoneIndex3)
                    {
                        boneWeight.boneIndex3 = boneIndex;
                    }
                }
                else
                {
                    boneWeight = spriteMeshCache.GetBoneWeight(spriteMeshCache.selectedNode);
                }

                EditorGUI.BeginChangeCheck();

                EditorGUI.BeginChangeCheck();
                boneWeight    = EditorGUIExtra.Weight(boneWeight, 0, names, mixedBoneIndex0, mixedWeight);
                changedIndex0 = EditorGUI.EndChangeCheck();

                EditorGUI.BeginChangeCheck();
                boneWeight    = EditorGUIExtra.Weight(boneWeight, 1, names, mixedBoneIndex1, mixedWeight);
                changedIndex1 = EditorGUI.EndChangeCheck();

                EditorGUI.BeginChangeCheck();
                boneWeight    = EditorGUIExtra.Weight(boneWeight, 2, names, mixedBoneIndex2, mixedWeight);
                changedIndex2 = EditorGUI.EndChangeCheck();

                EditorGUI.BeginChangeCheck();
                boneWeight    = EditorGUIExtra.Weight(boneWeight, 3, names, mixedBoneIndex3, mixedWeight);
                changedIndex3 = EditorGUI.EndChangeCheck();

                if (EditorGUI.EndChangeCheck())
                {
                    spriteMeshCache.RegisterUndo("modify weights");

                    if (spriteMeshCache.multiselection)
                    {
                        List <Node> selectedNodes = spriteMeshCache.selectedNodes;

                        foreach (Node node in selectedNodes)
                        {
                            BoneWeight l_boneWeight = spriteMeshCache.GetBoneWeight(node);

                            if (!mixedBoneIndex0 || changedIndex0)
                            {
                                l_boneWeight.SetWeight(0, boneWeight.boneIndex0, l_boneWeight.weight0);
                            }
                            if (!mixedBoneIndex1 || changedIndex1)
                            {
                                l_boneWeight.SetWeight(1, boneWeight.boneIndex1, l_boneWeight.weight1);
                            }
                            if (!mixedBoneIndex2 || changedIndex2)
                            {
                                l_boneWeight.SetWeight(2, boneWeight.boneIndex2, l_boneWeight.weight2);
                            }
                            if (!mixedBoneIndex3 || changedIndex3)
                            {
                                l_boneWeight.SetWeight(3, boneWeight.boneIndex3, l_boneWeight.weight3);
                            }

                            spriteMeshCache.SetBoneWeight(node, l_boneWeight);
                        }
                    }
                    else
                    {
                        spriteMeshCache.SetBoneWeight(spriteMeshCache.selectedNode, boneWeight);
                    }
                }

                EditorGUI.showMixedValue = false;
            }
        }