From 4c46a850f49413d3210a470c79d7974358ffcfde Mon Sep 17 00:00:00 2001 From: dexyfex Date: Tue, 1 May 2018 18:03:20 +1000 Subject: [PATCH] Improved rendering for nav poly selection, path shader now batches selection lines and tris --- Rendering/Renderer.cs | 67 +++++++++++++++++++++++++++++++- Rendering/Shaders/PathShader.cs | 60 +++++++++++++++++++++-------- WorldForm.cs | 68 ++------------------------------- 3 files changed, 113 insertions(+), 82 deletions(-) diff --git a/Rendering/Renderer.cs b/Rendering/Renderer.cs index 4792f59..aa74391 100644 --- a/Rendering/Renderer.cs +++ b/Rendering/Renderer.cs @@ -757,7 +757,7 @@ namespace CodeWalker.Rendering public void RenderSelectionNavPoly(YnvPoly poly) { ////draw poly triangles - var pcolour = new Color4(0.6f, 0.95f, 0.6f, 1.0f); + var pcolour = new Color4(1.0f, 1.0f, 1.0f, 0.2f); var colourval = (uint)pcolour.ToRgba(); var ynv = poly.Ynv; var ic = poly._RawData.IndexCount; @@ -792,6 +792,71 @@ namespace CodeWalker.Rendering } } + public void RenderSelectionNavPolyOutline(YnvPoly poly, uint colourval) + { + //var colourval = (uint)colour.ToRgba(); + var ynv = poly.Ynv; + var ic = poly._RawData.IndexCount; + var startid = poly._RawData.IndexID; + var endid = startid + ic; + var lastid = endid - 1; + var vc = ynv.Vertices.Count; + var startind = ynv.Indices[startid]; + + ////draw poly outline + VertexTypePC v = new VertexTypePC(); + v.Colour = colourval; + VertexTypePC v0 = new VertexTypePC(); + for (int id = startid; id < endid; id++) + { + var ind = ynv.Indices[id]; + if (ind >= vc) + { continue; } + + v.Position = ynv.Vertices[ind]; + SelectionLineVerts.Add(v); + if (id == startid) + { + v0 = v; + } + else + { + SelectionLineVerts.Add(v); + } + if (id == lastid) + { + SelectionLineVerts.Add(v0); + } + } + + + ////draw poly triangles + //VertexTypePC v0 = new VertexTypePC(); + //VertexTypePC v1 = new VertexTypePC(); + //VertexTypePC v2 = new VertexTypePC(); + //v0.Position = ynv.Vertices[startind]; + //v0.Colour = colourval; + //v1.Colour = colourval; + //v2.Colour = colourval; + //int tricount = ic - 2; + //for (int t = 0; t < tricount; t++) + //{ + // int tid = startid + t; + // int ind1 = ynv.Indices[tid + 1]; + // int ind2 = ynv.Indices[tid + 2]; + // if ((ind1 >= vc) || (ind2 >= vc)) + // { continue; } + // v1.Position = ynv.Vertices[ind1]; + // v2.Position = ynv.Vertices[ind2]; + // Renderer.SelectionTriVerts.Add(v0); + // Renderer.SelectionTriVerts.Add(v1); + // Renderer.SelectionTriVerts.Add(v2); + // Renderer.SelectionTriVerts.Add(v0); + // Renderer.SelectionTriVerts.Add(v2); + // Renderer.SelectionTriVerts.Add(v1); + //} + } + public void RenderSelectionGeometry(MapSelectionMode mode) { diff --git a/Rendering/Shaders/PathShader.cs b/Rendering/Shaders/PathShader.cs index 72d1e65..45732e4 100644 --- a/Rendering/Shaders/PathShader.cs +++ b/Rendering/Shaders/PathShader.cs @@ -178,16 +178,31 @@ namespace CodeWalker.Rendering SetInputLayout(context, VertexType.PC); SetSceneVars(context, camera, null, lights); - vertices.Clear(); - foreach (var vert in verts) + int drawn = 0; + int tricount = verts.Count / 3; + int maxcount = vertices.StructCount / 3; + while (drawn < tricount) { - vertices.Add(vert); - } - vertices.Update(context); - vertices.SetVSResource(context, 0); + vertices.Clear(); + + int offset = drawn*3; + int bcount = Math.Min(tricount - drawn, maxcount); + for (int i = 0; i < bcount; i++) + { + int t = offset + (i * 3); + vertices.Add(verts[t + 0]); + vertices.Add(verts[t + 1]); + vertices.Add(verts[t + 2]); + } + drawn += bcount; + + vertices.Update(context); + vertices.SetVSResource(context, 0); + + context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList; + context.Draw(vertices.CurrentCount, 0); + } - context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList; - context.Draw(vertices.CurrentCount, 0); } public void RenderLines(DeviceContext context, List verts, Camera camera, ShaderGlobalLights lights) @@ -197,16 +212,29 @@ namespace CodeWalker.Rendering SetInputLayout(context, VertexType.PC); SetSceneVars(context, camera, null, lights); - vertices.Clear(); - foreach (var vert in verts) + int drawn = 0; + int linecount = verts.Count / 2; + int maxcount = vertices.StructCount / 2; + while (drawn < linecount) { - vertices.Add(vert); - } - vertices.Update(context); - vertices.SetVSResource(context, 0); + vertices.Clear(); - context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.LineList; - context.Draw(vertices.CurrentCount, 0); + int offset = drawn * 2; + int bcount = Math.Min(linecount - drawn, maxcount); + for (int i = 0; i < bcount; i++) + { + int t = offset + (i * 2); + vertices.Add(verts[t + 0]); + vertices.Add(verts[t + 1]); + } + drawn += bcount; + + vertices.Update(context); + vertices.SetVSResource(context, 0); + + context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.LineList; + context.Draw(vertices.CurrentCount, 0); + } } diff --git a/WorldForm.cs b/WorldForm.cs index 6219a66..c39cf6f 100644 --- a/WorldForm.cs +++ b/WorldForm.cs @@ -1435,7 +1435,8 @@ namespace CodeWalker if (selectionItem.NavPoly != null) { Renderer.RenderSelectionNavPoly(selectionItem.NavPoly); - //return;//don't render a selection box for nav mesh? + Renderer.RenderSelectionNavPolyOutline(selectionItem.NavPoly, cgrn); + return;//don't render a selection box for nav poly } if (selectionItem.NavPoint != null) { @@ -2717,70 +2718,7 @@ namespace CodeWalker if ((CurMouseHit.NavPoly != null) && MouseSelectEnabled) { - var colour = Color4.White; - var colourval = (uint)colour.ToRgba(); - var poly = CurMouseHit.NavPoly; - var ynv = poly.Ynv; - var ic = poly._RawData.IndexCount; - var startid = poly._RawData.IndexID; - var endid = startid + ic; - var lastid = endid - 1; - var vc = ynv.Vertices.Count; - var startind = ynv.Indices[startid]; - - ////draw poly outline - VertexTypePC v = new VertexTypePC(); - v.Colour = colourval; - VertexTypePC v0 = new VertexTypePC(); - for (int id = startid; id < endid; id++) - { - var ind = ynv.Indices[id]; - if (ind >= vc) - { continue; } - - v.Position = ynv.Vertices[ind]; - Renderer.SelectionLineVerts.Add(v); - if (id == startid) - { - v0 = v; - } - else - { - Renderer.SelectionLineVerts.Add(v); - } - if (id == lastid) - { - Renderer.SelectionLineVerts.Add(v0); - } - } - - - ////draw poly triangles - //VertexTypePC v0 = new VertexTypePC(); - //VertexTypePC v1 = new VertexTypePC(); - //VertexTypePC v2 = new VertexTypePC(); - //v0.Position = ynv.Vertices[startind]; - //v0.Colour = colourval; - //v1.Colour = colourval; - //v2.Colour = colourval; - //int tricount = ic - 2; - //for (int t = 0; t < tricount; t++) - //{ - // int tid = startid + t; - // int ind1 = ynv.Indices[tid + 1]; - // int ind2 = ynv.Indices[tid + 2]; - // if ((ind1 >= vc) || (ind2 >= vc)) - // { continue; } - // v1.Position = ynv.Vertices[ind1]; - // v2.Position = ynv.Vertices[ind2]; - // Renderer.SelectionTriVerts.Add(v0); - // Renderer.SelectionTriVerts.Add(v1); - // Renderer.SelectionTriVerts.Add(v2); - // Renderer.SelectionTriVerts.Add(v0); - // Renderer.SelectionTriVerts.Add(v2); - // Renderer.SelectionTriVerts.Add(v1); - //} - + Renderer.RenderSelectionNavPolyOutline(CurMouseHit.NavPoly, 0xFFFFFFFF); } }