Пример #1
0
        public BoneWeight WeightForBone(IBoneNode b)
        {
            int i = IndexOfBone(b); if (i == -1)

            {
                return(null);
            }
            return(GetBoneWeights()[i]);
        }
Пример #2
0
        public void SetSelectedBone(IBoneNode bone)
        {
            if (_treeNodes != null && bone != null && bone.BoneIndex < _treeNodes.Length)
            {
                boneTree.SelectedNode = _treeNodes[bone.BoneIndex];
            }

            lstBones.SelectedItem = bone;
        }
Пример #3
0
 public Influence(IBoneNode bone)
 {
     _weights = new List <BoneWeight> {
         new BoneWeight(bone)
     };
     if (!bone.LinkedInfluences.Contains(this))
     {
         bone.LinkedInfluences.Add(this);
     }
 }
Пример #4
0
        private void MouseDownTargetBone(MouseEventArgs e, ModelPanel panel)
        {
            //Re-target selected bone
            IBoneNode bone = SelectedBone;

            if (bone != null)
            {
                _snapX    = _hiX;
                _snapY    = _hiY;
                _snapZ    = _hiZ;
                _snapCirc = _hiCirc;

                //Targeting functions are done in HighlightStuff
                if (!(_snapX || _snapY || _snapZ || _snapCirc || _hiSphere))
                {
                    //Orb selection missed. Assign bone and move to next step.
                    SelectedBone = bone = null;
                    goto GetBone;
                }

                //Bone re-targeted. Get frame values and local point aligned to snapping plane.
                Vector3 point;
                if (GetTransformPoint(new Vector2(e.X, e.Y), out point, panel.CurrentViewport, GetBoneWorldMtx()))
                {
                    _lastPointLocal = GetBoneInvWorldMtx() * (_lastPointWorld = point);

                    if (_rotating = ControlType == TransformType.Rotation)
                    {
                        _oldAngles = bone.FrameState._rotate;
                    }
                    else if (_translating = ControlType == TransformType.Translation)
                    {
                        _oldPosition = bone.FrameState._translate;
                    }
                    else if (_scaling = ControlType == TransformType.Scale)
                    {
                        _oldScale = bone.FrameState._scale;
                    }

                    panel.CurrentViewport.AllowSelection = false;
                    if (_rotating || _translating || _scaling)
                    {
                        BoneChange(SelectedBone);
                    }
                }
            }

GetBone:

            //Try selecting new bone
            if (bone == null && panel.RenderBones)
            {
                SelectedBone = _hiBone;
            }
        }
Пример #5
0
        private void SetBone(IBoneNode bone)
        {
            if (SelectedBone != null)
            {
                SelectedBone.BoneColor = SelectedBone.NodeColor = Color.Transparent;
            }

            SelectedBone = bone;

            _mainWindow.ModelPanel.Invalidate();
        }
Пример #6
0
        public unsafe void RenderOrb(IBoneNode bone, GLDisplayList list, ModelPanelViewport v, bool doScale)
        {
            float radius = MDL0BoneNode._nodeRadius * (doScale ? OrbRadius(bone, v) : 1.0f);

            Matrix m = Matrix.TransformMatrix(new Vector3(radius), new Vector3(), bone.Matrix.GetPoint());

            GL.PushMatrix();
            GL.MultMatrix((float *)&m);

            list.Call();
            GL.PopMatrix();
        }
Пример #7
0
        private void RecursivePopulate(IBoneNode bone, TreeNodeCollection nodes)
        {
            TreeNode node = new TreeNode()
            {
                Tag = bone, Text = bone.Name, Checked = bone.IsRendering
            };

            _treeNodes[bone.BoneIndex] = node;
            nodes.Add(node);

            foreach (IBoneNode b in ((ResourceNode)bone).Children)
            {
                RecursivePopulate(b, node.Nodes);
            }
        }
Пример #8
0
        private bool CompareBoneDistanceRecursive(IBoneNode bone, Vector3 point, ref IBoneNode match, ModelPanelViewport v, bool doScale)
        {
            float dist = bone.Matrix.GetPoint().TrueDistance(point) / (doScale ? OrbRadius(bone, v) : 1.0f);

            if (Math.Abs(dist - MDL0BoneNode._nodeRadius) < 0.01f)
            {
                match = bone;
                return(true);
            }

            foreach (IBoneNode b in ((ResourceNode)bone).Children)
            {
                if (CompareBoneDistanceRecursive(b, point, ref match, v, doScale))
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #9
0
        public Color GetWeightColor(IBoneNode targetBone)
        {
            float weight = -1.0f;

            if (_matrixNode == null || targetBone == null)
            {
                return(Color.Transparent);
            }
            if (_matrixNode is MDL0BoneNode)
            {
                if (_matrixNode == targetBone)
                {
                    weight = 1.0f;
                }
                else
                {
                    return(Color.Transparent);
                }
            }
            else
            {
                foreach (BoneWeight b in ((Influence)_matrixNode).Weights)
                {
                    if (b.Bone == targetBone)
                    {
                        weight = b.Weight;
                        break;
                    }
                }
            }
            if (weight < 0.0f || weight > 1.0f)
            {
                return(Color.Transparent);
            }
            int r = ((int)(weight * 255.0f)).Clamp(0, 0xFF);

            return(Color.FromArgb(r, 0, 0xFF - r));
        }
Пример #10
0
        private static PrimitiveManager DecodePrimitivesWeighted(
            Matrix bindMatrix,
            GeometryEntry geo,
            SkinEntry skin,
            SceneEntry scene,
            InfluenceManager infManager,
            Type boneType)
        {
            PrimitiveManager manager = DecodePrimitives(geo);

            IBoneNode[] boneList;
            IBoneNode   bone = null;
            int         boneCount;

            string[] jointStringArray = null;
            string   jointString      = null;

            byte *         pCmd = stackalloc byte[4];
            int            cmdCount = skin._weightInputs.Count;
            float          weight = 0;
            float *        pWeights = null;
            Vector3 *      pVert = null, pNorms = null;
            ushort *       pVInd    = (ushort *)manager._indices.Address;
            List <Vertex3> vertList = new List <Vertex3>(skin._weightCount);
            Matrix *       pMatrix  = null;

            UnsafeBuffer remap  = new UnsafeBuffer(skin._weightCount * 2);
            ushort *     pRemap = (ushort *)remap.Address;

            if (manager._faceData[1] != null)
            {
                pNorms = (Vector3 *)manager._faceData[1].Address;
            }

            manager._vertices = vertList;

            //Find vertex source
            foreach (SourceEntry s in geo._sources)
            {
                if (s._id == geo._verticesInput._source)
                {
                    pVert = (Vector3 *)((UnsafeBuffer)s._arrayData).Address;
                    break;
                }
            }

            //Find joint source
            foreach (InputEntry inp in skin._jointInputs)
            {
                if (inp._semantic == SemanticType.JOINT)
                {
                    foreach (SourceEntry src in skin._sources)
                    {
                        if (src._id == inp._source)
                        {
                            jointStringArray = src._arrayData as string[];
                            jointString      = src._arrayDataString;
                            break;
                        }
                    }
                }
                else if (inp._semantic == SemanticType.INV_BIND_MATRIX)
                {
                    foreach (SourceEntry src in skin._sources)
                    {
                        if (src._id == inp._source)
                        {
                            pMatrix = (Matrix *)((UnsafeBuffer)src._arrayData).Address;
                            break;
                        }
                    }
                }
            }

            Error = "There was a problem creating the list of bones for geometry entry " + geo._name;

            //Populate bone list
            boneCount = jointStringArray.Length;
            boneList  = new IBoneNode[boneCount];
            for (int i = 0; i < boneCount; i++)
            {
                NodeEntry entry = scene.FindNode(jointStringArray[i]);
                if (entry != null && entry._node != null)
                {
                    boneList[i] = entry._node as IBoneNode;
                }
                else
                {
                    //Search in reverse!
                    foreach (NodeEntry node in scene._nodes)
                    {
                        if ((entry = RecursiveTestNode(jointString, node)) != null)
                        {
                            if (entry._node != null)
                            {
                                boneList[i] = entry._node as IBoneNode;
                            }

                            break;
                        }
                    }

                    //Couldn't find the bone
                    if (boneList[i] == null)
                    {
                        boneList[i] = Activator.CreateInstance(boneType) as IBoneNode;
                    }
                }
            }

            //Build command list
            foreach (InputEntry inp in skin._weightInputs)
            {
                switch (inp._semantic)
                {
                case SemanticType.JOINT:
                    pCmd[inp._offset] = 1;
                    break;

                case SemanticType.WEIGHT:
                    pCmd[inp._offset] = 2;

                    //Get weight source
                    foreach (SourceEntry src in skin._sources)
                    {
                        if (src._id == inp._source)
                        {
                            pWeights = (float *)((UnsafeBuffer)src._arrayData).Address;
                            break;
                        }
                    }

                    break;

                default:
                    pCmd[inp._offset] = 0;
                    break;
                }
            }

            Error = "There was a problem creating vertex influences for geometry entry " + geo._name;

            //Build vertex list and remap table
            for (int i = 0; i < skin._weightCount; i++)
            {
                //Create influence
                int       iCount = skin._weights[i].Length / cmdCount;
                Influence inf    = new Influence();
                fixed(int *p = skin._weights[i])
                {
                    int *iPtr = p;

                    for (int x = 0; x < iCount; x++)
                    {
                        for (int z = 0; z < cmdCount; z++, iPtr++)
                        {
                            if (pCmd[z] == 1)
                            {
                                bone = boneList[*iPtr];
                            }
                            else if (pCmd[z] == 2)
                            {
                                weight = pWeights[*iPtr];
                            }
                        }

                        inf.AddWeight(new BoneWeight(bone, weight));
                    }
                }

                inf.CalcMatrix();

                Error = "There was a problem creating a vertex from the geometry entry " + geo._name +
                        ".\nMake sure that all the vertices are weighted properly.";

                Vector3 worldPos = bindMatrix * skin._bindMatrix * pVert[i];
                Vertex3 v;
                if (inf.Weights.Count > 1)
                {
                    //Match with manager
                    inf = infManager.FindOrCreate(inf);
                    v   = new Vertex3(worldPos, inf); //World position
                }
                else
                {
                    bone = inf.Weights[0].Bone;
                    v    = new Vertex3(bone.InverseBindMatrix * worldPos, bone); //Local position
                }

                ushort index = 0;
                while (index < vertList.Count)
                {
                    if (v.Equals(vertList[index]))
                    {
                        break;
                    }

                    index++;
                }

                if (index == vertList.Count)
                {
                    vertList.Add(v);
                }

                pRemap[i] = index;
            }

            Error = "There was a problem fixing normal rotations for geometry entry " + geo._name;

            //Remap vertex indices and fix normals
            for (int i = 0; i < manager._pointCount; i++, pVInd++)
            {
                *pVInd = pRemap[*pVInd];

                if (pNorms != null)
                {
                    Vertex3 v = null;
                    if (*pVInd < vertList.Count)
                    {
                        v = vertList[*pVInd];
                    }

                    if (v != null && v.MatrixNode != null)
                    {
                        if (v.MatrixNode.Weights.Count > 1)
                        {
                            pNorms[i] =
                                (bindMatrix *
                                 skin._bindMatrix).GetRotationMatrix() *
                                pNorms[i];
                        }
                        else
                        {
                            pNorms[i] =
                                (v.MatrixNode.Weights[0].Bone.InverseBindMatrix *
                                 bindMatrix *
                                 skin._bindMatrix).GetRotationMatrix() *
                                pNorms[i];
                        }
                    }
                }
            }

            remap.Dispose();
            return(manager);
        }
 public static Vector3 BoneLoc(IBoneNode b)
 {
     return(b == null ? new Vector3() : b.Matrix.GetPoint());
 }
 public static float OrbRadius(IBoneNode b, GLCamera cam, float radius = _orbRadius)
 {
     return(CamDistance(BoneLoc(b), cam, radius));
 }
 public static float OrbRadius(IBoneNode b, GLViewport viewport, float radius = _orbRadius)
 {
     return(CamDistance(BoneLoc(b), viewport.Camera, radius));
 }
Пример #14
0
 public BoneWeight(IBoneNode bone) : this(bone, 1.0f)
 {
 }
Пример #15
0
 public int IndexOfBone(IBoneNode b)
 {
     return(Array.IndexOf(GetBones(), b));
 }
Пример #16
0
        private void GetBone(ModelPanel panel, MouseEventArgs e, float depth, ModelPanelViewport v)
        {
            if (!_boneSelection.IsMoving() && depth < 1.0f)
            {
                IBoneNode o = null;

                Vector3 point   = v.UnProject(e.X, e.Y, depth);
                bool    doScale = v._renderAttrib._scaleBones;

                //Find orb near chosen point
                if (EditingAll)
                {
                    foreach (IModel m in _targetModels)
                    {
                        foreach (IBoneNode b in m.RootBones)
                        {
                            if (CompareBoneDistanceRecursive(b, point, ref o, v, doScale))
                            {
                                break;
                            }
                        }
                    }
                }
                else if (_targetModel != null)
                {
                    foreach (IBoneNode b in _targetModel.RootBones)
                    {
                        if (CompareBoneDistanceRecursive(b, point, ref o, v, doScale))
                        {
                            break;
                        }
                    }
                }

                bool update = false;
                if (_hiBone != null && _hiBone != SelectedBone && (update = _hiBone.NodeColor != Color.Transparent))
                {
                    _hiBone.NodeColor = Color.Transparent;
                }

                if ((_hiBone = o) != null)
                {
                    _hiBone.NodeColor = Color.FromArgb(255, 128, 0);
                    panel.Cursor      = Cursors.Hand;
                    update            = true;
                }

                if (update)
                {
                    panel.Invalidate();
                }
            }
            else if (_hiBone != null)
            {
                if (_hiBone != SelectedBone)
                {
                    _hiBone.NodeColor = Color.Transparent;
                    panel.Invalidate();
                }
                _hiBone = null;
            }
        }
Пример #17
0
 public void BoneChanged()
 {
     TargetBone = SelectedBone;
 }
Пример #18
0
 public BoneWeight(IBoneNode bone, float weight)
 {
     Bone = bone; Weight = weight;
 }