@@ -12,19 +12,98 @@ namespace FlaxEditor.Gizmo
|
|||||||
[HideInEditor]
|
[HideInEditor]
|
||||||
public class GridGizmo : GizmoBase
|
public class GridGizmo : GizmoBase
|
||||||
{
|
{
|
||||||
private bool _enabled = true;
|
[HideInEditor]
|
||||||
|
private sealed class Renderer : PostProcessEffect
|
||||||
|
{
|
||||||
|
private IntPtr _debugDrawContext;
|
||||||
|
|
||||||
|
public Renderer()
|
||||||
|
{
|
||||||
|
Order = -100;
|
||||||
|
UseSingleTarget = true;
|
||||||
|
Location = PostProcessEffectLocation.BeforeForwardPass;
|
||||||
|
}
|
||||||
|
|
||||||
|
~Renderer()
|
||||||
|
{
|
||||||
|
if (_debugDrawContext != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
DebugDraw.FreeContext(_debugDrawContext);
|
||||||
|
_debugDrawContext = IntPtr.Zero;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Render(GPUContext context, ref RenderContext renderContext, GPUTexture input, GPUTexture output)
|
||||||
|
{
|
||||||
|
Profiler.BeginEventGPU("Editor Grid");
|
||||||
|
|
||||||
|
if (_debugDrawContext == IntPtr.Zero)
|
||||||
|
_debugDrawContext = DebugDraw.AllocateContext();
|
||||||
|
DebugDraw.SetContext(_debugDrawContext);
|
||||||
|
DebugDraw.UpdateContext(_debugDrawContext, 1.0f / Mathf.Max(Engine.FramesPerSecond, 1));
|
||||||
|
|
||||||
|
var viewPos = (Vector3)renderContext.View.Position;
|
||||||
|
var plane = new Plane(Vector3.Zero, Vector3.UnitY);
|
||||||
|
var dst = CollisionsHelper.DistancePlanePoint(ref plane, ref viewPos);
|
||||||
|
|
||||||
|
float space, size;
|
||||||
|
if (dst <= 500.0f)
|
||||||
|
{
|
||||||
|
space = 50;
|
||||||
|
size = 8000;
|
||||||
|
}
|
||||||
|
else if (dst <= 2000.0f)
|
||||||
|
{
|
||||||
|
space = 100;
|
||||||
|
size = 8000;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
space = 1000;
|
||||||
|
size = 100000;
|
||||||
|
}
|
||||||
|
|
||||||
|
Color color = Color.Gray * 0.7f;
|
||||||
|
int count = (int)(size / space);
|
||||||
|
|
||||||
|
Vector3 start = new Vector3(0, 0, size * -0.5f);
|
||||||
|
Vector3 end = new Vector3(0, 0, size * 0.5f);
|
||||||
|
|
||||||
|
for (int i = 0; i <= count; i++)
|
||||||
|
{
|
||||||
|
start.X = end.X = i * space + start.Z;
|
||||||
|
DebugDraw.DrawLine(start, end, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
start = new Vector3(size * -0.5f, 0, 0);
|
||||||
|
end = new Vector3(size * 0.5f, 0, 0);
|
||||||
|
|
||||||
|
for (int i = 0; i <= count; i++)
|
||||||
|
{
|
||||||
|
start.Z = end.Z = i * space + start.X;
|
||||||
|
DebugDraw.DrawLine(start, end, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
DebugDraw.Draw(ref renderContext, input.View(), null, true);
|
||||||
|
DebugDraw.SetContext(IntPtr.Zero);
|
||||||
|
|
||||||
|
Profiler.EndEventGPU();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Renderer _renderer;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether this <see cref="GridGizmo"/> is enabled.
|
/// Gets or sets a value indicating whether this <see cref="GridGizmo"/> is enabled.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Enabled
|
public bool Enabled
|
||||||
{
|
{
|
||||||
get => _enabled;
|
get => _renderer.Enabled;
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (_enabled != value)
|
if (_renderer.Enabled != value)
|
||||||
{
|
{
|
||||||
_enabled = value;
|
_renderer.Enabled = value;
|
||||||
EnabledChanged?.Invoke(this);
|
EnabledChanged?.Invoke(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,55 +121,13 @@ namespace FlaxEditor.Gizmo
|
|||||||
public GridGizmo(IGizmoOwner owner)
|
public GridGizmo(IGizmoOwner owner)
|
||||||
: base(owner)
|
: base(owner)
|
||||||
{
|
{
|
||||||
|
_renderer = new Renderer();
|
||||||
|
owner.RenderTask.AddCustomPostFx(_renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
~GridGizmo()
|
||||||
public override void Draw(ref RenderContext renderContext)
|
|
||||||
{
|
{
|
||||||
if (!Enabled)
|
FlaxEngine.Object.Destroy(ref _renderer);
|
||||||
return;
|
|
||||||
|
|
||||||
var viewPos = Owner.ViewPosition;
|
|
||||||
var plane = new Plane(Vector3.Zero, Vector3.UnitY);
|
|
||||||
var dst = CollisionsHelper.DistancePlanePoint(ref plane, ref viewPos);
|
|
||||||
|
|
||||||
float space, size;
|
|
||||||
if (dst <= 500.0f)
|
|
||||||
{
|
|
||||||
space = 50;
|
|
||||||
size = 8000;
|
|
||||||
}
|
|
||||||
else if (dst <= 2000.0f)
|
|
||||||
{
|
|
||||||
space = 100;
|
|
||||||
size = 8000;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
space = 1000;
|
|
||||||
size = 100000;
|
|
||||||
}
|
|
||||||
|
|
||||||
Color color = Color.Gray * 0.7f;
|
|
||||||
int count = (int)(size / space);
|
|
||||||
|
|
||||||
Vector3 start = new Vector3(0, 0, size * -0.5f);
|
|
||||||
Vector3 end = new Vector3(0, 0, size * 0.5f);
|
|
||||||
|
|
||||||
for (int i = 0; i <= count; i++)
|
|
||||||
{
|
|
||||||
start.X = end.X = i * space + start.Z;
|
|
||||||
DebugDraw.DrawLine(start, end, color);
|
|
||||||
}
|
|
||||||
|
|
||||||
start = new Vector3(size * -0.5f, 0, 0);
|
|
||||||
end = new Vector3(size * 0.5f, 0, 0);
|
|
||||||
|
|
||||||
for (int i = 0; i <= count; i++)
|
|
||||||
{
|
|
||||||
start.Z = end.Z = i * space + start.X;
|
|
||||||
DebugDraw.DrawLine(start, end, color);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -466,7 +466,7 @@ inline void DrawText3D(const DebugText3D& t, const RenderContext& renderContext,
|
|||||||
Matrix::Multiply(fw, vp, m);
|
Matrix::Multiply(fw, vp, m);
|
||||||
Render2D::Begin(context, target, depthBuffer, viewport, m);
|
Render2D::Begin(context, target, depthBuffer, viewport, m);
|
||||||
const StringView text(t.Text.Get(), t.Text.Count() - 1);
|
const StringView text(t.Text.Get(), t.Text.Count() - 1);
|
||||||
Render2D::DrawText(DebugDrawFont->CreateFont(t.Size), text, t.Color, Vector2::Zero);
|
Render2D::DrawText(DebugDrawFont->CreateFont((float)t.Size), text, t.Color, Vector2::Zero);
|
||||||
Render2D::End();
|
Render2D::End();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -777,7 +777,7 @@ void DebugDraw::Draw(RenderContext& renderContext, GPUTextureView* target, GPUTe
|
|||||||
context->BindSR(0, renderContext.Buffers->DepthBuffer);
|
context->BindSR(0, renderContext.Buffers->DepthBuffer);
|
||||||
const bool enableDepthWrite = data.EnableDepthTest;
|
const bool enableDepthWrite = data.EnableDepthTest;
|
||||||
|
|
||||||
context->SetRenderTarget(depthBuffer ? depthBuffer : *renderContext.Buffers->DepthBuffer, target);
|
context->SetRenderTarget(depthBuffer ? depthBuffer : (data.EnableDepthTest ? nullptr : renderContext.Buffers->DepthBuffer->View()), target);
|
||||||
|
|
||||||
// Lines
|
// Lines
|
||||||
if (depthTestLines.VertexCount)
|
if (depthTestLines.VertexCount)
|
||||||
@@ -859,12 +859,12 @@ void DebugDraw::Draw(RenderContext& renderContext, GPUTextureView* target, GPUTe
|
|||||||
for (auto& t : Context->DebugDrawDefault.DefaultText2D)
|
for (auto& t : Context->DebugDrawDefault.DefaultText2D)
|
||||||
{
|
{
|
||||||
const StringView text(t.Text.Get(), t.Text.Count() - 1);
|
const StringView text(t.Text.Get(), t.Text.Count() - 1);
|
||||||
Render2D::DrawText(DebugDrawFont->CreateFont(t.Size), text, t.Color, t.Position);
|
Render2D::DrawText(DebugDrawFont->CreateFont((float)t.Size), text, t.Color, t.Position);
|
||||||
}
|
}
|
||||||
for (auto& t : Context->DebugDrawDefault.OneFrameText2D)
|
for (auto& t : Context->DebugDrawDefault.OneFrameText2D)
|
||||||
{
|
{
|
||||||
const StringView text(t.Text.Get(), t.Text.Count() - 1);
|
const StringView text(t.Text.Get(), t.Text.Count() - 1);
|
||||||
Render2D::DrawText(DebugDrawFont->CreateFont(t.Size), text, t.Color, t.Position);
|
Render2D::DrawText(DebugDrawFont->CreateFont((float)t.Size), text, t.Color, t.Position);
|
||||||
}
|
}
|
||||||
Render2D::End();
|
Render2D::End();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user