Beispiel #1
0
        private GenericVertex SingleBind(GenericVertex vert, GenericSkeleton s)
        {
            Vector3 p = Vector3.Zero;
            Vector3 n = Vector3.Zero;

            for (int i = 0; i < 1; i++)
            {
                if (vert.Weights[i] > 0)
                {
                    n += Vector3.TransformNormal(vert.Nrm, s.GetWorldTransform((int)vert.Bones[i]));   // * vert.Weights[i];
                    p += Vector3.TransformPosition(vert.Pos, s.GetWorldTransform((int)vert.Bones[i])); // * vert.Weights[i];
                }
            }

            vert.Pos = p;
            vert.Nrm = n;
            return(vert);
        }
Beispiel #2
0
        private List <GenericVertex> ToGenericVertex(GXVertex[] InVerts, List <HSD_JOBJ> BoneList, List <HSD_JOBJWeight> WeightList, HSD_JOBJ parent)
        {
            var finalList = new List <GenericVertex>(InVerts.Length);

            var transform = skeleton.GetWorldTransform(skeleton.Bones[jobjToIndex[parent]]);

            foreach (var inVert in InVerts)
            {
                GenericVertex vertex = new GenericVertex();
                vertex.Pos = Vector3.TransformPosition(new Vector3(inVert.Pos.X, inVert.Pos.Y, inVert.Pos.Z), transform);
                vertex.Nrm = Vector3.TransformNormal(new Vector3(inVert.Nrm.X, inVert.Nrm.Y, inVert.Nrm.Z), transform);
                vertex.UV0 = new Vector2(inVert.TEX0.X, inVert.TEX0.Y);
                if (inVert.Clr0.A != 0 || inVert.Clr0.R != 0 || inVert.Clr0.G != 0 || inVert.Clr0.B != 0)
                {
                    vertex.Clr = new Vector4(inVert.Clr0.R, inVert.Clr0.G, inVert.Clr0.B, inVert.Clr0.A);
                }

                vertex.Bones   = new Vector4(jobjToIndex[parent], 0, 0, 0);
                vertex.Weights = new Vector4(1, 0, 0, 0);

                if (WeightList != null)
                {
                    var weightList = WeightList[inVert.PMXID / 3];
                    // single bind fix
                    if (weightList != null && weightList.JOBJs.Count == 1)
                    {
                        vertex.Pos = Vector3.TransformPosition(vertex.Pos, skeleton.GetWorldTransform(skeleton.Bones[jobjToIndex[weightList.JOBJs[0]]]));
                        vertex.Nrm = Vector3.TransformNormal(vertex.Nrm, skeleton.GetWorldTransform(skeleton.Bones[jobjToIndex[weightList.JOBJs[0]]]));
                    }

                    //Bone Weights
                    for (int i = 0; i < weightList.Weights.Count; i++)
                    {
                        vertex.Bones[i]   = jobjToIndex[weightList.JOBJs[i]];
                        vertex.Weights[i] = weightList.Weights[i];
                    }
                }

                finalList.Add(vertex);
            }

            return(finalList);
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="skel"></param>
        /// <param name="weights"></param>
        /// <param name="bones"></param>
        /// <param name="nunoBoneToBone"></param>
        /// <param name="normal"></param>
        /// <returns></returns>
        private Vector3 Weight(Vector3 pos, GenericSkeleton skel, Vector4 weights, Vector4 bones, Dictionary <int, int> nunoBoneToBone, bool normal = false)
        {
            Vector3 temp = Vector3.Zero;

            if (normal)
            {
                temp += Vector3.TransformNormal(pos, skel.GetWorldTransform(nunoBoneToBone[(int)bones.X])) * weights.X;
                temp += Vector3.TransformNormal(pos, skel.GetWorldTransform(nunoBoneToBone[(int)bones.Y])) * weights.Y;
                temp += Vector3.TransformNormal(pos, skel.GetWorldTransform(nunoBoneToBone[(int)bones.Z])) * weights.Z;
                temp += Vector3.TransformNormal(pos, skel.GetWorldTransform(nunoBoneToBone[(int)bones.W])) * weights.W;
            }
            else
            {
                temp += Vector3.TransformPosition(pos, skel.GetWorldTransform(nunoBoneToBone[(int)bones.X])) * weights.X;
                temp += Vector3.TransformPosition(pos, skel.GetWorldTransform(nunoBoneToBone[(int)bones.Y])) * weights.Y;
                temp += Vector3.TransformPosition(pos, skel.GetWorldTransform(nunoBoneToBone[(int)bones.Z])) * weights.Z;
                temp += Vector3.TransformPosition(pos, skel.GetWorldTransform(nunoBoneToBone[(int)bones.W])) * weights.W;
            }
            return(temp);
        }
Beispiel #4
0
        private void RigMe(Vector3 position, GenericSkeleton skel, Dictionary <int, int> nuno, out Vector4 bone, out Vector4 weight)
        {
            weight = new Vector4();
            bone   = new Vector4();

            foreach (var v in nuno)
            {
                var t = skel.GetWorldTransform(v.Value);
                var p = Vector3.TransformPosition(Vector3.Zero, t);

                var dis = Vector3.Distance(position, p);
                for (int i = 0; i < 4; i++)
                {
                    if (dis < weight[i] || weight[i] == 0)
                    {
                        weight[i] = dis;
                        bone[i]   = v.Value;
                        break;
                    }
                }
            }
            weight = weight.Normalized();
        }
Beispiel #5
0
        public void Open(FileItem File)
        {
            var Data = File.GetFileBinary();

            byte[] DecompressedData = null;

            /*using (DataReader reader = new DataReader(new MemoryStream(Data)))
             * {
             *  reader.BigEndian = true;
             *  reader.Seek(0x14); // skip header stuff
             *  ushort CompressedChunkCount = reader.ReadUInt16();
             *
             *  ushort[] CompressedSize = new ushort[CompressedChunkCount];
             *  for(int i = 0; i < CompressedChunkCount; i++)
             *  {
             *      CompressedSize[i] = reader.ReadUInt16();
             *      reader.ReadUInt16();
             *  }
             *
             *  MemoryStream data = new MemoryStream();
             *  using (BinaryWriter o = new BinaryWriter(data))
             *  {
             *      foreach (ushort compsize in CompressedSize)
             *      {
             *          o.Write(Metanoia.Tools.Decompress.ZLIB(reader.ReadBytes(compsize)));
             *      }
             *      DecompressedData = data.GetBuffer();
             *      data.Close();
             *      System.IO.File.WriteAllBytes("LBP\\Decomp.bin", DecompressedData);
             *  }
             *
             *  reader.PrintPosition();
             * }*/

            using (DataReader reader = new DataReader(new MemoryStream(File.GetFileBinary())))
            {
                reader.BigEndian = true;

                // bit pack hell
                reader.Seek(0x10);
                int bufferSizeCount = reader.ReadInt32();
                int uvchannelCount  = reader.ReadInt32();
                int morphCount      = reader.ReadInt32();

                Console.WriteLine(bufferSizeCount);

                reader.Seek(0x220);
                Console.WriteLine(reader.ReadSingle() + " " + reader.ReadSingle() + " " + reader.ReadInt32());
                Console.WriteLine(reader.ReadSingle() + " " + reader.ReadSingle() + " " + reader.ReadSingle() + " " + reader.ReadInt32());

                int[] bufferOffsets = new int[bufferSizeCount];
                for (int i = 0; i < bufferSizeCount; i++)
                {
                    bufferOffsets[i] = reader.ReadInt32();
                }

                var vertexBufferSize = reader.ReadUInt32();
                var start            = reader.Position;
                PositionBuffer = new Vector4[bufferOffsets[0] / 0x10];
                for (int i = 0; i < PositionBuffer.Length; i++)
                {
                    PositionBuffer[i] = new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                }
                reader.Seek(start + vertexBufferSize);

                var uvbufferSize = reader.ReadUInt32();
                UV0Buffer = new Vector2[uvbufferSize / 0x18];
                UV1Buffer = new Vector2[uvbufferSize / 0x18];
                UV2Buffer = new Vector2[uvbufferSize / 0x18];
                for (int i = 0; i < uvbufferSize / 0x18; i++)
                {
                    if (uvchannelCount > 0)
                    {
                        UV0Buffer[i] = new Vector2(reader.ReadSingle(), reader.ReadSingle());
                    }
                    if (uvchannelCount > 1)
                    {
                        UV1Buffer[i] = new Vector2(reader.ReadSingle(), reader.ReadSingle());
                    }
                    if (uvchannelCount > 2)
                    {
                        UV2Buffer[i] = new Vector2(reader.ReadSingle(), reader.ReadSingle());
                    }
                }

                Console.WriteLine(PositionBuffer.Length + " " + UV0Buffer.Length);

                var trianglebufferSize = reader.ReadInt32();
                IndexBuffer = new short[trianglebufferSize / 2];
                for (int i = 0; i < IndexBuffer.Length; i++)
                {
                    IndexBuffer[i] = reader.ReadInt16();
                }

                uint boneCount = reader.ReadUInt32();
                reader.Skip(0x22 * boneCount);

                uint      boneCount2  = reader.ReadUInt32();
                var       boneStart   = reader.Position;
                Matrix4[] wtransforms = new Matrix4[boneCount2];
                Matrix4[] itransforms = new Matrix4[boneCount2];
                for (int i = 0; i < boneCount2; i++)
                {
                    reader.Seek((uint)(boneStart + (i * 0x114))); // 0xD4 for older versions
                    var bone = new GenericBone();
                    bone.Name = reader.ReadString((uint)(boneStart + (i * 0x114)), -1);
                    reader.Skip(0x28);
                    bone.ParentIndex = reader.ReadInt32();
                    reader.Skip(0x8);
                    wtransforms[i] = new Matrix4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
                                                 reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
                                                 reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
                                                 reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                    itransforms[i] = new Matrix4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
                                                 reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
                                                 reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(),
                                                 reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                    if (bone.ParentIndex != -1 && bone.ParentIndex != 0)
                    {
                        var par          = skeleton.GetWorldTransform(bone.ParentIndex).Inverted();
                        var reltransform = wtransforms[i] * par;
                        bone.Transform          = reltransform;
                        bone.QuaternionRotation = wtransforms[i].ExtractRotation() * par.ExtractRotation();
                    }
                    else
                    if (i != 0)
                    {
                        bone.Transform = wtransforms[i];
                    }
                    else
                    {
                        bone.Transform = Matrix4.Identity;
                    }

                    skeleton.Bones.Add(bone);
                }

                reader.Seek(boneStart + (boneCount2 * 0x114));

                var boneIndexTableCount = reader.ReadUInt32();
                reader.Skip(boneIndexTableCount * 2);

                var bonecountTableCount = reader.ReadUInt32();
                reader.Skip(bonecountTableCount);

                var boneIndexTableCount2 = reader.ReadUInt32();
                reader.Skip(boneIndexTableCount2 * 2);

                reader.Skip(5);
                reader.Skip(reader.ReadUInt32() * 0x10); // vector 4s?

                reader.Skip(reader.ReadUInt32() * 0x40); // some matrices?

                reader.Skip(reader.ReadUInt32() * 0x4);  // float table? maybe weights?

                int clusterStringCount = reader.ReadInt32();
                var clusterstringstart = reader.Position;
                for (int i = 0; i < clusterStringCount; i++)
                {
                }
                reader.Seek(clusterstringstart + 0x20 * (uint)clusterStringCount);

                reader.Skip(reader.ReadUInt32() * 0x8); // ?? may have something to do with weighting

                reader.Skip(reader.ReadUInt32() * 0x4); // ?? may have something to do with weighting

                reader.Skip(reader.ReadUInt32() * 0x4); // ?? another float table??

                reader.PrintPosition();
            }
        }
Beispiel #6
0
        public void Open(FileItem File)
        {
            var igb = new IGBFile(File.GetFileBinary());

            var igbSkel = new IGSkeleton(igb);

            Matrix4[] transforms = new Matrix4[igbSkel.Bones.Count];
            Matrix4[] inverts = new Matrix4[igbSkel.Bones.Count];
            int boneIndex = 0;
            foreach (var v in igbSkel.Bones)
            {
                if (v == null)
                    continue;
                GenericBone b = new GenericBone();
                b.Name = v.Name;
                b.ParentIndex = v.ParentIndex;
                transforms[boneIndex] = new Matrix4(
                    v.WorldInverseMatrix.M11, v.WorldInverseMatrix.M12, v.WorldInverseMatrix.M13, v.WorldInverseMatrix.M14,
                    v.WorldInverseMatrix.M21, v.WorldInverseMatrix.M22, v.WorldInverseMatrix.M23, v.WorldInverseMatrix.M24,
                    v.WorldInverseMatrix.M31, v.WorldInverseMatrix.M32, v.WorldInverseMatrix.M33, v.WorldInverseMatrix.M34,
                    v.WorldInverseMatrix.M41, v.WorldInverseMatrix.M42, v.WorldInverseMatrix.M43, v.WorldInverseMatrix.M44);
                inverts[boneIndex] = transforms[boneIndex].Inverted();
                b.Transform = inverts[boneIndex];

                boneIndex++;
                Skeleton.Bones.Add(b);
            }
            
            foreach (var b in Skeleton.Bones)
            {
                if (b.ParentIndex != -1)
                {
                    b.Transform = inverts[b.ParentIndex] * transforms[Skeleton.Bones.IndexOf(b)];
                    b.Transform = b.Transform.Inverted();
                    var position = igbSkel.Bones[Skeleton.Bones.IndexOf(b)].Position;
                    b.Position = new Vector3(position.X, position.Y, position.Z);
                }
            }

            var vertexBuffers = IGVertexAccessor.ToDivaModels(igb);

            Model.Skeleton = Skeleton;

            Console.WriteLine(vertexBuffers.Count);
            foreach (var model in vertexBuffers)
            {
                GenericMesh m = new GenericMesh();
                m.Name = model.Name;
                if (m.Name.Equals(""))
                    m.Name = "Mesh_" + vertexBuffers.IndexOf(model);
                Console.WriteLine(m.Name + " " + !(model.Texture==null));

                if(model.Texture != null)
                {
                    GenericTexture t = new GenericTexture();
                    t.Name = System.IO.Path.GetFileNameWithoutExtension(model.Texture.Name);
                    t.Width = (uint)model.Texture.Width;
                    t.Height = (uint)model.Texture.Height;
                    t.Mipmaps.Add(model.Texture.RGBA);

                    if(!Model.MaterialBank.ContainsKey(t.Name))
                        Model.TextureBank.Add(t.Name, t);

                    GenericMaterial mat = new GenericMaterial();
                    mat.TextureDiffuse = t.Name;

                    if (!Model.MaterialBank.ContainsKey(t.Name))
                        Model.MaterialBank.Add(t.Name, mat);

                    m.MaterialName = t.Name;
                }
                
                foreach (var mesh in model.Mesh)
                {
                    var vertices = ToGenericVertices(mesh.Vertices);
                    foreach (var dl in mesh.DisplayList)
                    {
                        if (dl.PrimitiveType == PrimType.Triangles)
                        {
                            foreach(var f in dl.Indices)
                                m.Vertices.Add(vertices[f]);
                        }
                        if (dl.PrimitiveType == PrimType.TriangleStrip)
                        {
                            var tempList = new List<GenericVertex>();
                            foreach (var f in dl.Indices)
                                tempList.Add(vertices[f]);
                            Tools.TriangleConverter.StripToList(tempList, out tempList);
                            m.Vertices.AddRange(tempList);
                        }
                    }
                }
                

                if(model.SingleBindBone != null && model.SingleBindBone != "")
                {
                    var singleBone = Skeleton.Bones.Find(e => e.Name.Equals(model.SingleBindBone));
                    var singleBindTransform = Skeleton.GetWorldTransform(singleBone);
                    var singleBindIndex = Skeleton.Bones.IndexOf(singleBone);
                    for (int i = 0; i < m.VertexCount;i++)
                    {
                        var vert = m.Vertices[i];
                        vert.Pos = Vector3.TransformPosition(vert.Pos, singleBindTransform);
                        vert.Nrm = Vector3.TransformNormal(vert.Nrm, singleBindTransform);
                        vert.Bones = new Vector4(singleBindIndex, 0, 0, 0);
                        vert.Weights = new Vector4(1, 0, 0, 0);
                        m.Vertices[i] = vert;
                    }
                }

                m.Optimize();

                Model.Meshes.Add(m);
            }
        }
Beispiel #7
0
        private GenericVertex GetVertex(MeshObject mesh, VertexGroup g, GenericSkeleton skeleton)
        {
            // Rigging
            Vector4 boneIndices = new Vector4(mesh.SingleBind, 0, 0, 0);
            Vector4 weight      = new Vector4(1, 0, 0, 0);

            var Position = Vector3.Zero;

            if (g.PositionIndex < mesh.Positions.Count)
            {
                Position = mesh.Positions[g.PositionIndex];
            }

            var bone = skeleton.Bones.Find(e => e.Name.Equals(mesh.Name));

            if (bone != null)
            {
                boneIndices = new Vector4(skeleton.Bones.IndexOf(bone), 0, 0, 0);
                Position    = Vector3.TransformPosition(Position, skeleton.GetWorldTransform(bone));
            }


            foreach (var singleBind in mesh.SingleBinds)
            {
                if (g.PositionIndex >= singleBind.PositionIndex && g.PositionIndex < singleBind.PositionIndex + singleBind.PositionCount)
                {
                    boneIndices = new Vector4(singleBind.BoneIndex, 0, 0, 0);
                    break;
                }
            }
            int mbOffset = 0;

            foreach (var multiBind in mesh.DoubleBinds)
            {
                for (int i = mbOffset; i < mbOffset + multiBind.Count; i++)
                {
                    var w = mesh.DoubleWeights[i];
                    if (g.PositionIndex >= w.PositionIndex && g.PositionIndex < w.PositionIndex + w.PositionCount)
                    {
                        boneIndices = new Vector4(multiBind.Bone1, multiBind.Bone2, 0, 0);
                        weight      = new Vector4(w.Weight, 1 - w.Weight, 0, 0);
                        break;
                    }
                }
                mbOffset += multiBind.Count;
            }

            mbOffset = 0;
            foreach (var multiBind in mesh.MultiBinds)
            {
                if (g.PositionIndex >= multiBind.PositionIndex && g.PositionIndex < multiBind.PositionIndex + multiBind.PositionCount)
                {
                    boneIndices = new Vector4(0);
                    weight      = new Vector4(0);
                    for (int i = mbOffset; i < mbOffset + multiBind.Count; i++)
                    {
                        boneIndices[i - mbOffset] = mesh.MultiWeights[i].BoneIndex;
                        weight[i - mbOffset]      = mesh.MultiWeights[i].Weight;
                    }
                    break;
                }
                mbOffset += multiBind.Count;
            }

            var uv = Vector2.Zero;

            var Normal = Vector3.Zero;
            var Color  = Vector4.One;

            if (mesh.Normals.Count > 0 && g.NormalIndex < mesh.Normals.Count)
            {
                Normal = mesh.Normals[g.NormalIndex];
            }

            if (mesh.Colors.Count > 0)
            {
                Normal = mesh.Colors[g.NormalIndex].Xyz;
            }

            if (g.UVIndex >= 0 && mesh.UVs.Count > 0 && g.UVIndex < mesh.UVs.Count)
            {
                uv = mesh.UVs[g.UVIndex];
            }

            return(new GenericVertex()
            {
                Pos = Position,
                Nrm = Normal, // TODO: single bind normal
                Clr = Color,
                UV0 = uv,
                Bones = boneIndices,
                Weights = weight
            });
        }