Anima2D.SpriteMeshCache.SmoothWeights C# (CSharp) Method

SmoothWeights() public method

public SmoothWeights ( List targetNodes ) : void
targetNodes List
return void
		public void SmoothWeights(List<Node> targetNodes)
		{
			float[,] weights = new float[nodes.Count,bindPoses.Count];
			Array.Clear(weights,0,weights.Length);
			
			List<int> usedIndices = new List<int>();
			
			for (int i = 0; i < nodes.Count; i++)
			{
				usedIndices.Clear();
				
				BoneWeight weight = boneWeights[i];
				
				if(weight.boneIndex0 >= 0)
				{
					weights[i,weight.boneIndex0] = weight.weight0;
					usedIndices.Add(weight.boneIndex0);
				}
				if(weight.boneIndex1 >= 0 && !usedIndices.Contains(weight.boneIndex1))
				{
					weights[i,weight.boneIndex1] = weight.weight1;
					usedIndices.Add(weight.boneIndex1);
				}
				if(weight.boneIndex2 >= 0 && !usedIndices.Contains(weight.boneIndex2))
				{
					weights[i,weight.boneIndex2] = weight.weight2;
					usedIndices.Add(weight.boneIndex2);
				}
				if(weight.boneIndex3 >= 0 && !usedIndices.Contains(weight.boneIndex3))
				{
					weights[i,weight.boneIndex3] = weight.weight3;
					usedIndices.Add(weight.boneIndex3);
				}
			}
			
			float[] denominator = new float[nodes.Count];
			float[,] smoothedWeights = new float[nodes.Count,bindPoses.Count]; 
			Array.Clear(smoothedWeights,0,smoothedWeights.Length);
			
			for (int i = 0; i < indices.Count / 3; ++i)
			{
				for (int j = 0; j < 3; ++j)
				{
					int j1 = (j + 1) % 3;
					int j2 = (j + 2) % 3;
					
					for(int k = 0; k < bindPoses.Count; ++k)
					{
						smoothedWeights[indices[i*3 + j],k] += weights[indices[i*3 + j1],k] + weights[indices[i*3 + j2],k]; 
					}
					
					denominator[indices[i*3 + j]] += 2;
				}
			}
			
			for (int i = 0; i < nodes.Count; ++i)
			{
				for (int j = 0; j < bindPoses.Count; ++j)
				{
					smoothedWeights[i,j] /= denominator[i];
				}
			}
			
			float[,] smoothedWeightsTransposed = new float[bindPoses.Count,nodes.Count]; 
			
			for (int i = 0; i < nodes.Count; ++i)
			{
				for (int j = 0; j < bindPoses.Count; ++j)
				{
					smoothedWeightsTransposed[j,i] = smoothedWeights[i,j];
				}
			}
			
			FillBoneWeights(targetNodes, smoothedWeightsTransposed);
			
			isDirty = true;
		}
		

Usage Example

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;
        }