From 67754e8693b46366ffd34231afa871ec3a907b24 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 9 Oct 2020 22:16:07 +0100 Subject: [PATCH] Add alpha support to SpriteRenderer. --- OpenRA.Game/Graphics/RgbaSpriteRenderer.cs | 8 ++++---- OpenRA.Game/Graphics/SpriteFont.cs | 12 ++++++------ OpenRA.Game/Graphics/SpriteRenderable.cs | 2 +- OpenRA.Game/Graphics/SpriteRenderer.cs | 16 ++++++++-------- OpenRA.Game/Graphics/TerrainSpriteLayer.cs | 8 ++++---- OpenRA.Game/Graphics/Util.cs | 18 +++++++++--------- OpenRA.Game/Graphics/Vertex.cs | 16 ++++++++-------- OpenRA.Mods.Common/Graphics/ModelRenderable.cs | 4 ++-- OpenRA.Platforms.Default/VertexBuffer.cs | 2 +- glsl/combined.vert | 6 +++--- 10 files changed, 46 insertions(+), 46 deletions(-) diff --git a/OpenRA.Game/Graphics/RgbaSpriteRenderer.cs b/OpenRA.Game/Graphics/RgbaSpriteRenderer.cs index eca37f72b3c7..764a83893d24 100644 --- a/OpenRA.Game/Graphics/RgbaSpriteRenderer.cs +++ b/OpenRA.Game/Graphics/RgbaSpriteRenderer.cs @@ -46,20 +46,20 @@ public void DrawSprite(Sprite s, in float3 a, in float3 b, in float3 c, in float 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) 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) 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); } } } diff --git a/OpenRA.Game/Graphics/SpriteFont.cs b/OpenRA.Game/Graphics/SpriteFont.cs index 06829426e04a..21d8bc3fe121 100644 --- a/OpenRA.Game/Graphics/SpriteFont.cs +++ b/OpenRA.Game/Graphics/SpriteFont.cs @@ -89,10 +89,10 @@ void DrawTextContrast(string text, float2 location, Color contrastColor, int con if (g.Sprite != null) { var contrastSprite = contrastGlyphs[(s, screenContrast)]; - Game.Renderer.RgbaSpriteRenderer.DrawSpriteWithTint(contrastSprite, + Game.Renderer.RgbaSpriteRenderer.DrawSprite(contrastSprite, (screen + g.Offset - contrastVector) / deviceScale, contrastSprite.Size / deviceScale, - tint); + tint, 1f); } screen += new int2((int)(g.Advance + 0.5f), 0); @@ -120,10 +120,10 @@ public void DrawText(string text, float2 location, Color c) // Convert screen coordinates back to UI coordinates for drawing if (g.Sprite != null) - Game.Renderer.RgbaSpriteRenderer.DrawSpriteWithTint(g.Sprite, + Game.Renderer.RgbaSpriteRenderer.DrawSprite(g.Sprite, (screen + g.Offset).ToFloat2() / deviceScale, g.Sprite.Size / deviceScale, - tint); + tint, 1f); screen += new int2((int)(g.Advance + 0.5f), 0); } @@ -172,12 +172,12 @@ public void DrawText(string text, float2 location, Color c, float angle) // 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; - Game.Renderer.RgbaSpriteRenderer.DrawSpriteWithTint(g.Sprite, + Game.Renderer.RgbaSpriteRenderer.DrawSprite(g.Sprite, ra + screenOffset, rb + screenOffset, rc + screenOffset, rd + screenOffset, - tint); + tint, 1f); } p += new float2(g.Advance / deviceScale, 0); diff --git a/OpenRA.Game/Graphics/SpriteRenderable.cs b/OpenRA.Game/Graphics/SpriteRenderable.cs index d51a7656989e..2432fe88edf9 100644 --- a/OpenRA.Game/Graphics/SpriteRenderable.cs +++ b/OpenRA.Game/Graphics/SpriteRenderable.cs @@ -80,7 +80,7 @@ public void Render(WorldRenderer wr) if (wr.TerrainLighting != null) 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); } } diff --git a/OpenRA.Game/Graphics/SpriteRenderer.cs b/OpenRA.Game/Graphics/SpriteRenderer.cs index 695f3a3680cc..6d3bfb9baa38 100644 --- a/OpenRA.Game/Graphics/SpriteRenderer.cs +++ b/OpenRA.Game/Graphics/SpriteRenderer.cs @@ -111,7 +111,7 @@ int2 SetRenderStateForSprite(Sprite s) internal void DrawSprite(Sprite s, in float3 location, float paletteTextureIndex, in float3 size) { 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; } @@ -128,26 +128,26 @@ public void DrawSprite(Sprite s, in float3 location, PaletteReference pal, float public void DrawSprite(Sprite s, in float3 a, in float3 b, in float3 c, in float3 d) { 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; } - 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); - 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; } - 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); - 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; } diff --git a/OpenRA.Game/Graphics/TerrainSpriteLayer.cs b/OpenRA.Game/Graphics/TerrainSpriteLayer.cs index 31ef7fcda633..1b22f4006b20 100644 --- a/OpenRA.Game/Graphics/TerrainSpriteLayer.cs +++ b/OpenRA.Game/Graphics/TerrainSpriteLayer.cs @@ -68,7 +68,7 @@ void UpdatePaletteIndices() for (var i = 0; i < vertices.Length; 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++) @@ -106,7 +106,7 @@ void UpdateTint(MPos uv) for (var i = 0; i < 6; 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; @@ -131,7 +131,7 @@ void UpdateTint(MPos uv) for (var i = 0; i < 6; 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); @@ -155,7 +155,7 @@ public void Update(MPos uv, Sprite sprite, in float3 pos, bool ignoreTint) return; 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) { diff --git a/OpenRA.Game/Graphics/Util.cs b/OpenRA.Game/Graphics/Util.cs index 874ee254e580..011d4fe96dfe 100644 --- a/OpenRA.Game/Graphics/Util.cs +++ b/OpenRA.Game/Graphics/Util.cs @@ -20,18 +20,18 @@ public static class Util // yes, our channel order is nuts. 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 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); - 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, in float3 a, in float3 b, in float3 c, in float3 d, Sprite r, int2 samplers, float paletteTextureIndex, - in float3 tint, int nv) + in float3 tint, float alpha, int nv) { float sl = 0; float st = 0; @@ -54,12 +54,12 @@ public static void FastCreateQuad(Vertex[] vertices, } var fAttribC = (float)attribC; - vertices[nv] = new Vertex(a, r.Left, r.Top, sl, st, paletteTextureIndex, fAttribC, tint); - vertices[nv + 1] = new Vertex(b, r.Right, r.Top, sr, st, paletteTextureIndex, fAttribC, tint); - vertices[nv + 2] = 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); - vertices[nv + 4] = new Vertex(d, r.Left, r.Bottom, sl, sb, paletteTextureIndex, fAttribC, tint); - vertices[nv + 5] = 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, alpha); + 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, alpha); + 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, alpha); } public static void FastCopyIntoChannel(Sprite dest, byte[] src, SpriteFrameType srcType) diff --git a/OpenRA.Game/Graphics/Vertex.cs b/OpenRA.Game/Graphics/Vertex.cs index 2b45bdb31a2d..6893a3bee44f 100644 --- a/OpenRA.Game/Graphics/Vertex.cs +++ b/OpenRA.Game/Graphics/Vertex.cs @@ -26,24 +26,24 @@ public struct Vertex public readonly float P, C; // 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) - : 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) - : this(xyz.X, xyz.Y, xyz.Z, s, t, u, v, p, c, tint.X, tint.Y, tint.Z) { } + 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, a) { } - public Vertex(float x, float y, float z, float s, float t, float u, float v, float p, float c, in float3 tint) - : this(x, y, z, s, t, u, v, p, c, tint.X, tint.Y, tint.Z) { } + 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, 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; S = s; T = t; U = u; V = v; P = p; C = c; - R = r; G = g; B = b; + R = r; G = g; B = b; A = a; } } } diff --git a/OpenRA.Mods.Common/Graphics/ModelRenderable.cs b/OpenRA.Mods.Common/Graphics/ModelRenderable.cs index 487b2698c6bf..4ec0bc5f3d4f 100644 --- a/OpenRA.Mods.Common/Graphics/ModelRenderable.cs +++ b/OpenRA.Mods.Common/Graphics/ModelRenderable.cs @@ -148,8 +148,8 @@ public void Render(WorldRenderer wr) if (wr.TerrainLighting != null) t *= wr.TerrainLighting.TintAt(model.pos); - wrsr.DrawSpriteWithTint(renderProxy.ShadowSprite, sa, sb, sc, sd, t); - wrsr.DrawSpriteWithTint(renderProxy.Sprite, pxOrigin - 0.5f * renderProxy.Sprite.Size, renderProxy.Sprite.Size, t); + wrsr.DrawSprite(renderProxy.ShadowSprite, sa, sb, sc, sd, t, 1f); + wrsr.DrawSprite(renderProxy.Sprite, pxOrigin - 0.5f * renderProxy.Sprite.Size, renderProxy.Sprite.Size, t, 1f); } public void RenderDebugGeometry(WorldRenderer wr) diff --git a/OpenRA.Platforms.Default/VertexBuffer.cs b/OpenRA.Platforms.Default/VertexBuffer.cs index e6759add363d..7bbcc0e5ad5f 100644 --- a/OpenRA.Platforms.Default/VertexBuffer.cs +++ b/OpenRA.Platforms.Default/VertexBuffer.cs @@ -91,7 +91,7 @@ public void Bind() OpenGL.CheckGLError(); OpenGL.glVertexAttribPointer(Shader.TexMetadataAttributeIndex, 2, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(28)); 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(); } diff --git a/glsl/combined.vert b/glsl/combined.vert index d1b30e746ad5..f4d8c807ae1b 100644 --- a/glsl/combined.vert +++ b/glsl/combined.vert @@ -7,7 +7,7 @@ uniform vec3 r1, r2; attribute vec4 aVertexPosition; attribute vec4 aVertexTexCoord; attribute vec2 aVertexTexMetadata; -attribute vec3 aVertexTint; +attribute vec4 aVertexTint; varying vec4 vTexCoord; varying vec2 vTexMetadata; @@ -23,7 +23,7 @@ varying vec4 vTint; in vec4 aVertexPosition; in vec4 aVertexTexCoord; in vec2 aVertexTexMetadata; -in vec3 aVertexTint; +in vec4 aVertexTint; out vec4 vTexCoord; out vec2 vTexMetadata; @@ -127,5 +127,5 @@ void main() vPalettedFraction = SelectPalettedFraction(attrib.s); vDepthMask = SelectChannelMask(attrib.t); vTexSampler = attrib.pq; - vTint = vec4(aVertexTint, 1.0); + vTint = aVertexTint; }