Пример #1
0
        public override void OnBind(FrameSkin value)
        {
            _skin               = value.Skin;
            _bones              = _skin.Bones;
            _bindShapePose      = _skin.BindShapePose;
            _boneOffsetMatrices = _skin.BoneBindingMatrices;

            if (_bones != null && !_skin.HasBonesPerLayer)
            {
                #region Compute Bone Matrices
                int paletteEntry = 0;
                //set all bones
                for (paletteEntry = 0; paletteEntry < _bones.Length && paletteEntry < maxPaletteMatrices; paletteEntry++)
                {
                    Matrix globalPose = _bones[paletteEntry].GlobalPose;
                    Matrix.Multiply(ref _bindShapePose, ref _boneOffsetMatrices[paletteEntry], out _boneMatrices[paletteEntry]);
                    Matrix.Multiply(ref _boneMatrices[paletteEntry], ref globalPose, out _boneMatrices[paletteEntry]);

                    //boneMatrices[paletteEntry] = bindShapePose *
                    //                               boneOffsetMatrices[paletteEntry] *
                    //                              bones[paletteEntry].GlobalPose;
                }

                if (Mapping != null)
                {
                    Mapping.WorldArray = new SArray <Matrix>(_boneMatrices, paletteEntry);
                    //mapping.WorldArray = boneMatrices;
                }

                #endregion
            }
        }
Пример #2
0
        public static bool RemoveSkin(Frame bone, MeshSkin skin)
        {
            var list = GetSkins(bone);

            if (list != null)
            {
                if (list.Count > 0)
                {
                    return(list.Remove(skin));
                }
                else
                {
                    return(_skins.Remove(bone));
                }
            }
            return(false);
        }
Пример #3
0
        public static void AddSkin(Frame bone, MeshSkin skin)
        {
            var list = GetSkins(bone);

            if (list == null)
            {
                list = new List <MeshSkin>()
                {
                    skin
                };
                _skins.Add(bone, list);
            }
            else
            {
                list.Add(skin);
            }
        }
Пример #4
0
        private void RenderLayers(GraphicDevice device,
                                  MeshSkin skin,
                                  Frame[] bones,
                                  Matrix[] boneOffsetMatrices,
                                  ref Matrix bindShapePose,
                                  MeshPart[] layers)
        {
            var effect = this.Effect;

            effect.OnRender(this);

            foreach (var pass in effect.Passes())
            {
                effect.Apply(pass);
                foreach (var layer in layers)
                {
                    #region Compute Bone Matrices

                    var bonesIDs = skin.GetBones(layer);
                    if (bonesIDs != null)
                    {
                        int paletteEntry = 0;
                        for (paletteEntry = 0; paletteEntry < bonesIDs.Length; paletteEntry++)
                        {
                            int boneIndex = bonesIDs[paletteEntry];

                            Matrix globalPose = bones[boneIndex].GlobalPose;

                            Matrix.Multiply(ref bindShapePose, ref boneOffsetMatrices[boneIndex], out _boneMatrices[paletteEntry]);
                            Matrix.Multiply(ref _boneMatrices[paletteEntry], ref globalPose, out _boneMatrices[paletteEntry]);
                            //boneMatrices[paletteEntry] = skin.BindShapePose * boneOffsetMatrices[boneIndex] * bones[boneIndex].GlobalPose;
                        }

                        SkinMap.WorldArray = new SArray <Matrix>(_boneMatrices, paletteEntry);
                        //mapping.WorldArray = boneMatrices;
                    }

                    #endregion

                    device.DrawIndexed(layer.primitiveCount * 3, layer.startIndex, 0);
                }
            }
            effect.EndPasses();
        }
Пример #5
0
        public static void TransformSkin(MeshSkin skin, ref Vector3 min, ref Vector3 max, Matrix view)
        {
            var mesh        = skin.Mesh;
            var positions   = mesh.GetVertexBufferView <Vector3>(IASemantic.Position);
            var boneIndices = mesh.GetVertexBufferView <Vector4>(IASemantic.BlendIndices);
            var boneWeights = mesh.GetVertexBufferView <Vector4>(IASemantic.BlendWeight);
            var bones       = skin.Bones;
            var boneOffets  = skin.BoneBindingMatrices;

            if (skin.HasBonesPerLayer)
            {
                foreach (var part in mesh.Layers)
                {
                    var partBones = skin.GetBones(part);
                    for (int i = 0; i < part.VertexCount; i++)
                    {
                        var     pos          = Vector3.TransformCoordinates(positions[part.StartVertex + i], skin.BindShapePose);
                        var     blendIndices = boneIndices[part.StartVertex + i];
                        var     blendWeights = boneWeights[part.StartVertex + i];
                        Vector3 posWorld     = new Vector3();
                        float   lastWeight   = 0;
                        unsafe
                        {
                            float *pIndices = (float *)&blendIndices;
                            float *pWeights = (float *)&blendWeights;
                            int    ibone    = 0;

                            for (int k = 0; k < 3; k++)
                            {
                                lastWeight += pWeights[k];
                                ibone       = partBones[(int)pIndices[k]];

                                posWorld += Vector3.TransformCoordinates(pos,
                                                                         boneOffets[ibone] * bones[ibone].GlobalPose) * pWeights[k];
                            }

                            lastWeight = 1.0f - lastWeight;
                            ibone      = partBones[(int)pIndices[3]];
                            posWorld  += Vector3.TransformCoordinates(pos,
                                                                      boneOffets[ibone] * bones[ibone].GlobalPose) * pWeights[3];
                        }

                        posWorld = Vector3.TransformCoordinates(posWorld, view);
                        min      = Vector3.Min(min, posWorld);
                        max      = Vector3.Max(max, posWorld);
                    }
                }
            }
            else
            {
                for (int i = 0; i < positions.Count; i++)
                {
                    var     pos          = Vector3.TransformCoordinates(positions[i], skin.BindShapePose);
                    var     blendIndices = boneIndices[i];
                    var     blendWeights = boneWeights[i];
                    Vector3 posWorld     = new Vector3();
                    float   lastWeight   = 0;
                    unsafe
                    {
                        float *pIndices = (float *)&blendIndices;
                        float *pWeights = (float *)&blendWeights;
                        int    ibone    = 0;

                        for (int k = 0; k < 3; k++)
                        {
                            lastWeight += pWeights[k];
                            ibone       = (int)pIndices[k];

                            posWorld += Vector3.TransformCoordinates(pos,
                                                                     boneOffets[ibone] * bones[ibone].GlobalPose) * pWeights[k];
                        }

                        lastWeight = 1.0f - lastWeight;
                        ibone      = (int)pIndices[3];
                        posWorld  += Vector3.TransformCoordinates(pos,
                                                                  boneOffets[ibone] * bones[ibone].GlobalPose) * pWeights[3];
                    }

                    posWorld = Vector3.TransformCoordinates(posWorld, view);

                    min = Vector3.Min(min, posWorld);
                    max = Vector3.Max(max, posWorld);
                }
            }

            mesh.ReleaseViews();
        }
Пример #6
0
 public SkinMeshComponent(MeshSkin skin, IEnumerable <Material> materials)
 {
     Skin      = skin;
     Materials = new List <Material>(materials);
 }
Пример #7
0
 public FrameSkin(BasicMaterial[] materials = null, MeshSkin skin = null) :
     base(materials)
 {
     this._skin = skin;
 }