public void Update(GraphicsDevice Device) { if (!CompositeValid) { CompositeValid = true; Layers.Sort((a, b) => a.Layer.Precedence - b.Layer.Precedence); // Render the composite texture var maxSize = new Point(0, 0); foreach (var layer in Layers) { maxSize.X = Math.Max(layer.Layer.CachedTexture.Width, maxSize.X); maxSize.Y = Math.Max(layer.Layer.CachedTexture.Height, maxSize.Y); } if (maxSize.X == 0 || maxSize.Y == 0) { return; } if (MemoryComposite == null || MemoryComposite.Width != maxSize.X || MemoryComposite.Height != maxSize.Y) { MemoryComposite = new MemoryTexture(maxSize.X, maxSize.Y); } TextureTool.ClearMemoryTexture(MemoryComposite); foreach (var layer in Layers) { TextureTool.Blit(layer.Layer.CachedTexture, layer.Palette.CachedPalette, MemoryComposite); } Composite = TextureTool.Texture2DFromMemoryTexture(Device, MemoryComposite); } }
public static void ConvertTestPSD() { var stream = System.IO.File.OpenRead("Content/Entities/Dwarf/Layers/test.psd"); var psd = new PhotoshopFile.PsdFile(stream, new PhotoshopFile.LoadContext()); if (FindPalette("Hair 02").HasValue(out var conversionPalette)) { foreach (var layer in psd.Layers) { var channels = new List <PhotoshopFile.Channel>(); channels.Add(layer.Channels.Where(c => c.ID == 0).FirstOrDefault()); channels.Add(layer.Channels.Where(c => c.ID == 1).FirstOrDefault()); channels.Add(layer.Channels.Where(c => c.ID == 2).FirstOrDefault()); channels.Add(layer.AlphaChannel); var rawMemText = new MemoryTexture(layer.Rect.Width, layer.Rect.Height); for (var index = 0; index < layer.Rect.Width * layer.Rect.Height; ++index) { if (channels[3].ImageData[index] == 0) { rawMemText.Data[index] = new Color(0, 0, 0, 0); } else { rawMemText.Data[index] = new Color(channels[0].ImageData[index], channels[1].ImageData[index], channels[2].ImageData[index], channels[3].ImageData[index]); } } var memTex = new MemoryTexture(psd.ColumnCount, psd.RowCount); TextureTool.Blit(rawMemText, new Rectangle(0, 0, layer.Rect.Width, layer.Rect.Height), memTex, new Point(layer.Rect.X, layer.Rect.Y)); var decomposed = TextureTool.DecomposeTexture(memTex, conversionPalette.CachedPalette); var composed = TextureTool.ComposeTexture(decomposed, BasePalette.CachedPalette); TextureTool.Blit(composed, new Rectangle(layer.Rect.X, layer.Rect.Y, layer.Rect.Width, layer.Rect.Height), rawMemText, new Rectangle(0, 0, rawMemText.Width, rawMemText.Height)); for (var index = 0; index < rawMemText.Width * rawMemText.Height; ++index) { channels[0].ImageData[index] = rawMemText.Data[index].R; channels[1].ImageData[index] = rawMemText.Data[index].G; channels[2].ImageData[index] = rawMemText.Data[index].B; channels[3].ImageData[index] = rawMemText.Data[index].A; } foreach (var channel in layer.Channels) { channel.ImageDataRaw = null; } } } psd.PrepareSave(); psd.Save("processed.psd", Encoding.Unicode); }
private static bool RuinTemplatesLoaded = false; // Todo: Move this over into the Library private static void LoadRuinTemplates() { if (RuinTemplatesLoaded) { return; } RuinTemplatesLoaded = true; RuinTemplates = new List <MemoryTexture>(); var tex = TextureTool.MemoryTextureFromTexture2D(AssetManager.GetContentTexture("World/ruins")); for (var x = 0; x < tex.Width; x += 16) // If our texture isn't the right size this will f**k up. { for (var y = 0; y < tex.Height; y += 16) { var template = new MemoryTexture(16, 16); TextureTool.Blit(tex, new Rectangle(x, y, 16, 16), template, new Point(0, 0)); RuinTemplates.Add(template); } } }