Add alpha support to SpriteRenderer.

This commit is contained in:
Paul Chote
2020-10-09 22:16:07 +01:00
committed by abcdefg30
parent b88495c689
commit 67754e8693
10 changed files with 46 additions and 46 deletions

View File

@@ -46,20 +46,20 @@ namespace OpenRA.Graphics
parent.DrawSprite(s, a, b, c, d); parent.DrawSprite(s, a, b, c, d);
} }
public void DrawSpriteWithTint(Sprite s, in float3 location, in float3 size, in float3 tint) public void DrawSprite(Sprite s, in float3 location, in float3 size, in float3 tint, float alpha)
{ {
if (s.Channel != TextureChannel.RGBA) if (s.Channel != TextureChannel.RGBA)
throw new InvalidOperationException("DrawRGBASprite requires a RGBA sprite."); throw new InvalidOperationException("DrawRGBASprite requires a RGBA sprite.");
parent.DrawSpriteWithTint(s, location, 0, size, tint); parent.DrawSprite(s, location, 0, size, tint, alpha);
} }
public void DrawSpriteWithTint(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d, in float3 tint) public void DrawSprite(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d, in float3 tint, float alpha)
{ {
if (s.Channel != TextureChannel.RGBA) if (s.Channel != TextureChannel.RGBA)
throw new InvalidOperationException("DrawRGBASprite requires a RGBA sprite."); throw new InvalidOperationException("DrawRGBASprite requires a RGBA sprite.");
parent.DrawSpriteWithTint(s, a, b, c, d, tint); parent.DrawSprite(s, a, b, c, d, tint, alpha);
} }
} }
} }

View File

@@ -89,10 +89,10 @@ namespace OpenRA.Graphics
if (g.Sprite != null) if (g.Sprite != null)
{ {
var contrastSprite = contrastGlyphs[(s, screenContrast)]; var contrastSprite = contrastGlyphs[(s, screenContrast)];
Game.Renderer.RgbaSpriteRenderer.DrawSpriteWithTint(contrastSprite, Game.Renderer.RgbaSpriteRenderer.DrawSprite(contrastSprite,
(screen + g.Offset - contrastVector) / deviceScale, (screen + g.Offset - contrastVector) / deviceScale,
contrastSprite.Size / deviceScale, contrastSprite.Size / deviceScale,
tint); tint, 1f);
} }
screen += new int2((int)(g.Advance + 0.5f), 0); screen += new int2((int)(g.Advance + 0.5f), 0);
@@ -120,10 +120,10 @@ namespace OpenRA.Graphics
// Convert screen coordinates back to UI coordinates for drawing // Convert screen coordinates back to UI coordinates for drawing
if (g.Sprite != null) if (g.Sprite != null)
Game.Renderer.RgbaSpriteRenderer.DrawSpriteWithTint(g.Sprite, Game.Renderer.RgbaSpriteRenderer.DrawSprite(g.Sprite,
(screen + g.Offset).ToFloat2() / deviceScale, (screen + g.Offset).ToFloat2() / deviceScale,
g.Sprite.Size / deviceScale, g.Sprite.Size / deviceScale,
tint); tint, 1f);
screen += new int2((int)(g.Advance + 0.5f), 0); screen += new int2((int)(g.Advance + 0.5f), 0);
} }
@@ -172,12 +172,12 @@ namespace OpenRA.Graphics
// Offset rotated glyph to align the top-left corner with the screen pixel grid // Offset rotated glyph to align the top-left corner with the screen pixel grid
var screenOffset = new float2((int)(ra.X * deviceScale + 0.5f), (int)(ra.Y * deviceScale + 0.5f)) / deviceScale - ra; var screenOffset = new float2((int)(ra.X * deviceScale + 0.5f), (int)(ra.Y * deviceScale + 0.5f)) / deviceScale - ra;
Game.Renderer.RgbaSpriteRenderer.DrawSpriteWithTint(g.Sprite, Game.Renderer.RgbaSpriteRenderer.DrawSprite(g.Sprite,
ra + screenOffset, ra + screenOffset,
rb + screenOffset, rb + screenOffset,
rc + screenOffset, rc + screenOffset,
rd + screenOffset, rd + screenOffset,
tint); tint, 1f);
} }
p += new float2(g.Advance / deviceScale, 0); p += new float2(g.Advance / deviceScale, 0);

View File

@@ -80,7 +80,7 @@ namespace OpenRA.Graphics
if (wr.TerrainLighting != null) if (wr.TerrainLighting != null)
t *= wr.TerrainLighting.TintAt(pos); t *= wr.TerrainLighting.TintAt(pos);
wsr.DrawSpriteWithTint(sprite, ScreenPosition(wr), palette, scale * sprite.Size, t); wsr.DrawSprite(sprite, ScreenPosition(wr), palette, scale * sprite.Size, t, 1f);
} }
} }

View File

@@ -111,7 +111,7 @@ namespace OpenRA.Graphics
internal void DrawSprite(Sprite s, in float3 location, float paletteTextureIndex, in float3 size) internal void DrawSprite(Sprite s, in float3 location, float paletteTextureIndex, in float3 size)
{ {
var samplers = SetRenderStateForSprite(s); var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, samplers, paletteTextureIndex, nv, size, float3.Ones); Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, samplers, paletteTextureIndex, nv, size, float3.Ones, 1f);
nv += 6; nv += 6;
} }
@@ -128,26 +128,26 @@ namespace OpenRA.Graphics
public void DrawSprite(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d) public void DrawSprite(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d)
{ {
var samplers = SetRenderStateForSprite(s); var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, a, b, c, d, s, samplers, 0, float3.Ones, nv); Util.FastCreateQuad(vertices, a, b, c, d, s, samplers, 0, float3.Ones, 1f, nv);
nv += 6; nv += 6;
} }
internal void DrawSpriteWithTint(Sprite s, in float3 location, float paletteTextureIndex, in float3 size, in float3 tint) internal void DrawSprite(Sprite s, in float3 location, float paletteTextureIndex, in float3 size, in float3 tint, float alpha)
{ {
var samplers = SetRenderStateForSprite(s); var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, samplers, paletteTextureIndex, nv, size, tint); Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, samplers, paletteTextureIndex, nv, size, tint, alpha);
nv += 6; nv += 6;
} }
public void DrawSpriteWithTint(Sprite s, in float3 location, PaletteReference pal, in float3 size, in float3 tint) public void DrawSprite(Sprite s, in float3 location, PaletteReference pal, in float3 size, in float3 tint, float alpha)
{ {
DrawSpriteWithTint(s, location, pal.TextureIndex, size, tint); DrawSprite(s, location, pal.TextureIndex, size, tint, alpha);
} }
public void DrawSpriteWithTint(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d, in float3 tint) public void DrawSprite(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d, in float3 tint, float alpha)
{ {
var samplers = SetRenderStateForSprite(s); var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, a, b, c, d, s, samplers, 0, tint, nv); Util.FastCreateQuad(vertices, a, b, c, d, s, samplers, 0, tint, alpha, nv);
nv += 6; nv += 6;
} }

View File

@@ -68,7 +68,7 @@ namespace OpenRA.Graphics
for (var i = 0; i < vertices.Length; i++) for (var i = 0; i < vertices.Length; i++)
{ {
var v = vertices[i]; var v = vertices[i];
vertices[i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, v.R, v.G, v.B); vertices[i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, v.R, v.G, v.B, v.A);
} }
for (var row = 0; row < map.MapSize.Y; row++) for (var row = 0; row < map.MapSize.Y; row++)
@@ -106,7 +106,7 @@ namespace OpenRA.Graphics
for (var i = 0; i < 6; i++) for (var i = 0; i < 6; i++)
{ {
var v = vertices[offset + i]; var v = vertices[offset + i];
vertices[offset + i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, noTint); vertices[offset + i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, noTint, 1f);
} }
return; return;
@@ -131,7 +131,7 @@ namespace OpenRA.Graphics
for (var i = 0; i < 6; i++) for (var i = 0; i < 6; i++)
{ {
var v = vertices[offset + i]; var v = vertices[offset + i];
vertices[offset + i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, weights[CornerVertexMap[i]]); vertices[offset + i] = new Vertex(v.X, v.Y, v.Z, v.S, v.T, v.U, v.V, palette.TextureIndex, v.C, weights[CornerVertexMap[i]], 1f);
} }
dirtyRows.Add(uv.V); dirtyRows.Add(uv.V);
@@ -155,7 +155,7 @@ namespace OpenRA.Graphics
return; return;
var offset = rowStride * uv.V + 6 * uv.U; var offset = rowStride * uv.V + 6 * uv.U;
Util.FastCreateQuad(vertices, pos, sprite, int2.Zero, palette.TextureIndex, offset, sprite.Size, float3.Ones); Util.FastCreateQuad(vertices, pos, sprite, int2.Zero, palette.TextureIndex, offset, sprite.Size, float3.Ones, 1f);
if (worldRenderer.TerrainLighting != null) if (worldRenderer.TerrainLighting != null)
{ {

View File

@@ -20,18 +20,18 @@ namespace OpenRA.Graphics
// yes, our channel order is nuts. // yes, our channel order is nuts.
static readonly int[] ChannelMasks = { 2, 1, 0, 3 }; static readonly int[] ChannelMasks = { 2, 1, 0, 3 };
public static void FastCreateQuad(Vertex[] vertices, in float3 o, Sprite r, int2 samplers, float paletteTextureIndex, int nv, in float3 size, in float3 tint) public static void FastCreateQuad(Vertex[] vertices, in float3 o, Sprite r, int2 samplers, float paletteTextureIndex, int nv, in float3 size, in float3 tint, float alpha)
{ {
var b = new float3(o.X + size.X, o.Y, o.Z); var b = new float3(o.X + size.X, o.Y, o.Z);
var c = new float3(o.X + size.X, o.Y + size.Y, o.Z + size.Z); var c = new float3(o.X + size.X, o.Y + size.Y, o.Z + size.Z);
var d = new float3(o.X, o.Y + size.Y, o.Z + size.Z); var d = new float3(o.X, o.Y + size.Y, o.Z + size.Z);
FastCreateQuad(vertices, o, b, c, d, r, samplers, paletteTextureIndex, tint, nv); FastCreateQuad(vertices, o, b, c, d, r, samplers, paletteTextureIndex, tint, alpha, nv);
} }
public static void FastCreateQuad(Vertex[] vertices, public static void FastCreateQuad(Vertex[] vertices,
in float3 a, in float3 b, in float3 c, in float3 d, in float3 a, in float3 b, in float3 c, in float3 d,
Sprite r, int2 samplers, float paletteTextureIndex, Sprite r, int2 samplers, float paletteTextureIndex,
in float3 tint, int nv) in float3 tint, float alpha, int nv)
{ {
float sl = 0; float sl = 0;
float st = 0; float st = 0;
@@ -54,12 +54,12 @@ namespace OpenRA.Graphics
} }
var fAttribC = (float)attribC; var fAttribC = (float)attribC;
vertices[nv] = new Vertex(a, r.Left, r.Top, sl, st, paletteTextureIndex, fAttribC, tint); vertices[nv] = new Vertex(a, r.Left, r.Top, sl, st, paletteTextureIndex, fAttribC, tint, alpha);
vertices[nv + 1] = new Vertex(b, r.Right, r.Top, sr, st, paletteTextureIndex, fAttribC, tint); vertices[nv + 1] = new Vertex(b, r.Right, r.Top, sr, st, paletteTextureIndex, fAttribC, tint, alpha);
vertices[nv + 2] = new Vertex(c, r.Right, r.Bottom, sr, sb, paletteTextureIndex, fAttribC, tint); vertices[nv + 2] = new Vertex(c, r.Right, r.Bottom, sr, sb, paletteTextureIndex, fAttribC, tint, alpha);
vertices[nv + 3] = new Vertex(c, r.Right, r.Bottom, sr, sb, paletteTextureIndex, fAttribC, tint); vertices[nv + 3] = new Vertex(c, r.Right, r.Bottom, sr, sb, paletteTextureIndex, fAttribC, tint, alpha);
vertices[nv + 4] = new Vertex(d, r.Left, r.Bottom, sl, sb, paletteTextureIndex, fAttribC, tint); vertices[nv + 4] = new Vertex(d, r.Left, r.Bottom, sl, sb, paletteTextureIndex, fAttribC, tint, alpha);
vertices[nv + 5] = new Vertex(a, r.Left, r.Top, sl, st, paletteTextureIndex, fAttribC, tint); vertices[nv + 5] = new Vertex(a, r.Left, r.Top, sl, st, paletteTextureIndex, fAttribC, tint, alpha);
} }
public static void FastCopyIntoChannel(Sprite dest, byte[] src, SpriteFrameType srcType) public static void FastCopyIntoChannel(Sprite dest, byte[] src, SpriteFrameType srcType)

View File

@@ -26,24 +26,24 @@ namespace OpenRA.Graphics
public readonly float P, C; public readonly float P, C;
// Color tint // Color tint
public readonly float R, G, B; public readonly float R, G, B, A;
public Vertex(in float3 xyz, float s, float t, float u, float v, float p, float c) public Vertex(in float3 xyz, float s, float t, float u, float v, float p, float c)
: this(xyz.X, xyz.Y, xyz.Z, s, t, u, v, p, c, float3.Ones) { } : this(xyz.X, xyz.Y, xyz.Z, s, t, u, v, p, c, float3.Ones, 1f) { }
public Vertex(in float3 xyz, float s, float t, float u, float v, float p, float c, in float3 tint) public Vertex(in float3 xyz, float s, float t, float u, float v, float p, float c, in float3 tint, float a)
: this(xyz.X, xyz.Y, xyz.Z, s, t, u, v, p, c, tint.X, tint.Y, tint.Z) { } : this(xyz.X, xyz.Y, xyz.Z, s, t, u, v, p, c, tint.X, tint.Y, tint.Z, a) { }
public Vertex(float x, float y, float z, float s, float t, float u, float v, float p, float c, in float3 tint) public Vertex(float x, float y, float z, float s, float t, float u, float v, float p, float c, in float3 tint, float a)
: this(x, y, z, s, t, u, v, p, c, tint.X, tint.Y, tint.Z) { } : this(x, y, z, s, t, u, v, p, c, tint.X, tint.Y, tint.Z, a) { }
public Vertex(float x, float y, float z, float s, float t, float u, float v, float p, float c, float r, float g, float b) public Vertex(float x, float y, float z, float s, float t, float u, float v, float p, float c, float r, float g, float b, float a)
{ {
X = x; Y = y; Z = z; X = x; Y = y; Z = z;
S = s; T = t; S = s; T = t;
U = u; V = v; U = u; V = v;
P = p; C = c; P = p; C = c;
R = r; G = g; B = b; R = r; G = g; B = b; A = a;
} }
} }
} }

View File

@@ -148,8 +148,8 @@ namespace OpenRA.Mods.Common.Graphics
if (wr.TerrainLighting != null) if (wr.TerrainLighting != null)
t *= wr.TerrainLighting.TintAt(model.pos); t *= wr.TerrainLighting.TintAt(model.pos);
wrsr.DrawSpriteWithTint(renderProxy.ShadowSprite, sa, sb, sc, sd, t); wrsr.DrawSprite(renderProxy.ShadowSprite, sa, sb, sc, sd, t, 1f);
wrsr.DrawSpriteWithTint(renderProxy.Sprite, pxOrigin - 0.5f * renderProxy.Sprite.Size, renderProxy.Sprite.Size, t); wrsr.DrawSprite(renderProxy.Sprite, pxOrigin - 0.5f * renderProxy.Sprite.Size, renderProxy.Sprite.Size, t, 1f);
} }
public void RenderDebugGeometry(WorldRenderer wr) public void RenderDebugGeometry(WorldRenderer wr)

View File

@@ -91,7 +91,7 @@ namespace OpenRA.Platforms.Default
OpenGL.CheckGLError(); OpenGL.CheckGLError();
OpenGL.glVertexAttribPointer(Shader.TexMetadataAttributeIndex, 2, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(28)); OpenGL.glVertexAttribPointer(Shader.TexMetadataAttributeIndex, 2, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(28));
OpenGL.CheckGLError(); OpenGL.CheckGLError();
OpenGL.glVertexAttribPointer(Shader.TintAttributeIndex, 3, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(36)); OpenGL.glVertexAttribPointer(Shader.TintAttributeIndex, 4, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(36));
OpenGL.CheckGLError(); OpenGL.CheckGLError();
} }

View File

@@ -7,7 +7,7 @@ uniform vec3 r1, r2;
attribute vec4 aVertexPosition; attribute vec4 aVertexPosition;
attribute vec4 aVertexTexCoord; attribute vec4 aVertexTexCoord;
attribute vec2 aVertexTexMetadata; attribute vec2 aVertexTexMetadata;
attribute vec3 aVertexTint; attribute vec4 aVertexTint;
varying vec4 vTexCoord; varying vec4 vTexCoord;
varying vec2 vTexMetadata; varying vec2 vTexMetadata;
@@ -23,7 +23,7 @@ varying vec4 vTint;
in vec4 aVertexPosition; in vec4 aVertexPosition;
in vec4 aVertexTexCoord; in vec4 aVertexTexCoord;
in vec2 aVertexTexMetadata; in vec2 aVertexTexMetadata;
in vec3 aVertexTint; in vec4 aVertexTint;
out vec4 vTexCoord; out vec4 vTexCoord;
out vec2 vTexMetadata; out vec2 vTexMetadata;
@@ -127,5 +127,5 @@ void main()
vPalettedFraction = SelectPalettedFraction(attrib.s); vPalettedFraction = SelectPalettedFraction(attrib.s);
vDepthMask = SelectChannelMask(attrib.t); vDepthMask = SelectChannelMask(attrib.t);
vTexSampler = attrib.pq; vTexSampler = attrib.pq;
vTint = vec4(aVertexTint, 1.0); vTint = aVertexTint;
} }