public Texture GetTexture(int id, Geometry geom) { //TODO: properly implement rectangle assignment ImageMap map; string movieName; if (geom.Container != null) { map = geom.Container.ImageMap; movieName = geom.Container.MovieName; } else { map = _imageMap; movieName = _movieName; } var texId = map.Mapping[id].TextureId; var texturePath = "art/Textures/apt_" + movieName + "_" + texId.ToString() + ".tga"; var loadOptions = new TextureLoadOptions() { GenerateMipMaps = false }; return(_contentManager.Load <Texture>(texturePath, loadOptions)); }
public Texture GetTexture(int id, Character callee) { var aptFile = callee.Container; //TODO: properly implement rectangle assignment var texId = aptFile.ImageMap.Mapping[id].TextureId; var movieName = aptFile.MovieName; var texturePath = "art/Textures/apt_" + movieName + "_" + texId.ToString() + ".tga"; var loadOptions = new TextureLoadOptions() { GenerateMipMaps = false }; return(_contentManager.Load <Texture>(texturePath, loadOptions)); }
public void Initialize(ContentManager contentManager, ImageMap map) { _map = map; _usedTextures = new Dictionary <int, Texture>(); //get all used image id's var usedIds = new HashSet <int>(); foreach (var entry in Shape.Entries) { if (entry is GeometryTexturedTriangles gtt) { var assignment = _map.Mapping[gtt.Image]; usedIds.Add(assignment.TextureId); } } foreach (var id in usedIds) { var texturePath = "art/Textures/apt_" + MovieName + "_" + id.ToString() + ".tga"; var loadOptions = new TextureLoadOptions() { GenerateMipMaps = false }; _usedTextures.Add(id, contentManager.Load <Texture>(texturePath, loadOptions)); } Material = new SpriteMaterial(contentManager.EffectLibrary.Sprite); _materialConstantsBuffer = new ConstantBuffer <SpriteMaterial.MaterialConstants>(contentManager.GraphicsDevice); _materialConstantsBuffer.Value.Opacity = 1; _materialConstantsBuffer.Update(); Material.SetMaterialConstants(_materialConstantsBuffer.Buffer); }
public static ShaderResourceView CreateTexture2DArray(Device device, DeviceContext context, string[] filePaths, TextureLoadOptions options) { var srcTex = new Texture2D[filePaths.Length]; for (var i = 0; i < filePaths.Length; i++) { srcTex[i] = CreateTextureFromFile(device, filePaths[i], options); } var texElementDesc = srcTex[0].Description; var texArrayDesc = new Texture2DDescription { Width = texElementDesc.Width, Height = texElementDesc.Height, MipLevels = texElementDesc.MipLevels, ArraySize = srcTex.Length, Format = texElementDesc.Format, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }; var texArray = new Texture2D(device, texArrayDesc); texArray.DebugName = "texture array: + " + filePaths.Aggregate((i, j) => i + ", " + j); for (int texElement = 0; texElement < srcTex.Length; texElement++) { for (int mipLevel = 0; mipLevel < texElementDesc.MipLevels; mipLevel++) { int mippedSize; DataBox mappedTex2D; mappedTex2D = context.MapSubresource(srcTex[texElement], mipLevel, 0, MapMode.Read, MapFlags.None, out mippedSize); context.UpdateSubresource( mappedTex2D, texArray, Resource.CalculateSubResourceIndex(mipLevel, texElement, texElementDesc.MipLevels) ); context.UnmapSubresource(srcTex[texElement], mipLevel); } } var viewDesc = new ShaderResourceViewDescription { Format = texArrayDesc.Format, Dimension = ShaderResourceViewDimension.Texture2DArray, Texture2DArray = new ShaderResourceViewDescription.Texture2DArrayResource() { MostDetailedMip = 0, MipLevels = texArrayDesc.MipLevels, FirstArraySlice = 0, ArraySize = srcTex.Length } }; var texArraySRV = new ShaderResourceView(device, texArray, viewDesc); Utilities.Dispose(ref texArray); for (int i = 0; i < srcTex.Length; i++) { Utilities.Dispose(ref srcTex[i]); } return texArraySRV; }
public static Texture2D CreateTextureFromFile(Device device, string filename, TextureLoadOptions options) { using (var bitmapSource = LoadBitmapSourceFromFile(_factory, filename)) { return CreateTexture2DFromBitmapSource(device, bitmapSource, options); } }
private static Texture2D CreateTexture2DFromBitmapSource(Device device, BitmapSource bitmapSource, TextureLoadOptions options) { // Allocate DataStream to receive the WIC image pixels var stride = bitmapSource.Size.Width * 4; using (var buffer = new DataStream(bitmapSource.Size.Height * stride, true, true)) { // Copy the content of the WIC to the buffer bitmapSource.CopyPixels(stride, buffer); var texture2DDescription = new Texture2DDescription() { Width = bitmapSource.Size.Width, Height = bitmapSource.Size.Height, ArraySize = 1, BindFlags = options.BindFlags, Usage = options.ResourceUsage, CpuAccessFlags = options.CpuAccessFlags, Format = options.Format, MipLevels = options.MipLevels, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), }; bitmapSource.Dispose(); var dataRectangle = new DataRectangle(buffer.DataPointer, stride); return new Texture2D(device, texture2DDescription, dataRectangle); } }
public static Texture2D CreateTextureFromFile(Device device, string filename, TextureLoadOptions options) { using (var bitmapSource = LoadBitmapSourceFromFile(_factory, filename)) { return(CreateTexture2DFromBitmapSource(device, bitmapSource, options)); } }
private static Texture2D CreateTexture2DFromBitmapSource(Device device, BitmapSource bitmapSource, TextureLoadOptions options) { // Allocate DataStream to receive the WIC image pixels var stride = bitmapSource.Size.Width * 4; using (var buffer = new DataStream(bitmapSource.Size.Height * stride, true, true)) { // Copy the content of the WIC to the buffer bitmapSource.CopyPixels(stride, buffer); var texture2DDescription = new Texture2DDescription() { Width = bitmapSource.Size.Width, Height = bitmapSource.Size.Height, ArraySize = 1, BindFlags = options.BindFlags, Usage = options.ResourceUsage, CpuAccessFlags = options.CpuAccessFlags, Format = options.Format, MipLevels = options.MipLevels, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), }; bitmapSource.Dispose(); var dataRectangle = new DataRectangle(buffer.DataPointer, stride); return(new Texture2D(device, texture2DDescription, dataRectangle)); } }
public static ShaderResourceView CreateTexture2DArray(Device device, DeviceContext context, string[] filePaths, TextureLoadOptions options) { var srcTex = new Texture2D[filePaths.Length]; for (var i = 0; i < filePaths.Length; i++) { srcTex[i] = CreateTextureFromFile(device, filePaths[i], options); } var texElementDesc = srcTex[0].Description; var texArrayDesc = new Texture2DDescription { Width = texElementDesc.Width, Height = texElementDesc.Height, MipLevels = texElementDesc.MipLevels, ArraySize = srcTex.Length, Format = texElementDesc.Format, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }; var texArray = new Texture2D(device, texArrayDesc); texArray.DebugName = "texture array: + " + filePaths.Aggregate((i, j) => i + ", " + j); for (int texElement = 0; texElement < srcTex.Length; texElement++) { for (int mipLevel = 0; mipLevel < texElementDesc.MipLevels; mipLevel++) { int mippedSize; DataBox mappedTex2D; mappedTex2D = context.MapSubresource(srcTex[texElement], mipLevel, 0, MapMode.Read, MapFlags.None, out mippedSize); context.UpdateSubresource( mappedTex2D, texArray, Resource.CalculateSubResourceIndex(mipLevel, texElement, texElementDesc.MipLevels) ); context.UnmapSubresource(srcTex[texElement], mipLevel); } } var viewDesc = new ShaderResourceViewDescription { Format = texArrayDesc.Format, Dimension = ShaderResourceViewDimension.Texture2DArray, Texture2DArray = new ShaderResourceViewDescription.Texture2DArrayResource() { MostDetailedMip = 0, MipLevels = texArrayDesc.MipLevels, FirstArraySlice = 0, ArraySize = srcTex.Length } }; var texArraySRV = new ShaderResourceView(device, texArray, viewDesc); Utilities.Dispose(ref texArray); for (int i = 0; i < srcTex.Length; i++) { Utilities.Dispose(ref srcTex[i]); } return(texArraySRV); }