Example #1
0
        public DX11IndexedGeometry LoadFromMesh(Assimp.Mesh mesh, AssimpLoadInformation loadInfo, bool allowRawView = false)
        {
            uint[] inds = mesh.GetIndices();

            if (inds.Length > 0 && mesh.VertexCount > 0)
            {
                int vertexsize;
                var layout = mesh.InputLayout(loadInfo, out vertexsize);

                BoundingBox bb;
                DataStream  ds = mesh.LoadVertices(loadInfo, vertexsize, out bb);

                DX11IndexedGeometry geom = new DX11IndexedGeometry(device)
                {
                    HasBoundingBox = true,
                    BoundingBox    = bb,
                    IndexBuffer    = DX11IndexBuffer.CreateImmutable(device, inds, allowRawView),
                    InputLayout    = layout,
                    PrimitiveType  = "AssimpModel",
                    Tag            = null,
                    Topology       = SharpDX.Direct3D.PrimitiveTopology.TriangleList,
                    VertexBuffer   = DX11VertexBuffer.CreateImmutable(device, mesh.VertexCount, vertexsize, ds, allowRawView)
                };

                ds.Dispose();
                return(geom);
            }

            return(null);
        }
Example #2
0
        public List <DX11IndexedGeometry> LoadModelsFromFile(string path, AssimpLoadInformation loadInfo)
        {
            Assimp.AssimpImporter importer = new AssimpImporter();
            Scene scene = importer.ImportFile(path, PostProcessPreset.ConvertToLeftHanded | PostProcessPreset.TargetRealTimeQuality);

            List <DX11IndexedGeometry> result = new List <DX11IndexedGeometry>();

            for (int j = 0; j < scene.MeshCount; j++)
            {
                Assimp.Mesh mesh = scene.Meshes[j];

                if (mesh.HasFaces && mesh.HasVertices)
                {
                    result.Add(this.LoadFromMesh(mesh, loadInfo));
                }
            }
            return(result);
        }
Example #3
0
        public static DataStream LoadVertices(this Assimp.Mesh mesh, AssimpLoadInformation loadInfo, int vertexsize, out BoundingBox bb)
        {
            bb         = new BoundingBox();
            bb.Maximum = new Vector3(float.MinValue, float.MinValue, float.MinValue);
            bb.Minimum = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);

            DataStream ds = new DataStream(mesh.VertexCount * vertexsize, true, true);

            for (int i = 0; i < mesh.VertexCount; i++)
            {
                var vert = mesh.Vertices[i];
                ds.Write <Assimp.Vector3D>(vert);

                bb.Minimum.X = vert.X < bb.Minimum.X ? vert.X : bb.Minimum.X;
                bb.Minimum.Y = vert.Y < bb.Minimum.Y ? vert.Y : bb.Minimum.Y;
                bb.Minimum.Z = vert.Z < bb.Minimum.Z ? vert.Z : bb.Minimum.Z;

                bb.Maximum.X = vert.X > bb.Maximum.X ? vert.X : bb.Maximum.X;
                bb.Maximum.Y = vert.Y > bb.Maximum.Y ? vert.Y : bb.Maximum.X;
                bb.Maximum.Z = vert.Z > bb.Maximum.Z ? vert.Z : bb.Maximum.Z;

                if (mesh.HasNormals && loadInfo.Normals)
                {
                    ds.Write <Assimp.Vector3D>(mesh.Normals[i]);
                }

                if (mesh.HasTangentBasis && loadInfo.Tangents)
                {
                    ds.Write <Assimp.Vector3D>(mesh.Tangents[i]);
                    ds.Write <Assimp.Vector3D>(mesh.BiTangents[i]);
                }

                if (loadInfo.VertexColors)
                {
                    for (int j = 0; j < mesh.VertexColorChannelCount; j++)
                    {
                        ds.Write <Assimp.Color4D>(mesh.GetVertexColors(j)[i]);
                    }
                }

                if (loadInfo.TexCoords)
                {
                    for (int j = 0; j < mesh.TextureCoordsChannelCount; j++)
                    {
                        int numcomp = mesh.GetUVComponentCount(j);
                        if (numcomp == 1)
                        {
                            ds.Write <float>(mesh.GetTextureCoords(j)[i].X);
                        }
                        if (numcomp == 2)
                        {
                            ds.Write <float>(mesh.GetTextureCoords(j)[i].X);
                            ds.Write <float>(mesh.GetTextureCoords(j)[i].Y);
                        }
                        if (numcomp == 3)
                        {
                            ds.Write <Assimp.Vector3D>(mesh.GetTextureCoords(j)[i]);
                        }
                    }
                }
            }
            return(ds);
        }
Example #4
0
        public static InputElement[] InputLayout(this Assimp.Mesh mesh, AssimpLoadInformation loadInfo, out int vertexsize)
        {
            List <InputElement> result = new List <InputElement>();
            int offset = 0;

            result.Add(new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0));
            offset += 12;

            if (mesh.HasNormals && loadInfo.Normals)
            {
                result.Add(new InputElement("NORMAL", 0, Format.R32G32B32_Float, offset, 0));
                offset += 12;
            }

            if (mesh.HasTangentBasis && loadInfo.Tangents)
            {
                result.Add(new InputElement("TANGENT", 0, Format.R32G32B32_Float, offset, 0));
                offset += 12;
                result.Add(new InputElement("BINORMAL", 0, Format.R32G32B32_Float, offset, 0));
                offset += 12;
            }

            if (loadInfo.VertexColors)
            {
                for (int i = 0; i < mesh.VertexColorChannelCount; i++)
                {
                    result.Add(new InputElement("COLOR", i, Format.R32G32B32A32_Float, offset, 0));
                    offset += 16;
                }
            }


            if (loadInfo.TexCoords)
            {
                for (int i = 0; i < mesh.TextureCoordsChannelCount; i++)
                {
                    int    numcomp = mesh.GetUVComponentCount(i);
                    int    stride  = 4;
                    Format fmt     = Format.R32_Float;

                    if (numcomp == 2)
                    {
                        fmt    = Format.R32G32_Float;
                        stride = 8;
                    }
                    if (numcomp == 3)
                    {
                        fmt    = Format.R32G32B32_Float;
                        stride = 12;
                    }
                    result.Add(new InputElement("TEXCOORD", i, fmt, offset, 0));
                    offset += stride;
                }
            }

            if (mesh.HasBones && loadInfo.Bones)
            {
                result.Add(new InputElement("BLENDINDICES", 0, Format.R32G32B32A32_Float, offset, 0));
                offset += 16;
                result.Add(new InputElement("BLENDWEIGHT", 0, Format.R32G32B32A32_Float, offset, 0));
                offset += 16;
            }

            vertexsize = offset;
            return(result.ToArray());
        }