Пример #1
0
        public override void Read(FileReader reader, HsfFile header)
        {
            List <ComponentData> Components = reader.ReadMultipleStructs <ComponentData>(this.Count);
            long pos = reader.Position;

            var ExtOffset = pos;

            foreach (var att in Components)
            {
                ExtOffset += att.DataCount * 48;
            }
            foreach (var comp in Components)
            {
                List <PrimitiveObject> primatives = new List <PrimitiveObject>();
                reader.SeekBegin(pos + comp.DataOffset);
                for (int i = 0; i < comp.DataCount; i++)
                {
                    var prim = new PrimitiveObject();
                    primatives.Add(prim);

                    prim.Type          = (PrimitiveType)reader.ReadUInt16();
                    prim.Flags         = reader.ReadUInt16();
                    prim.MaterialIndex = prim.Flags & 0xFFF;
                    prim.FlagValue     = prim.Flags >> 12;

                    int primCount = 3;
                    if (prim.Type == PrimitiveType.Triangle || prim.Type == PrimitiveType.Quad)
                    {
                        primCount = 4;
                    }

                    prim.Vertices = reader.ReadMultipleStructs <VertexGroup>(primCount).ToArray();
                    if (prim.Type == PrimitiveType.TriangleStrip)
                    {
                        primCount = reader.ReadInt32();
                        var offset = reader.ReadUInt32();
                        var temp   = reader.Position;
                        reader.Position = ExtOffset + offset * 8;

                        var verts = reader.ReadMultipleStructs <VertexGroup>(primCount).ToArray();
                        reader.Position = temp;

                        prim.TriCount = prim.Vertices.Length;
                        var newVert = new VertexGroup[prim.Vertices.Length + primCount + 1];
                        //Copy the first 3 group to the full vertex group
                        Array.Copy(prim.Vertices, 0, newVert, 0, prim.Vertices.Length);
                        //Make the 4th group set as the second group
                        newVert[3] = newVert[1];
                        //Copy the rest of the groups in the strip
                        Array.Copy(verts, 0, newVert, prim.Vertices.Length + 1, verts.Length);
                        prim.Vertices = newVert;
                    }
                    prim.NbtData = reader.ReadVec3();
                }

                header.AddPrimitiveComponent(Components.IndexOf(comp), primatives);
            }
        }
Пример #2
0
        public STVertex ToGenericVertex(Mesh mesh, VertexGroup group, STSkeleton skeleton)
        {
            List <int>   boneIndices = new List <int>();
            List <float> boneWeights = new List <float>();

            Vector3 position = Vector3.Zero;
            Vector3 normal   = Vector3.Zero;
            Vector2 uv0      = Vector2.Zero;
            Vector4 color    = Vector4.One;

            if (mesh.Positions.Count > group.PositionIndex)
            {
                position = mesh.Positions[group.PositionIndex];
            }
            if (mesh.Normals.Count > group.NormalIndex && group.NormalIndex != -1)
            {
                normal = mesh.Normals[group.NormalIndex];
            }
            if (mesh.TexCoords.Count > group.UVIndex && group.UVIndex != -1)
            {
                uv0 = mesh.TexCoords[group.UVIndex];
            }
            if (mesh.Colors.Count > group.ColorIndex && group.ColorIndex != -1)
            {
                color = mesh.Colors[group.ColorIndex];
            }

            foreach (var msh in Header.ObjectData.Meshes)
            {
                if (msh.ObjectParent == mesh.ObjectData)
                {
                    if (msh.Positions.Count > group.PositionIndex)
                    {
                        position = msh.Positions[group.PositionIndex];
                    }
                    if (msh.Normals.Count > group.NormalIndex)
                    {
                        normal = msh.Normals[group.NormalIndex];
                    }
                }
            }

            position *= HSF_Renderer.PreviewScale;

            int nodeIndex = Header.ObjectData.Objects.IndexOf(mesh.ObjectData);

            if (nodeIndex != -1)
            {
                boneIndices.Clear();
                boneWeights.Clear();
                boneIndices.Add(nodeIndex);
                boneWeights.Add(1);

                position = Vector3.TransformPosition(position, skeleton.Bones[nodeIndex].Transform);
            }

            if (mesh.HasRigging)
            {
                foreach (var singleBind in mesh.RiggingInfo.SingleBinds)
                {
                    if (group.PositionIndex >= singleBind.PositionIndex && group.PositionIndex < singleBind.PositionIndex + singleBind.PositionCount)
                    {
                        boneIndices.Clear();
                        boneWeights.Clear();

                        boneIndices.Add(singleBind.BoneIndex);
                        boneWeights.Add(1);
                        break;
                    }
                }

                int mbOffset = 0;
                foreach (var multiBind in mesh.RiggingInfo.DoubleBinds)
                {
                    for (int i = mbOffset; i < mbOffset + multiBind.Count; i++)
                    {
                        var w = mesh.RiggingInfo.DoubleWeights[i];
                        if (group.PositionIndex >= w.PositionIndex && group.PositionIndex < w.PositionIndex + w.PositionCount)
                        {
                            boneIndices.Clear();
                            boneWeights.Clear();

                            boneIndices.Add(multiBind.Bone1);
                            boneIndices.Add(multiBind.Bone2);
                            boneWeights.Add(w.Weight);
                            boneWeights.Add(1 - w.Weight);
                            break;
                        }
                    }
                    mbOffset += multiBind.Count;
                }

                mbOffset = 0;
                foreach (var multiBind in mesh.RiggingInfo.MultiBinds)
                {
                    if (group.PositionIndex >= multiBind.PositionIndex && group.PositionIndex < multiBind.PositionIndex + multiBind.PositionCount)
                    {
                        boneIndices.Clear();
                        boneWeights.Clear();

                        Vector4 indices = new Vector4(0);
                        Vector4 weight  = new Vector4(0);
                        for (int i = mbOffset; i < mbOffset + multiBind.Count; i++)
                        {
                            indices[i - mbOffset] = mesh.RiggingInfo.MultiWeights[i].BoneIndex;
                            weight[i - mbOffset]  = mesh.RiggingInfo.MultiWeights[i].Weight;
                        }

                        if (weight.X != 0)
                        {
                            boneIndices.Add((int)indices.X);
                            boneWeights.Add(weight.X);
                        }
                        if (weight.Y != 0)
                        {
                            boneIndices.Add((int)indices.Y);
                            boneWeights.Add(weight.Y);
                        }
                        if (weight.Z != 0)
                        {
                            boneIndices.Add((int)indices.Z);
                            boneWeights.Add(weight.Z);
                        }
                        if (weight.W != 0)
                        {
                            boneIndices.Add((int)indices.W);
                            boneWeights.Add(weight.W);
                        }
                        break;
                    }
                    mbOffset += multiBind.Count;
                }
            }

            return(new STVertex()
            {
                Position = position,
                Colors = new Vector4[] { color },
                Normal = normal,
                BoneIndices = boneIndices,
                BoneWeights = boneWeights,
                TexCoords = new Vector2[] { uv0 },
            });
        }