private VectorInt2 AllocateTexture(WadTexture texture) { // Check if the texture is already loaded { VectorInt2 position; if (PackedTextures.TryGetValue(texture, out position)) { return(position); } } // Allocate texture { VectorInt2?position = TexturePacker.TryAdd(texture.Image.Size); if (position == null) { throw new TextureAtlasFullException(); } // Upload texture if (Texture == null) { Texture = Texture2D.New(GraphicsDevice, TextureAtlasSize, TextureAtlasSize, SharpDX.DXGI.Format.B8G8R8A8_UNorm, TextureFlags.ShaderResource, 1, SharpDX.Direct3D11.ResourceUsage.Default); } TextureLoad.Update(GraphicsDevice, Texture, texture.Image, position.Value); PackedTextures.Add(texture, position.Value); return(position.Value); } }
private void PreloadReplaceTextures(IEnumerable <WadMoveable> newMoveables, IEnumerable <WadStatic> newStatics) { Dispose(); // Collect textures var textures = new HashSet <WadTexture>(); foreach (var moveable in newMoveables) { foreach (var mesh in moveable.Meshes) { foreach (WadPolygon polygon in mesh.Polys) { textures.Add((WadTexture)polygon.Texture.Texture); } } } foreach (var stat in newStatics) { foreach (WadPolygon polygon in stat.Mesh.Polys) { textures.Add((WadTexture)polygon.Texture.Texture); } } // Order textures for packing var packedTextures = new List <WadTexture>(textures); packedTextures.Sort(new ComparerWadTextures()); // Pack the textures in a single atlas CreateTexturePacker(); foreach (var texture in packedTextures) { VectorInt2?positionInAtlas = TexturePacker.TryAdd(texture.Image.Size); if (positionInAtlas == null) { throw new TextureAtlasFullException(); } PackedTextures.Add(texture, positionInAtlas.Value); } // Create texture atlas var tempBitmap = ImageC.CreateNew(TextureAtlasSize, TextureAtlasSize); foreach (var texture in PackedTextures) { tempBitmap.CopyFrom(texture.Value.X, texture.Value.Y, texture.Key.Image); } Texture = TextureLoad.Load(GraphicsDevice, tempBitmap, SharpDX.Direct3D11.ResourceUsage.Default); }