Ejemplo n.º 1
0
        private void ReadBones(ContentReader input)
        {
            ModelBone[] bones = new ModelBone[input.ReadInt32()];

            for (int i = 0; i < bones.Length; i++)
            {
                string name = input.ReadObject<string>();
                Matrix transform = input.ReadMatrix();
                bones[i] = new ModelBone(i, name, transform);
            }

            this.bones = new ModelBoneCollection(bones);

            foreach (ModelBone bone in bones)
            {
                ModelBone newParent = this.ReadBoneReference(input);
                int size = input.ReadInt32();
                ModelBone[] newChildren = new ModelBone[size];
                for (int j = 0; j < size; j++)
                {
                    newChildren[j] = this.ReadBoneReference(input);
                }
                bone.SetParentChildren(newParent, newChildren);
            }
        }
Ejemplo n.º 2
0
        // Internal creation
        internal BonePose(ModelBone bone,
                          ModelBoneCollection bones,
                          BonePose[] anims)
        {
            // Set the values according to the bone
            index         = bone.Index;
            name          = bone.Name;
            defaultMatrix = bone.Transform;
            if (bone.Parent != null)
            {
                parent = anims[bone.Parent.Index];
            }
            anims[index] = this;

            // Recurse on children
            List <BonePose> childList = new List <BonePose>();

            foreach (ModelBone child in bone.Children)
            {
                BonePose newChild = new BonePose(
                    bones[child.Index],
                    bones,
                    anims);
                childList.Add(newChild);
            }
            children = new BonePoseCollection(childList);
        }
Ejemplo n.º 3
0
		public Model(GraphicsDevice graphicsDevice, List<ModelBone> bones, List<ModelMesh> meshes)
		{
			// TODO: Complete member initialization
			this.graphicsDevice = graphicsDevice;

			Bones = new ModelBoneCollection(bones);
			Meshes = new ModelMeshCollection(meshes);
		}
Ejemplo n.º 4
0
 public Model(GraphicsBuffer elementBuffer, ElementType elementType, Sphere3d bounds)
 {
     if (elementBuffer == null)
         throw new ArgumentNullException("elementBuffer");
     this.elementBuffer = elementBuffer;
     this.elementType = elementType;
     this.bounds = bounds;
     bones = new ModelBoneCollection(this);
     meshes = new ModelMeshCollection(this);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Constructs a model. 
        /// </summary>
        /// <param name="graphicsDevice">A valid reference to <see cref="GraphicsDevice"/>.</param>
        /// <param name="bones">The collection of bones.</param>
        /// <param name="meshes">The collection of meshes.</param>
        public Model(GraphicsDevice graphicsDevice, List<ModelBone> bones, List<ModelMesh> meshes)
		{
            if(graphicsDevice == null)throw new ArgumentNullException("graphicsDevice");

			// TODO: Complete member initialization
			this.graphicsDevice = graphicsDevice;

			Bones = new ModelBoneCollection(bones);
			Meshes = new ModelMeshCollection(meshes);
		}
Ejemplo n.º 6
0
        /// <summary>
        /// Constructs a model. 
        /// </summary>
        /// <param name="graphicsDevice">A valid reference to <see cref="GraphicsDevice"/>.</param>
        /// <param name="bones">The collection of bones.</param>
        /// <param name="meshes">The collection of meshes.</param>
        public Model(GraphicsDevice graphicsDevice, List<ModelBone> bones, List<ModelMesh> meshes)
		{
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("graphicsDevice", FrameworkResources.ResourceCreationWhenDeviceIsNull);
            }

			// TODO: Complete member initialization
			this.graphicsDevice = graphicsDevice;

			Bones = new ModelBoneCollection(bones);
			Meshes = new ModelMeshCollection(meshes);
		}
Ejemplo n.º 7
0
            /// <summary>
            /// Build a dictionary out of the input bone collection.
            /// </summary>
            /// <param name="src"></param>
            /// <returns></returns>
            public static Dict Extract(ModelBoneCollection src)
            {
                Bone[] bones = new Bone[src.Count];

                for (int i = 0; i < src.Count; ++i)
                {
                    if (src[i].Parent == null)
                    {
                        AddBoneSubtree(src[i], bones);
                    }
                }
                return(new Dict(bones));
            }
Ejemplo n.º 8
0
        // Creates a set of bonepose objects from a skeleton
        internal static BonePoseCollection FromModelBoneCollection(
            ModelBoneCollection bones)
        {
            BonePose[] anims = new BonePose[bones.Count];
            for (int i = 0; i < bones.Count; i++)
            {
                if (bones[i].Parent == null)
                {
                    BonePose ba = new BonePose(
                        bones[i],
                        bones,
                        anims);
                }
            }

            return(new BonePoseCollection(anims));
        }
Ejemplo n.º 9
0
 internal c0000de(ModelBone p0, ModelBoneCollection p1, c0000de[] p2)
 {
     this.f00000b = p0.Index;
     this.f000006 = p0.Name;
     this.f0000e1 = p0.Transform;
     if (p0.Parent != null)
     {
         this.f000028 = p2[p0.Parent.Index];
     }
     p2[this.f00000b] = this;
     List<c0000de> list = new List<c0000de>();
     foreach (ModelBone bone in p0.Children)
     {
         c0000de item = new c0000de(p1[bone.Index], p1, p2);
         list.Add(item);
     }
     this.f000018 = new c0000db(list);
 }
Ejemplo n.º 10
0
        // Creates a set of bonepose objects from a skeleton
        internal static BonePoseCollection FromModelBoneCollection(
            ModelBoneCollection bones)
        {
            // A pose for every bone:
            BonePose[] anims = new BonePose[bones.Count];

            // Walk through and find root bones
            for (int i = 0; i < bones.Count; i++)
            {
                // Is this a root?
                if (bones[i].Parent == null)
                {
                    // Create a new bone pose and (ick) push it into the anims array
                    // This will recursively create bone poses for all the children of this
                    // bone and push them into the array as well
                    BonePose ba = new BonePose(
                        bones[i],
                        bones,
                        anims);
                }
            }

            return(new BonePoseCollection(anims));
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Helper used by the Update method to refresh the BoneTransforms data.
 /// </summary>
 public void UpdateBoneTransforms(int frame)
 {
     foreach (Keyframe k in keyframesList[frame])
     {
         ModelBoneCollection lowerTummyBones = currentModel.Bones["Pelvis"].Children;
         ModelBoneCollection upperTummyBones = currentModel.Bones["Spine"].Children;
         List <int>          tummyBoneIndexs = new List <int>();
         foreach (ModelBone t in lowerTummyBones)
         {
             tummyBoneIndexs.Add(t.Index);
         }
         foreach (ModelBone t in upperTummyBones)
         {
             if (t.Name != "Pelvis" && t.Name != "LShoulder" && t.Name != "RShoulder" && t.Name != "LChest" && t.Name != "RChest")
             {
                 tummyBoneIndexs.Add(t.Index);
             }
         }
         if (tummyBoneIndexs.Contains(k.Bone))
         {
             boneTransforms[k.Bone] = k.Transform;
         }
     }
 }
Ejemplo n.º 12
0
		/// <summary>
		/// Constructs a model.
		/// </summary>
		/// <param name="graphicsDevice">A valid reference to <see cref="GraphicsDevice"/>.</param>
		/// <param name="bones">The collection of bones.</param>
		/// <param name="meshes">The collection of meshes.</param>
		internal Model(GraphicsDevice graphicsDevice, List<ModelBone> bones, List<ModelMesh> meshes)
		{
			Bones = new ModelBoneCollection(bones);
			Meshes = new ModelMeshCollection(meshes);
		}
Ejemplo n.º 13
0
 public static void m000286(ModelBoneCollection p0, ref ModelBone p1)
 {
     Type type = typeof(ModelBone);
     type.GetField("children", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(p1, p0);
 }
Ejemplo n.º 14
0
		public ModelBone()
		{
			Children = new ModelBoneCollection(new List<ModelBone>());
			meshes = new List<ModelMesh>();
		}
Ejemplo n.º 15
0
		internal void AddChild(ModelBone modelBone)
		{
			children.Add(modelBone);
			Children = new ModelBoneCollection(children);
		}
Ejemplo n.º 16
0
 internal void SetParentChildren(ModelBone Parent, ModelBone[] Children)
 {
     this.parent = Parent;
     this.children = new ModelBoneCollection(Children);
 }
Ejemplo n.º 17
0
 public ModelBone()
 {
     children = new ModelBoneCollection();
     Index = 0;
     Transform = Matrix4.Identity;
 }
Ejemplo n.º 18
0
        protected void getChildren(ModelBone parent, ref ModelBoneCollection list)
        {
            foreach (var child in parent.children)
            {
                list.Add(child);

                child.getChildren(child, ref list);
            }
        }
Ejemplo n.º 19
0
        public ModelBoneCollection AllChildren(bool bIncludeSelf = true)
        {
            var childs = new ModelBoneCollection();

            if (bIncludeSelf) { childs.Add(this); }
            getChildren(this, ref childs);

            return childs;
        }
Ejemplo n.º 20
0
 public Model()
 {
     bones = new ModelBoneCollection();
     meshes = new List<ModelMesh>();
 }
Ejemplo n.º 21
0
        public static void MungeMeshWithBone(ModelBoneCollection bones)
        {
            var processed = new List<string>();

            var offset = (bones[0].Parent != null ? bones[0].Parent.Transform.ExtractTranslation() : OpenTK.Vector3.Zero);

            foreach (var bone in bones)
            {
                if (bone.Type == BoneType.Mesh && bone.Mesh != null && !processed.Contains(bone.Mesh.Name))
                {
                    var mesh = bone.Mesh;
                    var meshoffset = mesh.BoundingBox.Centre;

                    foreach (var meshpart in mesh.MeshParts)
                    {
                        for (int i = 0; i < meshpart.VertexCount; i++) { meshpart.VertexBuffer.ModifyVertexPosition(i, meshpart.VertexBuffer.Data[i].Position - meshoffset); }
                        meshpart.VertexBuffer.Initialise();
                    }
                    mesh.BoundingBox.Calculate(mesh);

                    var moffset = meshoffset - offset;
                    offset = meshoffset;

                    var m = bone.Transform;
                    m.M41 += moffset.X;
                    m.M42 += moffset.Y;
                    m.M43 += moffset.Z;
                    bone.Transform = m;

                    processed.Add(mesh.Name);
                }
            }
        }
Ejemplo n.º 22
0
        public static void Scale(ModelBoneCollection bones, Matrix4 scale, bool bApplyToHierarchy = false)
        {
            var processed = new List<string>();

            foreach (var bone in bones)
            {
                if (bone.Type == BoneType.Mesh && bone.Mesh != null && !processed.Contains(bone.Mesh.Name))
                {
                    var mesh = bone.Mesh;

                    foreach (var meshpart in mesh.MeshParts)
                    {
                        for (int i = 0; i < meshpart.VertexCount; i++)
                        {
                            var position = Vector3.Transform(meshpart.VertexBuffer.Data[i].Position, scale);
                            meshpart.VertexBuffer.ModifyVertexPosition(i, position);
                        }

                        meshpart.VertexBuffer.Initialise();
                    }

                    processed.Add(mesh.Name);
                }

                if (bApplyToHierarchy)
                {
                    var transform = bone.Transform;
                    var position = Vector3.TransformPosition(transform.ExtractTranslation(), scale);

                    transform.M41 = position.X;
                    transform.M42 = position.Y;
                    transform.M43 = position.Z;

                    bone.Transform = transform;
                }
            }
        }
Ejemplo n.º 23
0
        public static void ProcessLevelForCarmageddonMaxDamage()
        {
            if (SceneManager.Current.Models.Count == 0)
            {
                return;
            }

            ModelBoneCollection bones = SceneManager.Current.Models[0].Bones[0].AllChildren();

            SceneManager.Current.UpdateProgress("Applying Carmageddon: Max Damage scale");

            ModelManipulator.Scale(bones, Matrix4D.CreateScale(6.9f, 6.9f, -6.9f), true);
            ModelManipulator.FlipFaces(bones, true);

            SceneManager.Current.UpdateProgress("Fixing material names");

            foreach (Material material in SceneManager.Current.Materials)
            {
                if (material.Name.Contains("."))
                {
                    material.Name = material.Name.Substring(0, material.Name.IndexOf("."));
                }
                material.Name = material.Name.Replace("\\", "");

                MATMaterial m = (material.SupportingDocuments["Source"] as MATMaterial);
                if (!m.HasTexture)
                {
                    using (Bitmap bmp = new Bitmap(16, 16))
                        using (Graphics g = Graphics.FromImage(bmp))
                        {
                            g.FillRectangle(new SolidBrush(Color.FromArgb(m.DiffuseColour[3], m.DiffuseColour[0], m.DiffuseColour[1], m.DiffuseColour[2])), 0, 0, 16, 16);

                            Texture t = new Texture();
                            t.CreateFromBitmap(bmp, string.Format("{4}_R{0:x2}G{1:x2}B{2:x2}A{3:x2}", m.DiffuseColour[0], m.DiffuseColour[1], m.DiffuseColour[2], m.DiffuseColour[3], material.Name));
                            material.Texture = t;
                        }
                }
            }

            SceneManager.Current.UpdateProgress("Processing powerups and accessories");

            for (int i = bones.Count - 1; i >= 0; i--)
            {
                ModelBone bone = bones[i];

                if (bone.Name.StartsWith("&"))
                {
                    if (bone.Name.StartsWith("&£"))
                    {
                        C2Powerup pup     = Powerups.LookupID(int.Parse(bone.Name.Substring(2, 2)));
                        Powerup   powerup = new Powerup();

                        if (pup.InMD)
                        {
                            powerup.Name = $"pup_{pup.Name}";
                            powerup.Tag  = pup.Model;
                        }
                        else
                        {
                            powerup.Name = "pup_Credits";
                            powerup.Tag  = pup.Model;
                        }

                        powerup.Transform = bone.CombinedTransform;

                        SceneManager.Current.Entities.Add(powerup);
                    }
                    else
                    {
                        Accessory accessory = new Accessory
                        {
                            Name = $"C2_{bone.Mesh.Name.Substring(3)}"
                        };

                        SceneManager.Current.Entities.Add(accessory);
                    }

                    SceneManager.Current.Models[0].RemoveBone(bone.Index);
                }
            }

            if (SceneManager.Current.Models[0].SupportingDocuments.ContainsKey("TXT"))
            {
                Map map = SceneManager.Current.Models[0].GetSupportingDocument <Map>("TXT");

                StartingGrid grid = new StartingGrid
                {
                    Transform = Matrix4D.CreateTranslation(map.GridPosition.X * 6.9f, map.GridPosition.Y * 6.9f, map.GridPosition.Z * -6.9f)
                };

                SceneManager.Current.Entities.Add(grid);
            }

            SceneManager.Current.UpdateProgress("Processing complete!");

            SceneManager.Current.SetCoordinateSystem(CoordinateSystem.LeftHanded);

            SceneManager.Current.Change(ChangeType.Munge, ChangeContext.Model, -1);

            SceneManager.Current.SetContext("Carmageddon Max Damage", ContextMode.Level);
        }
Ejemplo n.º 24
0
        public static void ProcessCarForCarmageddonMaxDamage()
        {
            if (SceneManager.Current.Models.Count == 0)
            {
                return;
            }

            Model model = SceneManager.Current.Models[0];
            ModelBoneCollection bones = SceneManager.Current.Models[0].Bones[0].AllChildren();

            SceneManager.Current.UpdateProgress("Applying Carmageddon Reincarnation scale");

            ModelManipulator.Scale(bones, Matrix4D.CreateScale(6.9f, 6.9f, -6.9f), true);
            ModelManipulator.FlipFaces(bones, true);

            SceneManager.Current.UpdateProgress("Fixing material names");

            foreach (Material material in SceneManager.Current.Materials)
            {
                if (material.Name.Contains("."))
                {
                    material.Name = material.Name.Substring(0, material.Name.IndexOf("."));
                }
                material.Name = material.Name.Replace("\\", "");
            }

            SceneManager.Current.UpdateProgress("Munging parts and fixing wheels");

            float scale;

            for (int i = 0; i < bones.Count; i++)
            {
                ModelBone bone = bones[i];

                if (i == 0)
                {
                    bone.Name      = "c_Body";
                    bone.Mesh.Name = "c_Body";
                }
                else
                {
                    bone.Name = Path.GetFileNameWithoutExtension(bone.Name);
                }

                switch (bone.Name.ToUpper())
                {
                case "C_BODY":
                    break;

                case "FLPIVOT":
                case "FRPIVOT":
                    bone.Name = "Hub_" + bone.Name.ToUpper().Substring(0, 2);

                    if (bone.Transform.ExtractTranslation() == Vector3.Zero)
                    {
                        ModelManipulator.MungeMeshWithBone(bone.Children[0].Mesh, false);

                        Matrix4D m = bone.Transform;
                        m.M31          = bone.Children[0].Transform.M31;
                        m.M32          = bone.Children[0].Transform.M32;
                        m.M33          = bone.Children[0].Transform.M33;
                        bone.Transform = m;

                        model.SetTransform(Matrix4D.Identity, bone.Children[0].Index);
                    }
                    break;

                case "FLWHEEL":
                    scale = bone.CombinedTransform.ExtractTranslation().Y / 0.35f;

                    bone.Name = "Wheel_FL";
                    model.ClearMesh(bone.Index);
                    model.SetTransform(Matrix4D.CreateScale(scale) * Matrix4D.CreateRotationY(Maths.DegreesToRadians(180)), bone.Index);
                    break;

                case "FRWHEEL":
                    scale = bone.CombinedTransform.ExtractTranslation().Y / 0.35f;

                    bone.Name = "Wheel_FR";
                    model.ClearMesh(bone.Index);
                    model.SetTransform(Matrix4D.CreateScale(scale), bone.Index);
                    break;

                case "RLWHEEL":
                case "RRWHEEL":
                    string suffix = bone.Name.ToUpper().Substring(0, 2);

                    bone.Name = "Hub_" + suffix;

                    if (bone.Transform.ExtractTranslation() == Vector3.Zero)
                    {
                        ModelManipulator.MungeMeshWithBone(bone.Mesh, false);
                    }
                    model.ClearMesh(bone.Index);

                    scale = bone.CombinedTransform.ExtractTranslation().Y / 0.35f;

                    int newBone = model.AddMesh(null, bone.Index);
                    model.SetName("Wheel_" + suffix, newBone);
                    model.SetTransform(Matrix4D.CreateScale(scale) * (suffix == "RL" ? Matrix4D.CreateRotationY(Maths.DegreesToRadians(180)) : Matrix4D.Identity), newBone);
                    break;

                case "DRIVER":
                    bone.Name = "Dryver";
                    goto default;

                default:
                    if (bone.Type == BoneType.Mesh)
                    {
                        ModelManipulator.MungeMeshWithBone(bone.Mesh, false);
                    }
                    break;
                }
            }

            SceneManager.Current.UpdateProgress("Processing complete!");

            SceneManager.Current.SetCoordinateSystem(CoordinateSystem.LeftHanded);

            SceneManager.Current.Change(ChangeType.Munge, ChangeContext.Model, -1);

            SceneManager.Current.SetContext("Carmageddon Max Damage", ContextMode.Car);
        }
Ejemplo n.º 25
0
        public static void FlipFaces(ModelBoneCollection bones, bool bApplyToHierarchy = false)
        {
            var processed = new List<string>();

            foreach (var bone in bones)
            {
                if (bone.Type == BoneType.Mesh && bone.Mesh != null && !processed.Contains(bone.Mesh.Name))
                {
                    var mesh = bone.Mesh;

                    foreach (var meshpart in mesh.MeshParts)
                    {
                        for (int i = 0; i < meshpart.IndexBuffer.Data.Count; i += 3)
                        {
                            meshpart.IndexBuffer.SwapIndices(i + 1, i + 2);
                        }

                        meshpart.IndexBuffer.Initialise();
                    }

                    processed.Add(mesh.Name);
                }
            }
        }