Example #1
0
        public ImageData LoadImage(string fileName, Func <string, Stream> getStream)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException("Must provide an image name.", nameof(fileName));
            }

            if (getStream is null)
            {
                throw new ArgumentNullException(nameof(getStream));
            }

            var type = (from t in MediaType.GuessByFileName(fileName)
                        where t is MediaType.Image
                        select t)
                       .FirstOrDefault();

            if (!decoders.ContainsKey(type))
            {
                throw new NotSupportedException($"Don't know how to decode image type {type}");
            }

            using var stream = getStream(fileName);
            return(decoders[type].Deserialize(stream));
        }
Example #2
0
        private void LoadMesh(GraphicsDevice device, int index)
        {
            var factory = device.ResourceFactory;
            var mesh    = meshes[index];
            var vertexBuffer
                  = vertexBuffers[index]
                  = factory.CreateBuffer(new BufferDescription((uint)(mesh.Vertices.Length * typeof(VertexPositionNormalTexture).Size()), BufferUsage.VertexBuffer));

            device.UpdateBuffer(vertexBuffer, 0, mesh.Vertices);

            var indexBuffer
                  = indexBuffers[index]
                  = factory.CreateBuffer(new BufferDescription((uint)(mesh.Indices.Length * typeof(ushort).Size()), BufferUsage.IndexBuffer));

            device.UpdateBuffer(indexBuffer, 0, mesh.Indices);

            var mapNames       = meshMapNames[index];
            var texturesToLoad = mapNames
                                 .Values
                                 .Distinct()
                                 .Where(path => !textureViews.ContainsKey(path))
                                 .ToArray();

            for (var j = 0; j < texturesToLoad.Length; j++)
            {
                var path = texturesToLoad[j];
                if (!textures.ContainsKey(path))
                {
                    var decoder = MediaType.GuessByFileName(path)
                                  .Where(ImageDecoderSet.Default.ContainsKey)
                                  .Select(type => ImageDecoderSet.Default[type])
                                  .FirstOrDefault();
                    using var stream = dataSource.GetStream(path);
                    var image       = decoder.Deserialize(stream);
                    var imageData   = image.GetData();
                    var imageWidth  = (uint)image.Info.Dimensions.Width;
                    var imageHeight = (uint)image.Info.Dimensions.Height;

                    var texture = factory.CreateTexture(new TextureDescription(
                                                            imageWidth, imageHeight, 1,
                                                            1, 1,
                                                            PixelFormat.R8_G8_B8_A8_UNorm,
                                                            TextureUsage.Sampled,
                                                            TextureType.Texture2D));

                    textures[path]     = texture;
                    textureViews[path] = factory.CreateTextureView(texture);

                    device.UpdateTexture(
                        texture,
                        imageData,
                        0, 0, 0,
                        imageWidth, imageHeight, 1,
                        0, 0);
                }
            }

            var elements = mapNames.Keys.Select(name => new ResourceLayoutElementDescription()
            {
                Name   = name,
                Kind   = ResourceKind.TextureReadOnly,
                Stages = ShaderStages.Fragment
            }).ToArray();

            var layout    = factory.CreateResourceLayout(new ResourceLayoutDescription(elements));
            var resources = new BindableResource[elements.Length];

            for (var i = 0; i < elements.Length; ++i)
            {
                resources[i] = textureViews[mapNames[elements[i].Name]];
            }
            resourceSets[index] = factory.CreateResourceSet(new ResourceSetDescription(layout, resources));
        }