Add depth buffer support to RgbaColorRenderer.
This commit is contained in:
@@ -47,15 +47,15 @@ namespace OpenRA.Graphics
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawLine(float2 start, float2 end, float width, Color startColor, Color endColor)
|
public void DrawLine(float3 start, float3 end, float width, Color startColor, Color endColor)
|
||||||
{
|
{
|
||||||
renderer.CurrentBatchRenderer = this;
|
renderer.CurrentBatchRenderer = this;
|
||||||
|
|
||||||
if (nv + 6 > renderer.TempBufferSize)
|
if (nv + 6 > renderer.TempBufferSize)
|
||||||
Flush();
|
Flush();
|
||||||
|
|
||||||
var delta = (end - start) / (end - start).Length;
|
var delta = (end - start) / (end - start).XY.Length;
|
||||||
var corner = width / 2 * new float2(-delta.Y, delta.X);
|
var corner = width / 2 * new float3(-delta.Y, delta.X, delta.Z);
|
||||||
|
|
||||||
startColor = Util.PremultiplyAlpha(startColor);
|
startColor = Util.PremultiplyAlpha(startColor);
|
||||||
var sr = startColor.R / 255.0f;
|
var sr = startColor.R / 255.0f;
|
||||||
@@ -77,14 +77,14 @@ namespace OpenRA.Graphics
|
|||||||
vertices[nv++] = new Vertex(start - corner + Offset, sr, sg, sb, sa, 0, 0);
|
vertices[nv++] = new Vertex(start - corner + Offset, sr, sg, sb, sa, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawLine(float2 start, float2 end, float width, Color color)
|
public void DrawLine(float3 start, float3 end, float width, Color color)
|
||||||
{
|
{
|
||||||
renderer.CurrentBatchRenderer = this;
|
renderer.CurrentBatchRenderer = this;
|
||||||
|
|
||||||
if (nv + 6 > renderer.TempBufferSize)
|
if (nv + 6 > renderer.TempBufferSize)
|
||||||
Flush();
|
Flush();
|
||||||
|
|
||||||
var delta = (end - start) / (end - start).Length;
|
var delta = (end - start) / (end - start).XY.Length;
|
||||||
var corner = width / 2 * new float2(-delta.Y, delta.X);
|
var corner = width / 2 * new float2(-delta.Y, delta.X);
|
||||||
|
|
||||||
color = Util.PremultiplyAlpha(color);
|
color = Util.PremultiplyAlpha(color);
|
||||||
@@ -102,20 +102,21 @@ namespace OpenRA.Graphics
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Calculate the intersection of two lines.
|
/// Calculate the 2D intersection of two lines.
|
||||||
/// Will behave badly if the lines are parallel
|
/// Will behave badly if the lines are parallel.
|
||||||
|
/// Z position is the average of a and b (ignores actual intersection point if it exists)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
float2 IntersectionOf(float2 a, float2 da, float2 b, float2 db)
|
float3 IntersectionOf(float3 a, float3 da, float3 b, float3 db)
|
||||||
{
|
{
|
||||||
var crossA = a.X * (a.Y + da.Y) - a.Y * (a.X + da.X);
|
var crossA = a.X * (a.Y + da.Y) - a.Y * (a.X + da.X);
|
||||||
var crossB = b.X * (b.Y + db.Y) - b.Y * (b.X + db.X);
|
var crossB = b.X * (b.Y + db.Y) - b.Y * (b.X + db.X);
|
||||||
var x = da.X * crossB - db.X * crossA;
|
var x = da.X * crossB - db.X * crossA;
|
||||||
var y = da.Y * crossB - db.Y * crossA;
|
var y = da.Y * crossB - db.Y * crossA;
|
||||||
var d = da.X * db.Y - da.Y * db.X;
|
var d = da.X * db.Y - da.Y * db.X;
|
||||||
return new float2(x, y) / d;
|
return new float3(x / d, y / d, 0.5f * (a.Z + b.Z));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawDisconnectedLine(IEnumerable<float2> points, float width, Color color)
|
void DrawDisconnectedLine(IEnumerable<float3> points, float width, Color color)
|
||||||
{
|
{
|
||||||
using (var e = points.GetEnumerator())
|
using (var e = points.GetEnumerator())
|
||||||
{
|
{
|
||||||
@@ -132,7 +133,7 @@ namespace OpenRA.Graphics
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawConnectedLine(float2[] points, float width, Color color, bool closed)
|
void DrawConnectedLine(float3[] points, float width, Color color, bool closed)
|
||||||
{
|
{
|
||||||
// Not a line
|
// Not a line
|
||||||
if (points.Length < 2)
|
if (points.Length < 2)
|
||||||
@@ -154,8 +155,8 @@ namespace OpenRA.Graphics
|
|||||||
|
|
||||||
var start = points[0];
|
var start = points[0];
|
||||||
var end = points[1];
|
var end = points[1];
|
||||||
var dir = (end - start) / (end - start).Length;
|
var dir = (end - start) / (end - start).XY.Length;
|
||||||
var corner = width / 2 * new float2(-dir.Y, dir.X);
|
var corner = width / 2 * new float3(-dir.Y, dir.X, dir.Z);
|
||||||
|
|
||||||
// Corners for start of line segment
|
// Corners for start of line segment
|
||||||
var ca = start - corner;
|
var ca = start - corner;
|
||||||
@@ -165,8 +166,8 @@ namespace OpenRA.Graphics
|
|||||||
if (closed)
|
if (closed)
|
||||||
{
|
{
|
||||||
var prev = points[points.Length - 1];
|
var prev = points[points.Length - 1];
|
||||||
var prevDir = (start - prev) / (start - prev).Length;
|
var prevDir = (start - prev) / (start - prev).XY.Length;
|
||||||
var prevCorner = width / 2 * new float2(-prevDir.Y, prevDir.X);
|
var prevCorner = width / 2 * new float3(-prevDir.Y, prevDir.X, prevDir.Z);
|
||||||
ca = IntersectionOf(start - prevCorner, prevDir, start - corner, dir);
|
ca = IntersectionOf(start - prevCorner, prevDir, start - corner, dir);
|
||||||
cb = IntersectionOf(start + prevCorner, prevDir, start + corner, dir);
|
cb = IntersectionOf(start + prevCorner, prevDir, start + corner, dir);
|
||||||
}
|
}
|
||||||
@@ -175,8 +176,8 @@ namespace OpenRA.Graphics
|
|||||||
for (var i = 0; i < limit; i++)
|
for (var i = 0; i < limit; i++)
|
||||||
{
|
{
|
||||||
var next = points[(i + 2) % points.Length];
|
var next = points[(i + 2) % points.Length];
|
||||||
var nextDir = (next - end) / (next - end).Length;
|
var nextDir = (next - end) / (next - end).XY.Length;
|
||||||
var nextCorner = width / 2 * new float2(-nextDir.Y, nextDir.X);
|
var nextCorner = width / 2 * new float3(-nextDir.Y, nextDir.X, nextDir.Z);
|
||||||
|
|
||||||
// Vertices for the corners joining start-end to end-next
|
// Vertices for the corners joining start-end to end-next
|
||||||
var cc = closed || i < limit ? IntersectionOf(end + corner, dir, end + nextCorner, nextDir) : end + corner;
|
var cc = closed || i < limit ? IntersectionOf(end + corner, dir, end + nextCorner, nextDir) : end + corner;
|
||||||
@@ -204,33 +205,43 @@ namespace OpenRA.Graphics
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void DrawLine(IEnumerable<float2> points, float width, Color color, bool connectSegments = false)
|
public void DrawLine(IEnumerable<float2> points, float width, Color color, bool connectSegments = false)
|
||||||
|
{
|
||||||
|
DrawLine(points.Select(p => new float3(p, 0)), width, color, connectSegments);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawLine(IEnumerable<float3> points, float width, Color color, bool connectSegments = false)
|
||||||
{
|
{
|
||||||
if (!connectSegments)
|
if (!connectSegments)
|
||||||
DrawDisconnectedLine(points, width, color);
|
DrawDisconnectedLine(points, width, color);
|
||||||
else
|
else
|
||||||
DrawConnectedLine(points as float2[] ?? points.ToArray(), width, color, false);
|
DrawConnectedLine(points as float3[] ?? points.ToArray(), width, color, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawPolygon(float2[] vertices, float width, Color color)
|
public void DrawPolygon(float3[] vertices, float width, Color color)
|
||||||
{
|
{
|
||||||
DrawConnectedLine(vertices, width, color, true);
|
DrawConnectedLine(vertices, width, color, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawRect(float2 tl, float2 br, float width, Color color)
|
public void DrawPolygon(float2[] vertices, float width, Color color)
|
||||||
{
|
{
|
||||||
var tr = new float2(br.X, tl.Y);
|
DrawConnectedLine(vertices.Select(v => new float3(v, 0)).ToArray(), width, color, true);
|
||||||
var bl = new float2(tl.X, br.Y);
|
}
|
||||||
|
|
||||||
|
public void DrawRect(float3 tl, float3 br, float width, Color color)
|
||||||
|
{
|
||||||
|
var tr = new float3(br.X, tl.Y, tl.Z);
|
||||||
|
var bl = new float3(tl.X, br.Y, br.Z);
|
||||||
DrawPolygon(new[] { tl, tr, br, bl }, width, color);
|
DrawPolygon(new[] { tl, tr, br, bl }, width, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void FillRect(float2 tl, float2 br, Color color)
|
public void FillRect(float3 tl, float3 br, Color color)
|
||||||
{
|
{
|
||||||
var tr = new float2(br.X, tl.Y);
|
var tr = new float3(br.X, tl.Y, tl.Z);
|
||||||
var bl = new float2(tl.X, br.Y);
|
var bl = new float3(tl.X, br.Y, br.Z);
|
||||||
FillRect(tl, tr, br, bl, color);
|
FillRect(tl, tr, br, bl, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void FillRect(float2 a, float2 b, float2 c, float2 d, Color color)
|
public void FillRect(float3 a, float3 b, float3 c, float3 d, Color color)
|
||||||
{
|
{
|
||||||
renderer.CurrentBatchRenderer = this;
|
renderer.CurrentBatchRenderer = this;
|
||||||
|
|
||||||
@@ -251,25 +262,34 @@ namespace OpenRA.Graphics
|
|||||||
vertices[nv++] = new Vertex(a + Offset, cr, cg, cb, ca, 0, 0);
|
vertices[nv++] = new Vertex(a + Offset, cr, cg, cb, ca, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void FillEllipse(RectangleF r, Color color, int vertices = 32)
|
public void FillEllipse(float3 tl, float3 br, Color color, int vertices = 32)
|
||||||
{
|
{
|
||||||
// TODO: Create an ellipse polygon instead
|
// TODO: Create an ellipse polygon instead
|
||||||
var a = (r.Right - r.Left) / 2;
|
var a = (br.X - tl.X) / 2;
|
||||||
var b = (r.Bottom - r.Top) / 2;
|
var b = (br.Y - tl.Y) / 2;
|
||||||
var xc = (r.Right + r.Left) / 2;
|
var xc = (br.X + tl.X) / 2;
|
||||||
var yc = (r.Bottom + r.Top) / 2;
|
var yc = (br.Y + tl.Y) / 2;
|
||||||
for (var y = r.Top; y <= r.Bottom; y++)
|
for (var y = tl.Y; y <= br.Y; y++)
|
||||||
{
|
{
|
||||||
|
var z = float2.Lerp(tl.Z, br.Z, (y - tl.Y) / (br.Y - tl.Y));
|
||||||
var dx = a * (float)Math.Sqrt(1 - (y - yc) * (y - yc) / b / b);
|
var dx = a * (float)Math.Sqrt(1 - (y - yc) * (y - yc) / b / b);
|
||||||
DrawLine(new float2(xc - dx, y), new float2(xc + dx, y), 1, color);
|
DrawLine(new float3(xc - dx, y, z), new float3(xc + dx, y, z), 1, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetViewportParams(Size screen, float zoom, int2 scroll)
|
public void SetViewportParams(Size screen, float depthScale, float depthOffset, float zoom, int2 scroll)
|
||||||
{
|
{
|
||||||
shader.SetVec("Scroll", scroll.X, scroll.Y);
|
shader.SetVec("Scroll", scroll.X, scroll.Y, scroll.Y);
|
||||||
shader.SetVec("r1", zoom * 2f / screen.Width, -zoom * 2f / screen.Height);
|
shader.SetVec("r1",
|
||||||
shader.SetVec("r2", -1, 1);
|
zoom * 2f / screen.Width,
|
||||||
|
-zoom * 2f / screen.Height,
|
||||||
|
-depthScale * zoom / screen.Height);
|
||||||
|
shader.SetVec("r2", -1, 1, 1 - depthOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetDepthPreviewEnabled(bool enabled)
|
||||||
|
{
|
||||||
|
shader.SetBool("EnableDepthPreview", enabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,6 +127,7 @@ namespace OpenRA.Graphics
|
|||||||
{
|
{
|
||||||
Game.Renderer.WorldSpriteRenderer.SetDepthPreviewEnabled(devTrait.Value.ShowDepthPreview);
|
Game.Renderer.WorldSpriteRenderer.SetDepthPreviewEnabled(devTrait.Value.ShowDepthPreview);
|
||||||
Game.Renderer.WorldRgbaSpriteRenderer.SetDepthPreviewEnabled(devTrait.Value.ShowDepthPreview);
|
Game.Renderer.WorldRgbaSpriteRenderer.SetDepthPreviewEnabled(devTrait.Value.ShowDepthPreview);
|
||||||
|
Game.Renderer.WorldRgbaColorRenderer.SetDepthPreviewEnabled(devTrait.Value.ShowDepthPreview);
|
||||||
}
|
}
|
||||||
|
|
||||||
RefreshPalette();
|
RefreshPalette();
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ namespace OpenRA
|
|||||||
lastResolution = Resolution;
|
lastResolution = Resolution;
|
||||||
RgbaSpriteRenderer.SetViewportParams(Resolution, 0f, 0f, 1f, int2.Zero);
|
RgbaSpriteRenderer.SetViewportParams(Resolution, 0f, 0f, 1f, int2.Zero);
|
||||||
SpriteRenderer.SetViewportParams(Resolution, 0f, 0f, 1f, int2.Zero);
|
SpriteRenderer.SetViewportParams(Resolution, 0f, 0f, 1f, int2.Zero);
|
||||||
RgbaColorRenderer.SetViewportParams(Resolution, 1f, int2.Zero);
|
RgbaColorRenderer.SetViewportParams(Resolution, 0f, 0f, 1f, int2.Zero);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If zoom evaluates as different due to floating point weirdness that's OK, setting the parameters again is harmless.
|
// If zoom evaluates as different due to floating point weirdness that's OK, setting the parameters again is harmless.
|
||||||
@@ -130,7 +130,7 @@ namespace OpenRA
|
|||||||
WorldRgbaSpriteRenderer.SetViewportParams(Resolution, depthScale, depthOffset, zoom, scroll);
|
WorldRgbaSpriteRenderer.SetViewportParams(Resolution, depthScale, depthOffset, zoom, scroll);
|
||||||
WorldSpriteRenderer.SetViewportParams(Resolution, depthScale, depthOffset, zoom, scroll);
|
WorldSpriteRenderer.SetViewportParams(Resolution, depthScale, depthOffset, zoom, scroll);
|
||||||
WorldVoxelRenderer.SetViewportParams(Resolution, zoom, scroll);
|
WorldVoxelRenderer.SetViewportParams(Resolution, zoom, scroll);
|
||||||
WorldRgbaColorRenderer.SetViewportParams(Resolution, zoom, scroll);
|
WorldRgbaColorRenderer.SetViewportParams(Resolution, depthScale, depthOffset, zoom, scroll);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,7 +70,9 @@ namespace OpenRA.Widgets
|
|||||||
|
|
||||||
public static void FillEllipseWithColor(Rectangle r, Color c)
|
public static void FillEllipseWithColor(Rectangle r, Color c)
|
||||||
{
|
{
|
||||||
Game.Renderer.RgbaColorRenderer.FillEllipse(new RectangleF(r.X, r.Y, r.Width, r.Height), c);
|
var tl = new float2(r.Left, r.Top);
|
||||||
|
var br = new float2(r.Right, r.Bottom);
|
||||||
|
Game.Renderer.RgbaColorRenderer.FillEllipse(tl, br, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int[] GetBorderSizes(string collection)
|
public static int[] GetBorderSizes(string collection)
|
||||||
|
|||||||
@@ -75,9 +75,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
{
|
{
|
||||||
var tl = wr.ScreenPosition(i.CenterPosition - new WVec(r.Length, r.Length, 0));
|
var tl = wr.ScreenPosition(i.CenterPosition - new WVec(r.Length, r.Length, 0));
|
||||||
var br = wr.ScreenPosition(i.CenterPosition + new WVec(r.Length, r.Length, 0));
|
var br = wr.ScreenPosition(i.CenterPosition + new WVec(r.Length, r.Length, 0));
|
||||||
var rect = RectangleF.FromLTRB(tl.X, tl.Y, br.X, br.Y);
|
Game.Renderer.WorldRgbaColorRenderer.FillEllipse(tl, br, Color.FromArgb((int)alpha, i.Color));
|
||||||
|
|
||||||
Game.Renderer.WorldRgbaColorRenderer.FillEllipse(rect, Color.FromArgb((int)alpha, i.Color));
|
|
||||||
|
|
||||||
alpha -= rangeStep;
|
alpha -= rangeStep;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,18 @@
|
|||||||
varying vec4 vColor;
|
varying vec4 vColor;
|
||||||
|
uniform bool EnableDepthPreview;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
|
float depth = gl_FragCoord.z;
|
||||||
|
|
||||||
|
// Convert to window coords
|
||||||
|
gl_FragDepth = 0.5 * depth + 0.5;
|
||||||
|
|
||||||
|
if (EnableDepthPreview)
|
||||||
|
{
|
||||||
|
// Front of the depth buffer is at 0, but we want to render it as bright
|
||||||
|
gl_FragColor = vec4(vec3(1.0 - gl_FragDepth), 1.0);
|
||||||
|
}
|
||||||
|
else
|
||||||
gl_FragColor = vColor;
|
gl_FragColor = vColor;
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
uniform vec2 Scroll;
|
uniform vec3 Scroll;
|
||||||
uniform vec2 r1, r2;
|
uniform vec3 r1, r2;
|
||||||
|
|
||||||
attribute vec4 aVertexPosition;
|
attribute vec4 aVertexPosition;
|
||||||
attribute vec4 aVertexTexCoord;
|
attribute vec4 aVertexTexCoord;
|
||||||
@@ -7,7 +7,6 @@ varying vec4 vColor;
|
|||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec2 p = (aVertexPosition.xy - Scroll.xy)*r1 + r2;
|
gl_Position = vec4((aVertexPosition.xyz - Scroll.xyz) * r1 + r2, 1);
|
||||||
gl_Position = vec4(p.x,p.y,0,1);
|
|
||||||
vColor = aVertexTexCoord;
|
vColor = aVertexTexCoord;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user