Example #1
0
        private void Weight(float value, Vertex3 vertex, bool increment)
        {
            //LET'S TANGO

            Influence  targetInf     = null;
            BoneWeight targetWeight  = null;
            float      max           = 1.0f;
            int        selectedIndex = 0;

            IMatrixNode node = vertex.MatrixNode;

            if (node == null)
            {
                vertex._object.ConvertInf();
                node = vertex.MatrixNode;
            }

            List <BoneWeight> weights = node.Weights;

            if (_targetBone == null || _targetBone._locked)
            {
                return;
            }

            MDL0BoneNode origBone = null;

            if (node is MDL0BoneNode)
            {
                origBone = node as MDL0BoneNode;
                node     = new Influence(origBone);
            }

            bool refs = node.Users.Count > 1;

            if (refs)
            {
                targetInf = (node as Influence).Clone();
            }
            else
            {
                targetInf = (node as Influence);
            }

            weights = targetInf._weights;

            selectedIndex = vertex.IndexOfBone(TargetBone);
            if (selectedIndex == -1)
            {
                weights.Add(new BoneWeight(TargetBone, 0.0f));
                selectedIndex = weights.Count - 1;
            }

            targetWeight = targetInf._weights[selectedIndex];
            if (targetWeight.Locked)
            {
                return;
            }

            max = 1.0f;
            foreach (BoneWeight b in weights)
            {
                if (b.Locked)
                {
                    max -= b.Weight;
                }
            }

            value = increment ? RoundValue(targetWeight.Weight + value, max) : RoundValue(value, max);

            if (targetWeight.Weight == value)
            {
                return;
            }

            List <int> editableWeights = new List <int>();

            int c = 0;

            foreach (BoneWeight b in targetInf._weights)
            {
                if (!b.Locked && c != selectedIndex)
                {
                    editableWeights.Add(c);
                }
                c++;
            }

            if (editableWeights.Count == 0)
            {
                return;
            }

            float diff = targetWeight.Weight - value;

            targetWeight.Weight = value;

            float val = diff / (editableWeights.Count);

            if (value != max)
            {
                foreach (int i in editableWeights)
                {
                    targetInf._weights[i].Weight = (float)Math.Round((targetInf._weights[i].Weight + val).Clamp(0.0f, 1.0f), 7);
                }
            }
            else
            {
                foreach (int i in editableWeights)
                {
                    targetInf._weights[i].Weight = 0;
                }
            }

            //Don't want the modified value to be normalized
            bool locked = targetWeight.Locked;

            targetWeight.Locked = true;
            targetInf.Normalize();
            targetWeight.Locked = locked;

            vertex.MatrixNode = vertex._object.Model._influences.FindOrCreate(targetInf, false);
            vertex._object.ConvertInf();
            vertex._object.Model.SignalPropertyChange();

            _mainWindow.UpdateModel();
        }