Example #1
0
        public static ModelData Load(Stream stream)
        {
            ModelData modelData = new ModelData();

            Collada.ColladaRoot colladaRoot = new Collada.ColladaRoot(XElement.Load(stream));
            if (colladaRoot.Scene.VisualScene.Nodes.Count > 1)
            {
                ModelBoneData modelBoneData = new ModelBoneData();
                modelData.Bones.Add(modelBoneData);
                modelBoneData.ParentBoneIndex = -1;
                modelBoneData.Name            = string.Empty;
                modelBoneData.Transform       = Matrix.Identity;
                using (List <Collada.ColladaNode> .Enumerator enumerator = colladaRoot.Scene.VisualScene.Nodes.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        Collada.ColladaNode node = enumerator.Current;
                        Collada.LoadNode(modelData, modelBoneData, node, Matrix.CreateScale(colladaRoot.Asset.Meter));
                    }
                    goto IL_104;
                }
            }
            foreach (Collada.ColladaNode node2 in colladaRoot.Scene.VisualScene.Nodes)
            {
                Collada.LoadNode(modelData, null, node2, Matrix.CreateScale(colladaRoot.Asset.Meter));
            }
IL_104:
            foreach (ModelBuffersData modelBuffersData in modelData.Buffers)
            {
                Collada.IndexVertices(modelBuffersData.VertexDeclaration.VertexStride, modelBuffersData.Vertices, out modelBuffersData.Vertices, out modelBuffersData.Indices);
            }
            return(modelData);
        }
Example #2
0
        public ModelExporter(ModelData modelData)
        {
            this.document = new XDocument(new XDeclaration("1.0", "UTF-8", null), new object[0]);
            this.root     = new XElement(this.colladaNS + "COLLADA", new object[]
            {
                new XAttribute(XNamespace.Xmlns + "xsi", this.instanceNS),
                new XAttribute("version", "1.4.1"),
                new XElement(this.colladaNS + "asset", new object[]
                {
                    new XElement(this.colladaNS + "contributor", new object[]
                    {
                        new XElement(this.colladaNS + "author", "Survivalcraft Moder"),
                        new XElement(this.colladaNS + "authoring_tool", "Engine 0.0.0")
                    }),
                    new XElement(this.colladaNS + "created", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:00")),
                    new XElement(this.colladaNS + "modified", DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:00")),
                    new XElement(this.colladaNS + "up_axis", "Y_UP")
                }),
                new XElement(this.colladaNS + "library_images"),
                new XElement(this.colladaNS + "library_effects"),
                new XElement(this.colladaNS + "library_materials"),
                new XElement(this.colladaNS + "library_geometries"),
                new XElement(this.colladaNS + "library_controllers"),
                new XElement(this.colladaNS + "library_visual_scenes"),
                new XElement(this.colladaNS + "scene")
            });
            this.visualScene = this.InitVisualScene("Scene");
            this.document.Add(this.root);

            XElement[] array = new XElement[modelData.Bones.Count];
            for (int i = 0; i < array.Length; i++)
            {
                ModelBoneData modelBoneData = modelData.Bones[i];
                array[i] = this.GetNode(modelBoneData.Name, modelBoneData.Transform);
            }
            for (int j = 0; j < array.Length; j++)
            {
                ModelBoneData modelBoneData2 = modelData.Bones[j];
                if (modelBoneData2.ParentBoneIndex != -1)
                {
                    array[modelBoneData2.ParentBoneIndex].Add(array[j]);
                }
                else
                {
                    this.visualScene.Add(array[j]);
                }
            }
            foreach (ModelMeshData modelMeshData in modelData.Meshes)
            {
                XElement geometry = this.GetGeometry(modelData, modelMeshData);
                array[modelMeshData.ParentBoneIndex].Add(this.GetGeometryInstance(modelMeshData.Name, geometry));
            }
        }
Example #3
0
        private static ModelMeshData LoadGeometry(ModelData data, ModelBoneData parentBoneData, Collada.ColladaGeometry geometry)
        {
            ModelMeshData modelMeshData = new ModelMeshData();

            data.Meshes.Add(modelMeshData);
            modelMeshData.Name            = parentBoneData.Name;
            modelMeshData.ParentBoneIndex = data.Bones.IndexOf(parentBoneData);
            bool flag = false;

            foreach (Collada.ColladaPolygons polygons in geometry.Mesh.Polygons)
            {
                ModelMeshPartData modelMeshPartData = Collada.LoadPolygons(data, polygons);
                modelMeshData.MeshParts.Add(modelMeshPartData);
                modelMeshData.BoundingBox = (flag ? BoundingBox.Union(modelMeshData.BoundingBox, modelMeshPartData.BoundingBox) : modelMeshPartData.BoundingBox);
                flag = true;
            }
            return(modelMeshData);
        }
Example #4
0
        private static ModelBoneData LoadNode(ModelData data, ModelBoneData parentBoneData, Collada.ColladaNode node, Matrix transform)
        {
            ModelBoneData modelBoneData = new ModelBoneData();

            data.Bones.Add(modelBoneData);
            modelBoneData.ParentBoneIndex = ((parentBoneData != null) ? data.Bones.IndexOf(parentBoneData) : -1);
            modelBoneData.Name            = node.Name;
            modelBoneData.Transform       = node.Transform * transform;
            foreach (Collada.ColladaNode node2 in node.Nodes)
            {
                Collada.LoadNode(data, modelBoneData, node2, Matrix.Identity);
            }
            foreach (Collada.ColladaGeometry geometry in node.Geometries)
            {
                Collada.LoadGeometry(data, modelBoneData, geometry);
            }
            return(modelBoneData);
        }
Example #5
0
        public ModelData(Stream stream)
        {
            BinaryReader binaryReader = new BinaryReader(stream, Encoding.UTF8);

            binaryReader.ReadBoolean();
            this.Bones.Capacity = binaryReader.ReadInt32();
            for (int i = 0; i < this.Bones.Capacity; i++)
            {
                ModelBoneData modelBoneData = new ModelBoneData();
                this.Bones.Add(modelBoneData);
                modelBoneData.ParentBoneIndex = binaryReader.ReadInt32();
                modelBoneData.Name            = binaryReader.ReadString();
                modelBoneData.Transform       = new Matrix(
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle(),
                    binaryReader.ReadSingle());
            }
            this.Meshes.Capacity = binaryReader.ReadInt32();
            for (int j = 0; j < this.Meshes.Capacity; j++)
            {
                ModelMeshData modelMeshData = new ModelMeshData();
                this.Meshes.Add(modelMeshData);
                modelMeshData.ParentBoneIndex    = binaryReader.ReadInt32();
                modelMeshData.Name               = binaryReader.ReadString();
                modelMeshData.MeshParts.Capacity = binaryReader.ReadInt32();
                modelMeshData.BoundingBox        = new BoundingBox(
                    new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()),
                    new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()));
                for (int k = 0; k < modelMeshData.MeshParts.Capacity; k++)
                {
                    ModelMeshPartData modelMeshPartData = new ModelMeshPartData();
                    modelMeshData.MeshParts.Add(modelMeshPartData);
                    modelMeshPartData.BuffersDataIndex = binaryReader.ReadInt32();
                    modelMeshPartData.StartIndex       = binaryReader.ReadInt32();
                    modelMeshPartData.IndicesCount     = binaryReader.ReadInt32();
                    modelMeshPartData.BoundingBox      = new BoundingBox(
                        new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()),
                        new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle()));
                }
            }
            this.Buffers.Capacity = binaryReader.ReadInt32();
            for (int l = 0; l < this.Buffers.Capacity; l++)
            {
                ModelBuffersData modelBuffersData = new ModelBuffersData();
                this.Buffers.Add(modelBuffersData);
                VertexElement[] array = new VertexElement[binaryReader.ReadInt32()];
                for (int m = 0; m < array.Length; m++)
                {
                    array[m] = new VertexElement(binaryReader.ReadInt32(), (VertexElementFormat)binaryReader.ReadInt32(), binaryReader.ReadString());
                }
                modelBuffersData.VertexDeclaration = new VertexDeclaration(array);
                modelBuffersData.Vertices          = binaryReader.ReadBytes(binaryReader.ReadInt32());
                modelBuffersData.Indices           = binaryReader.ReadBytes(binaryReader.ReadInt32());
            }
        }
Example #6
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);
        }