Merge pull request #8176 from pchote/fix-palette-alpha

Load textures using premultiplied alpha.
This commit is contained in:
Oliver Brakmann
2015-05-24 13:15:54 +02:00
24 changed files with 230 additions and 91 deletions

View File

@@ -119,13 +119,12 @@ namespace OpenRA.Mods.Common.Graphics
var offset = LoadField<float2>(d, "Offset", float2.Zero);
var blendMode = LoadField<BlendMode>(d, "BlendMode", BlendMode.Alpha);
var alpha = LoadField<float>(d, "Alpha", 1f);
// Apply offset to each sprite in the sequence
// Different sequences may apply different offsets to the same frame
var src = GetSpriteSrc(modData, tileSet, sequence, animation, info.Value, d);
sprites = cache[src].Select(
s => new Sprite(s.Sheet, s.Bounds, s.Offset + offset, s.Channel, blendMode, alpha)).ToArray();
s => new Sprite(s.Sheet, s.Bounds, s.Offset + offset, s.Channel, blendMode)).ToArray();
MiniYaml length;
if (d.TryGetValue("Length", out length) && length.Value == "*")

View File

@@ -644,6 +644,8 @@
<Compile Include="Traits\World\EditorActorPreview.cs" />
<Compile Include="Traits\World\EditorResourceLayer.cs" />
<Compile Include="Widgets\EditorViewportControllerWidget.cs" />
<Compile Include="Traits\World\PaletteFromPaletteWithAlpha.cs" />
<Compile Include="Traits\World\PaletteFromPlayerPaletteWithAlpha.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

View File

@@ -0,0 +1,76 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Create a palette by applying alpha transparency to another palette.")]
class PaletteFromPaletteWithAlphaInfo : ITraitInfo
{
[Desc("Internal palette name")]
public readonly string Name = null;
[Desc("The name of the palette to base off.")]
public readonly string BasePalette = null;
[Desc("Allow palette modifiers to change the palette.")]
public readonly bool AllowModifiers = true;
[Desc("Alpha component that is applied to the base palette.")]
public readonly float Alpha = 1.0f;
[Desc("Premultiply color by the alpha component.")]
public readonly bool Premultiply = true;
public object Create(ActorInitializer init) { return new PaletteFromPaletteWithAlpha(this); }
}
class PaletteFromPaletteWithAlpha : ILoadsPalettes, IProvidesAssetBrowserPalettes
{
readonly PaletteFromPaletteWithAlphaInfo info;
public PaletteFromPaletteWithAlpha(PaletteFromPaletteWithAlphaInfo info) { this.info = info; }
public void LoadPalettes(WorldRenderer wr)
{
var remap = new AlphaPaletteRemap(info.Alpha, info.Premultiply);
wr.AddPalette(info.Name, new ImmutablePalette(wr.Palette(info.BasePalette).Palette, remap), info.AllowModifiers);
}
public IEnumerable<string> PaletteNames { get { yield return info.Name; } }
}
class AlphaPaletteRemap : IPaletteRemap
{
readonly float alpha;
readonly bool premultiply;
public AlphaPaletteRemap(float alpha, bool premultiply)
{
this.alpha = alpha;
this.premultiply = premultiply;
}
public Color GetRemappedColor(Color original, int index)
{
var a = (int)(original.A * alpha).Clamp(0, 255);
var r = premultiply ? (int)(alpha * original.R + 0.5f).Clamp(0, 255) : original.R;
var g = premultiply ? (int)(alpha * original.G + 0.5f).Clamp(0, 255) : original.G;
var b = premultiply ? (int)(alpha * original.B + 0.5f).Clamp(0, 255) : original.B;
return Color.FromArgb(a, r, g, b);
}
}
}

View File

@@ -0,0 +1,53 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Create player palettes by applying alpha transparency to another player palette.")]
class PaletteFromPlayerPaletteWithAlphaInfo : ITraitInfo
{
[Desc("The prefix for the resulting player palettes")]
public readonly string BaseName = null;
[Desc("The name of the player palette to base off.")]
public readonly string BasePalette = null;
[Desc("Allow palette modifiers to change the palette.")]
public readonly bool AllowModifiers = true;
[Desc("Alpha component that is applied to the base palette.")]
public readonly float Alpha = 1.0f;
[Desc("Premultiply color by the alpha component.")]
public readonly bool Premultiply = true;
public object Create(ActorInitializer init) { return new PaletteFromPlayerPaletteWithAlpha(this); }
}
class PaletteFromPlayerPaletteWithAlpha : ILoadsPlayerPalettes
{
readonly PaletteFromPlayerPaletteWithAlphaInfo info;
public PaletteFromPlayerPaletteWithAlpha(PaletteFromPlayerPaletteWithAlphaInfo info) { this.info = info; }
public void LoadPlayerPalettes(WorldRenderer wr, string playerName, HSLColor color, bool replaceExisting)
{
var remap = new AlphaPaletteRemap(info.Alpha, info.Premultiply);
var pal = new ImmutablePalette(wr.Palette(info.BasePalette + playerName).Palette, remap);
wr.AddPalette(info.BaseName + playerName, pal, info.AllowModifiers, replaceExisting);
}
}
}

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
@@ -50,7 +51,11 @@ namespace OpenRA.Mods.Common.Traits
if (info.Tileset != null && info.Tileset.ToLowerInvariant() != world.Map.Tileset.ToLowerInvariant())
return;
var c = (uint)((info.A << 24) | (info.R << 16) | (info.G << 8) | info.B);
var a = info.A / 255f;
var r = (int)(a * info.R + 0.5f).Clamp(0, 255);
var g = (int)(a * info.G + 0.5f).Clamp(0, 255);
var b = (int)(a * info.B + 0.5f).Clamp(0, 255);
var c = (uint)Color.FromArgb(info.A, r, g, b).ToArgb();
wr.AddPalette(info.Name, new ImmutablePalette(Enumerable.Range(0, Palette.Size).Select(i => (i == 0) ? 0 : c)), info.AllowModifiers);
}
}

View File

@@ -42,8 +42,8 @@ namespace OpenRA.Mods.Common.Traits
static readonly Color[] Fog = new[]
{
Color.Transparent, Color.Green,
Color.Blue, Color.Yellow,
Color.FromArgb(0, 0, 0, 0),
Color.Green, Color.Blue, Color.Yellow,
Color.FromArgb(128, 0, 0, 0),
Color.FromArgb(96, 0, 0, 0),
Color.FromArgb(64, 0, 0, 0),
@@ -52,8 +52,8 @@ namespace OpenRA.Mods.Common.Traits
static readonly Color[] Shroud = new[]
{
Color.Transparent, Color.Green,
Color.Blue, Color.Yellow,
Color.FromArgb(0, 0, 0, 0),
Color.Green, Color.Blue, Color.Yellow,
Color.Black,
Color.FromArgb(160, 0, 0, 0),
Color.FromArgb(128, 0, 0, 0),