Beispiel #1
0
        private static void SCModelWriter(MemoryStream memoryStream, FileStream fileStream)
        {
            ModelData modelData = Collada.Load(fileStream);

            ModelContentWriter.Write(memoryStream, modelData, Vector3.One);
        }
Beispiel #2
0
        private static ModelMeshPartData LoadPolygons(ModelData data, Collada.ColladaPolygons polygons)
        {
            ModelMeshPartData modelMeshPartData = new ModelMeshPartData();
            int num = 0;
            Dictionary <VertexElement, Collada.ColladaInput> dictionary = new Dictionary <VertexElement, Collada.ColladaInput>();

            foreach (Collada.ColladaInput colladaInput in polygons.Inputs)
            {
                string str = (colladaInput.Set == 0) ? string.Empty : colladaInput.Set.ToString(CultureInfo.InvariantCulture);
                if (colladaInput.Semantic == "POSITION")
                {
                    dictionary[new VertexElement(num, VertexElementFormat.Vector3, "POSITION" + str)] = colladaInput;
                    num += 12;
                }
                else if (colladaInput.Semantic == "NORMAL")
                {
                    dictionary[new VertexElement(num, VertexElementFormat.Vector3, "NORMAL" + str)] = colladaInput;
                    num += 12;
                }
                else if (colladaInput.Semantic == "TEXCOORD")
                {
                    dictionary[new VertexElement(num, VertexElementFormat.Vector2, "TEXCOORD" + str)] = colladaInput;
                    num += 8;
                }
                else if (colladaInput.Semantic == "COLOR")
                {
                    dictionary[new VertexElement(num, VertexElementFormat.NormalizedByte4, "COLOR" + str)] = colladaInput;
                    num += 4;
                }
            }
            VertexDeclaration vertexDeclaration = new VertexDeclaration(Enumerable.ToArray <VertexElement>(dictionary.Keys));
            ModelBuffersData  modelBuffersData  = Enumerable.FirstOrDefault <ModelBuffersData>(data.Buffers, (ModelBuffersData vd) => vd.VertexDeclaration == vertexDeclaration);

            if (modelBuffersData == null)
            {
                modelBuffersData = new ModelBuffersData();
                data.Buffers.Add(modelBuffersData);
                modelBuffersData.VertexDeclaration = vertexDeclaration;
            }
            modelMeshPartData.BuffersDataIndex = data.Buffers.IndexOf(modelBuffersData);
            int        num2 = polygons.P.Count / polygons.Inputs.Count;
            List <int> list = new List <int>();

            if (polygons.VCount.Count == 0)
            {
                int num3 = 0;
                for (int i = 0; i < num2 / 3; i++)
                {
                    list.Add(num3);
                    list.Add(num3 + 2);
                    list.Add(num3 + 1);
                    num3 += 3;
                }
            }
            else
            {
                int num4 = 0;
                foreach (int num5 in polygons.VCount)
                {
                    if (num5 == 3)
                    {
                        list.Add(num4);
                        list.Add(num4 + 2);
                        list.Add(num4 + 1);
                        num4 += 3;
                    }
                    else
                    {
                        if (num5 != 4)
                        {
                            throw new NotSupportedException("Collada polygons with less than 3 or more than 4 vertices are not supported.");
                        }
                        list.Add(num4);
                        list.Add(num4 + 2);
                        list.Add(num4 + 1);
                        list.Add(num4 + 2);
                        list.Add(num4);
                        list.Add(num4 + 3);
                        num4 += 4;
                    }
                }
            }
            int vertexStride = modelBuffersData.VertexDeclaration.VertexStride;
            int num6         = modelBuffersData.Vertices.Length;

            modelBuffersData.Vertices = Collada.ExtendArray <byte>(modelBuffersData.Vertices, list.Count * vertexStride);
            using (BinaryWriter binaryWriter = new BinaryWriter(new MemoryStream(modelBuffersData.Vertices, num6, list.Count * vertexStride)))
            {
                bool flag = false;
                foreach (KeyValuePair <VertexElement, Collada.ColladaInput> keyValuePair in dictionary)
                {
                    VertexElement        key   = keyValuePair.Key;
                    Collada.ColladaInput value = keyValuePair.Value;
                    if (key.Semantic.StartsWith("POSITION"))
                    {
                        for (int j = 0; j < list.Count; j++)
                        {
                            float[] array  = value.Source.Accessor.Source.Array;
                            int     offset = value.Source.Accessor.Offset;
                            int     stride = value.Source.Accessor.Stride;
                            int     num7   = polygons.P[list[j] * polygons.Inputs.Count + value.Offset];
                            binaryWriter.BaseStream.Position = (long)(j * vertexStride + key.Offset);
                            float num8  = array[offset + stride * num7];
                            float num9  = array[offset + stride * num7 + 1];
                            float num10 = array[offset + stride * num7 + 2];
                            modelMeshPartData.BoundingBox = (flag ? BoundingBox.Union(modelMeshPartData.BoundingBox, new Vector3(num8, num9, num10)) : new BoundingBox(num8, num9, num10, num8, num9, num10));
                            flag = true;
                            binaryWriter.Write(num8);
                            binaryWriter.Write(num9);
                            binaryWriter.Write(num10);
                        }
                    }
                    else if (key.Semantic.StartsWith("NORMAL"))
                    {
                        for (int k = 0; k < list.Count; k++)
                        {
                            float[] array2  = value.Source.Accessor.Source.Array;
                            int     offset2 = value.Source.Accessor.Offset;
                            int     stride2 = value.Source.Accessor.Stride;
                            int     num11   = polygons.P[list[k] * polygons.Inputs.Count + value.Offset];
                            binaryWriter.BaseStream.Position = (long)(k * vertexStride + key.Offset);
                            float num12 = array2[offset2 + stride2 * num11];
                            float num13 = array2[offset2 + stride2 * num11 + 1];
                            float num14 = array2[offset2 + stride2 * num11 + 2];
                            float num15 = 1f / (float)Math.Sqrt(num12 * num12 + num13 * num13 + num14 * num14);
                            binaryWriter.Write(num15 * num12);
                            binaryWriter.Write(num15 * num13);
                            binaryWriter.Write(num15 * num14);
                        }
                    }
                    else if (key.Semantic.StartsWith("TEXCOORD"))
                    {
                        for (int l = 0; l < list.Count; l++)
                        {
                            float[] array3  = value.Source.Accessor.Source.Array;
                            int     offset3 = value.Source.Accessor.Offset;
                            int     stride3 = value.Source.Accessor.Stride;
                            int     num16   = polygons.P[list[l] * polygons.Inputs.Count + value.Offset];
                            binaryWriter.BaseStream.Position = (long)(l * vertexStride + key.Offset);
                            binaryWriter.Write(array3[offset3 + stride3 * num16]);
                            binaryWriter.Write(1f - array3[offset3 + stride3 * num16 + 1]);
                        }
                    }
                    else
                    {
                        if (!key.Semantic.StartsWith("COLOR"))
                        {
                            throw new Exception();
                        }
                        for (int m = 0; m < list.Count; m++)
                        {
                            float[] array4  = value.Source.Accessor.Source.Array;
                            int     offset4 = value.Source.Accessor.Offset;
                            int     stride4 = value.Source.Accessor.Stride;
                            int     num17   = polygons.P[list[m] * polygons.Inputs.Count + value.Offset];
                            binaryWriter.BaseStream.Position = (long)(m * vertexStride + key.Offset);
                            Color color = new Color(array4[offset4 + stride4 * num17], array4[offset4 + stride4 * num17 + 1], array4[offset4 + stride4 * num17 + 2], array4[offset4 + stride4 * num17 + 3]);
                            binaryWriter.Write(color.PackedValue);
                        }
                    }
                }
            }
            modelMeshPartData.StartIndex   = num6 / vertexStride;
            modelMeshPartData.IndicesCount = list.Count;
            return(modelMeshPartData);
        }
Beispiel #3
0
        private XElement GetGeometry(ModelData model, ModelMeshData data)
        {
            ModelMeshPartData modelMeshPartData = data.MeshParts[0];
            ModelBoneData     modelBoneData     = model.Bones[data.ParentBoneIndex];
            string            name             = data.Name;
            ModelBuffersData  modelBuffersData = model.Buffers[modelMeshPartData.BuffersDataIndex];
            int indicesCount = modelMeshPartData.IndicesCount;
            VertexDeclaration vertexDeclaration = modelBuffersData.VertexDeclaration;

            byte[] array = new byte[indicesCount * 32];
            using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(modelBuffersData.Indices)))
            {
                binaryReader.BaseStream.Position = (long)(modelMeshPartData.StartIndex * 2);
                for (int i = 0; i < indicesCount; i++)
                {
                    short num = binaryReader.ReadInt16();
                    Buffer.BlockCopy(modelBuffersData.Vertices, (int)(num * 32), array, i * 32, 32);
                }
            }
            List <Vector3> list  = new List <Vector3>();
            List <Vector3> list2 = new List <Vector3>();

            float[] array2 = new float[indicesCount * 2];
            int[]   array3 = new int[indicesCount * 3];
            using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(array)))
            {
                foreach (VertexElement vertexElement in vertexDeclaration.VertexElements)
                {
                    if (vertexElement.Semantic.StartsWith("POSITION"))
                    {
                        Vector3[] array4 = new Vector3[3];
                        for (int j = 0; j < indicesCount; j++)
                        {
                            binaryReader.BaseStream.Position = (long)(vertexDeclaration.VertexStride * j + vertexElement.Offset);
                            Vector3 vector = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
                            if (!list.Contains(vector))
                            {
                                list.Add(vector);
                            }
                            int num2 = j % 3;
                            array4[num2] = vector;
                            if (num2 == 2)
                            {
                                array3[j * 3 - 6] = list.IndexOf(array4[0]);
                                array3[j * 3 - 3] = list.IndexOf(array4[2]);
                                array3[j * 3]     = list.IndexOf(array4[1]);
                            }
                        }
                    }
                    else if (vertexElement.Semantic.StartsWith("NORMAL"))
                    {
                        Vector3[] array5 = new Vector3[3];
                        for (int k = 0; k < indicesCount; k++)
                        {
                            binaryReader.BaseStream.Position = (long)(vertexDeclaration.VertexStride * k + vertexElement.Offset);
                            Vector3 vector2 = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
                            if (!list2.Contains(vector2))
                            {
                                list2.Add(vector2);
                            }
                            int num3 = k % 3;
                            array5[num3] = vector2;
                            if (num3 == 2)
                            {
                                array3[k * 3 - 5] = list2.IndexOf(array5[0]);
                                array3[k * 3 - 2] = list2.IndexOf(array5[2]);
                                array3[k * 3 + 1] = list2.IndexOf(array5[1]);
                            }
                        }
                    }
                    else if (vertexElement.Semantic.StartsWith("TEXCOORD"))
                    {
                        for (int l = 0; l < indicesCount; l++)
                        {
                            binaryReader.BaseStream.Position = (long)(vertexDeclaration.VertexStride * l + vertexElement.Offset);
                            array2[l * 2]     = binaryReader.ReadSingle();
                            array2[l * 2 + 1] = 1f - binaryReader.ReadSingle();
                            if (l % 3 == 2)
                            {
                                array3[l * 3 - 4] = l - 2;
                                array3[l * 3 - 1] = l;
                                array3[l * 3 + 2] = l - 1;
                            }
                        }
                    }
                }
            }
            XName name2 = this.colladaNS + "mesh";

            object[] array6 = new object[5];
            XElement source = this.GetSourceArray(name, "-mesh-positions", string.Join(" ", list.ConvertAll <string>((Vector3 v) => string.Format("{0} {1} {2}", v.X.ToString("R"), v.Y.ToString("R"), v.Z.ToString("R")))), list.Count * 3, 3, this.XYZParam());

            array6[0] = source;
            XElement source2 = this.GetSourceArray(name, "-mesh-normals", string.Join(" ", list2.ConvertAll <string>((Vector3 v) => string.Format("{0} {1} {2}", v.X.ToString("R"), v.Y.ToString("R"), v.Z.ToString("R")))), list2.Count * 3, 3, this.XYZParam());

            array6[1] = source2;
            XElement source3 = this.GetSourceArray(name, "-mesh-map", string.Join(" ", from f in array2 select f.ToString("R")), array2.Length, 2, this.STParam());

            array6[2] = source3;
            XElement source4 = new XElement(this.colladaNS + "vertices", new object[]
            {
                new XAttribute("id", name + "-mesh-vertices"),
                this.GetInput("POSITION", source)
            });

            array6[3] = source4;
            array6[4] = new XElement(this.colladaNS + "triangles", new object[]
            {
                new XAttribute("count", indicesCount / 3),
                this.GetInput("VERTEX", source4, 0),
                this.GetInput("NORMAL", source2, 1),
                this.GetInput("TEXCOORD", source3, 2),
                new XElement(this.colladaNS + "p", string.Join <int>(" ", array3))
            });
            XElement xelement = new XElement(name2, array6);
            XElement result;

            this.root.Element(this.colladaNS + "library_geometries").Add(result = new XElement(this.colladaNS + "geometry", new object[]
            {
                new XAttribute("id", name + "-mesh"),
                new XAttribute("name", name),
                xelement
            }));
            return(result);
        }
Beispiel #4
0
        public static void WriteModel(Stream mainStream, Stream daeStream)
        {
            BinaryWriter binaryWriter = new BinaryWriter(mainStream, Encoding.UTF8, true);

            binaryWriter.Write(true);
            Matrix    m         = Matrix.CreateScale(new Vector3(1f, 1f, 1f));
            ModelData modelData = Collada.Load(daeStream);

            binaryWriter.Write(modelData.Bones.Count);
            foreach (ModelBoneData current in modelData.Bones)
            {
                Matrix matrix = (current.ParentBoneIndex < 0) ? (current.Transform * m) : current.Transform;
                binaryWriter.Write(current.ParentBoneIndex);
                binaryWriter.Write(current.Name);
                binaryWriter.Write(matrix.M11);
                binaryWriter.Write(matrix.M12);
                binaryWriter.Write(matrix.M13);
                binaryWriter.Write(matrix.M14);
                binaryWriter.Write(matrix.M21);
                binaryWriter.Write(matrix.M22);
                binaryWriter.Write(matrix.M23);
                binaryWriter.Write(matrix.M24);
                binaryWriter.Write(matrix.M31);
                binaryWriter.Write(matrix.M32);
                binaryWriter.Write(matrix.M33);
                binaryWriter.Write(matrix.M34);
                binaryWriter.Write(matrix.M41);
                binaryWriter.Write(matrix.M42);
                binaryWriter.Write(matrix.M43);
                binaryWriter.Write(matrix.M44);
            }
            binaryWriter.Write(modelData.Meshes.Count);
            foreach (ModelMeshData current2 in modelData.Meshes)
            {
                binaryWriter.Write(current2.ParentBoneIndex);
                binaryWriter.Write(current2.Name);
                binaryWriter.Write(current2.MeshParts.Count);
                binaryWriter.Write(current2.BoundingBox.Min.X);
                binaryWriter.Write(current2.BoundingBox.Min.Y);
                binaryWriter.Write(current2.BoundingBox.Min.Z);
                binaryWriter.Write(current2.BoundingBox.Max.X);
                binaryWriter.Write(current2.BoundingBox.Max.Y);
                binaryWriter.Write(current2.BoundingBox.Max.Z);
                foreach (ModelMeshPartData current3 in current2.MeshParts)
                {
                    binaryWriter.Write(current3.BuffersDataIndex);
                    binaryWriter.Write(current3.StartIndex);
                    binaryWriter.Write(current3.IndicesCount);
                    binaryWriter.Write(current3.BoundingBox.Min.X);
                    binaryWriter.Write(current3.BoundingBox.Min.Y);
                    binaryWriter.Write(current3.BoundingBox.Min.Z);
                    binaryWriter.Write(current3.BoundingBox.Max.X);
                    binaryWriter.Write(current3.BoundingBox.Max.Y);
                    binaryWriter.Write(current3.BoundingBox.Max.Z);
                }
            }
            binaryWriter.Write(modelData.Buffers.Count);
            foreach (ModelBuffersData current4 in modelData.Buffers)
            {
                binaryWriter.Write(current4.VertexDeclaration.VertexElements.Length);
                foreach (VertexElement current5 in current4.VertexDeclaration.VertexElements)
                {
                    binaryWriter.Write(current5.Offset);
                    binaryWriter.Write((int)current5.Format);
                    binaryWriter.Write(current5.Semantic);
                }
                binaryWriter.Write(current4.Vertices.Length);
                binaryWriter.Write(current4.Vertices);
                binaryWriter.Write(current4.Indices.Length);
                binaryWriter.Write(current4.Indices);
            }
        }