Skip to content

Commit 8c2b1f4

Browse files
committed
reversed z depth because fighting was crazy bad
1 parent 2b562dd commit 8c2b1f4

18 files changed

Lines changed: 66 additions & 36 deletions

File tree

Editor/EditorWindow.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ public EditorWindow(double fps, int width, int height, IFileProvider fileProvide
6666

6767
private void DoLoad()
6868
{
69+
GL.ClipControl(ClipOrigin.LowerLeft, ClipDepthMode.ZeroToOne);
6970
GL.Enable(EnableCap.DepthTest);
70-
GL.DepthFunc(DepthFunction.Less);
71+
GL.DepthFunc(DepthFunction.Greater);
72+
GL.ClearDepth(0.0);
7173

7274
GL.Enable(EnableCap.Blend);
7375
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);

Editor/Widgets/SceneHierarchyWidget.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ private void DrawFlatNode(Actor actor, bool isSearching = false)
309309
{
310310
var btnW = ImGui.CalcTextSize(Settings.EyeIcon).X + style.FramePadding.X * 2;
311311
ImGui.SameLine(rightEdge - btnW);
312-
var isHidden = !actor.IsVisible;
312+
var isHidden = !actor.IsVisibleRecursive;
313313
if (isHidden) ImGui.PushStyleColor(ImGuiCol.Text, Settings.RedColor);
314-
if (ImGui.Button(actor.IsVisible ? Settings.EyeIcon : Settings.EyeSlashIcon)) actor.ToggleVisibility();
314+
if (ImGui.Button(actor.IsVisibleRecursive ? Settings.EyeIcon : Settings.EyeSlashIcon)) actor.ToggleVisibility();
315315
if (isHidden) ImGui.PopStyleColor();
316316
}
317317
ImGui.PopStyleVar();

Editor/Widgets/ViewportWidget.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ private void DrawFooterOverlay(Vector2 contentPos, Vector2 contentSize, float bo
182182
ImGui.PopFont();
183183
}
184184

185+
private bool IsGizmoVisible(CameraComponent camera, in Matrix4x4 matrix)
186+
{
187+
if (ImGuizmo.IsUsing()) return true;
188+
if (camera.ProjectionMode == CameraMode.Orthographic) return true;
189+
190+
var viewPosition = Vector3.Transform(matrix.Translation, camera.ViewMatrix);
191+
return viewPosition.Z < -camera.NearClipPlane;
192+
}
193+
185194
private void DrawComponentControlsOverlay(CameraComponent camera, ActorComponent? component, Vector2 contentPos, Vector2 contentSize)
186195
{
187196
var view = camera.ViewMatrix;
@@ -204,7 +213,7 @@ private void DrawComponentControlsOverlay(CameraComponent camera, ActorComponent
204213
if (manager._splineOverlay.SelectedHandle != -1 && manager._splineOverlay.SelectedSpline is not null)
205214
{
206215
var handleMatrix = manager._splineOverlay.SelectedHandleMatrix;
207-
if (ImGuizmo.Manipulate(ref view.M11, ref proj.M11, OPERATION.TRANSLATE, MODE.WORLD, ref handleMatrix.M11))
216+
if (IsGizmoVisible(camera, handleMatrix) && ImGuizmo.Manipulate(ref view.M11, ref proj.M11, OPERATION.TRANSLATE, MODE.WORLD, ref handleMatrix.M11))
208217
{
209218
manager._splineOverlay.ApplyGizmoMatrix(handleMatrix);
210219
manager._splineOverlay.SelectedSpline.MarkDirty(DirtyFlags.Spline);
@@ -222,7 +231,7 @@ private void DrawComponentControlsOverlay(CameraComponent camera, ActorComponent
222231
{
223232
var matrix = skeleton.BoneMatrices[boneIndex] * mesh.GizmoMatrix;
224233

225-
if (ImGuizmo.Manipulate(ref view.M11, ref proj.M11, _gizmoOperation, MODE.LOCAL, ref matrix.M11))
234+
if (IsGizmoVisible(camera, matrix) && ImGuizmo.Manipulate(ref view.M11, ref proj.M11, _gizmoOperation, MODE.LOCAL, ref matrix.M11))
226235
{
227236
Matrix4x4.Invert(mesh.GizmoMatrix, out var invGizmo);
228237
skeleton.MoveBone(boneIndex, matrix * invGizmo);
@@ -234,7 +243,7 @@ private void DrawComponentControlsOverlay(CameraComponent camera, ActorComponent
234243
case DirectionalLightComponent light:
235244
{
236245
var matrix = light.GizmoMatrix;
237-
if (ImGuizmo.Manipulate(ref view.M11, ref proj.M11, OPERATION.ROTATE_X | OPERATION.ROTATE_Y | OPERATION.ROTATE_SCREEN | OPERATION.TRANSLATE_Z, MODE.LOCAL, ref matrix.M11))
246+
if (IsGizmoVisible(camera, matrix) && ImGuizmo.Manipulate(ref view.M11, ref proj.M11, OPERATION.ROTATE_X | OPERATION.ROTATE_Y | OPERATION.ROTATE_SCREEN | OPERATION.TRANSLATE_Z, MODE.LOCAL, ref matrix.M11))
238247
{
239248
light.ApplyGizmoMatrix(matrix);
240249
}
@@ -243,7 +252,7 @@ private void DrawComponentControlsOverlay(CameraComponent camera, ActorComponent
243252
case SpatialComponent spatial when !_selectMode:
244253
{
245254
var matrix = spatial.GizmoMatrix;
246-
if (ImGuizmo.Manipulate(ref view.M11, ref proj.M11, _gizmoOperation, _localSpace ? MODE.LOCAL : MODE.WORLD, ref matrix.M11))
255+
if (IsGizmoVisible(camera, matrix) && ImGuizmo.Manipulate(ref view.M11, ref proj.M11, _gizmoOperation, _localSpace ? MODE.LOCAL : MODE.WORLD, ref matrix.M11))
247256
{
248257
spatial.ApplyGizmoMatrix(matrix);
249258
}

Snooper/Core/Containers/Renderbuffer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class Renderbuffer(int width, int height, RenderbufferStorage storage, bo
2323
RenderbufferStorage.Rg32f => 8,
2424
RenderbufferStorage.Rgb32f => 12,
2525
RenderbufferStorage.Rgba32f => 16,
26+
RenderbufferStorage.DepthComponent16 => 2,
27+
RenderbufferStorage.DepthComponent24 => 3,
28+
RenderbufferStorage.DepthComponent32f => 4,
29+
RenderbufferStorage.Depth24Stencil8 => 4,
30+
RenderbufferStorage.Depth32fStencil8 => 5,
2631
_ => 4
2732
};
2833

Snooper/Core/Containers/Resources/CullingResources.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ public void Cull<TInstanceData>(ReadOnlySpan<CullView> views, ShaderStorageBuffe
9191
{
9292
var matrix = views[i].ViewProjection;
9393
var b = i * 6;
94-
_planes[b + 0] = new Plane(matrix.M14 + matrix.M11, matrix.M24 + matrix.M21, matrix.M34 + matrix.M31, matrix.M44 + matrix.M41); // Near
95-
_planes[b + 1] = new Plane(matrix.M14 - matrix.M11, matrix.M24 - matrix.M21, matrix.M34 - matrix.M31, matrix.M44 - matrix.M41); // Far
96-
_planes[b + 2] = new Plane(matrix.M14 + matrix.M12, matrix.M24 + matrix.M22, matrix.M34 + matrix.M32, matrix.M44 + matrix.M42); // Left
97-
_planes[b + 3] = new Plane(matrix.M14 - matrix.M12, matrix.M24 - matrix.M22, matrix.M34 - matrix.M32, matrix.M44 - matrix.M42); // Right
98-
_planes[b + 4] = new Plane(matrix.M14 + matrix.M13, matrix.M24 + matrix.M23, matrix.M34 + matrix.M33, matrix.M44 + matrix.M43); // Bottom
99-
_planes[b + 5] = new Plane(matrix.M14 - matrix.M13, matrix.M24 - matrix.M23, matrix.M34 - matrix.M33, matrix.M44 - matrix.M43); // Top
94+
_planes[b + 0] = new Plane(matrix.M14 + matrix.M11, matrix.M24 + matrix.M21, matrix.M34 + matrix.M31, matrix.M44 + matrix.M41); // Left
95+
_planes[b + 1] = new Plane(matrix.M14 - matrix.M11, matrix.M24 - matrix.M21, matrix.M34 - matrix.M31, matrix.M44 - matrix.M41); // Right
96+
_planes[b + 2] = new Plane(matrix.M14 + matrix.M12, matrix.M24 + matrix.M22, matrix.M34 + matrix.M32, matrix.M44 + matrix.M42); // Bottom
97+
_planes[b + 3] = new Plane(matrix.M14 - matrix.M12, matrix.M24 - matrix.M22, matrix.M34 - matrix.M32, matrix.M44 - matrix.M42); // Top
98+
_planes[b + 4] = new Plane(matrix.M13, matrix.M23, matrix.M33, matrix.M43); // z >= 0
99+
_planes[b + 5] = new Plane(matrix.M14 - matrix.M13, matrix.M24 - matrix.M23, matrix.M34 - matrix.M33, matrix.M44 - matrix.M43); // w - z >= 0
100100

101101
_lodReferences[i] = new Vector4(views[i].LodReferencePosition, views[i].LodProjectionScale);
102102
_lodOrthoExtents[i] = views[i].LodOrthoExtent;

Snooper/Rendering/Components/Camera/CameraComponent.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,17 @@ public void UpdateMatrices()
137137
Matrix4x4.Decompose(WorldMatrix, out _, out var rotation, out var position);
138138

139139
ViewMatrix = Matrix4x4.CreateLookAt(position, position - Vector3.Transform(Settings.ForwardVector, rotation), Vector3.Transform(Settings.UpVector, rotation));
140-
ProjectionMatrix = ProjectionMode switch
140+
var projection = ProjectionMode switch
141141
{
142142
CameraMode.Orthographic => Matrix4x4.CreateOrthographic(OrthoWidth * AspectRatio, OrthoWidth, OrthoNearClipPlane, OrthoFarClipPlane),
143143
CameraMode.Perspective => Matrix4x4.CreatePerspectiveFieldOfView(FieldOfViewRadians, AspectRatio, PerspectiveNearClipPlane, PerspectiveFarClipPlane),
144144
_ => throw new ArgumentOutOfRangeException()
145145
};
146+
147+
var reverse = Matrix4x4.Identity;
148+
reverse.M33 = -1.0f;
149+
reverse.M43 = 1.0f;
150+
ProjectionMatrix = projection * reverse;
146151
}
147152

148153
public override string Icon => "\uf030";

Snooper/Rendering/Containers/Framebuffers/DeferredFramebuffer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class DeferredFramebuffer(int originalWidth, int originalHeight) : Frameb
1414
private readonly ResizableTexture2D _color = new(originalWidth, originalHeight, name: "Deferred - Color");
1515
private readonly ResizableTexture2D _specular = new(originalWidth, originalHeight, name: "Deferred - Specular");
1616
private readonly PickingTexture _picking = new(originalWidth, originalHeight, name: "Deferred - Picking");
17-
private readonly Renderbuffer _depth = new(originalWidth, originalHeight, RenderbufferStorage.Depth24Stencil8, false);
17+
private readonly Renderbuffer _depth = new(originalWidth, originalHeight, RenderbufferStorage.DepthComponent32f, false);
1818

1919
public override void Generate()
2020
{
@@ -59,7 +59,7 @@ public override void Generate()
5959
DrawBuffersEnum.ColorAttachment3,
6060
DrawBuffersEnum.ColorAttachment4,
6161
]);
62-
GL.NamedFramebufferRenderbuffer(Handle, FramebufferAttachment.DepthStencilAttachment, RenderbufferTarget.Renderbuffer, _depth);
62+
GL.NamedFramebufferRenderbuffer(Handle, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, _depth);
6363

6464
CheckStatus();
6565
}

Snooper/Rendering/Containers/Framebuffers/ForwardFramebuffer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ForwardFramebuffer(int originalWidth, int originalHeight) : Framebu
1111

1212
private readonly ResizableTexture2D _color = new(originalWidth, originalHeight, name: "Forward - Color");
1313
private readonly PickingTexture _picking = new(originalWidth, originalHeight, name: "Forward - Picking");
14-
private readonly Renderbuffer _depth = new(originalWidth, originalHeight, RenderbufferStorage.Depth24Stencil8, false);
14+
private readonly Renderbuffer _depth = new(originalWidth, originalHeight, RenderbufferStorage.DepthComponent32f, false);
1515

1616
public override void Generate()
1717
{
@@ -30,7 +30,7 @@ public override void Generate()
3030
GL.NamedFramebufferTexture(Handle, FramebufferAttachment.ColorAttachment0, _color, 0);
3131
GL.NamedFramebufferTexture(Handle, FramebufferAttachment.ColorAttachment1, _picking, 0);
3232
GL.NamedFramebufferDrawBuffers(Handle, 2, [DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.ColorAttachment1]);
33-
GL.NamedFramebufferRenderbuffer(Handle, FramebufferAttachment.DepthStencilAttachment, RenderbufferTarget.Renderbuffer, _depth);
33+
GL.NamedFramebufferRenderbuffer(Handle, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, _depth);
3434

3535
CheckStatus();
3636
}

Snooper/Rendering/Containers/Framebuffers/MaskFramebuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class MaskFramebuffer(int originalWidth, int originalHeight) : Framebuffe
99
public override int Width => _depth.Width;
1010
public override int Height => _depth.Height;
1111

12-
private readonly ResizableTexture2D _depth = new(originalWidth, originalHeight, SizedInternalFormat.DepthComponent16, PixelFormat.DepthComponent, PixelType.Float, "Mask - Depth");
12+
private readonly ResizableTexture2D _depth = new(originalWidth, originalHeight, SizedInternalFormat.DepthComponent32f, PixelFormat.DepthComponent, PixelType.Float, "Mask - Depth");
1313

1414
public override void Generate()
1515
{

Snooper/Rendering/Containers/Framebuffers/SunCascades.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ public ShadowMapView[] Update(CameraComponent camera, DirectionalLightComponent
9494
-radius, radius,
9595
0.0f, depthRange);
9696

97-
// System.Numerics emits a D3D style [0, 1] clip depth while this context still runs GL's
98-
// default [-1, 1] clip range, so window depth only ever spans [0.5, 1.0]
99-
var depthScale = 0.5f / depthRange;
97+
// System.Numerics emits a [0, 1] clip depth and the context runs ZERO_TO_ONE clip control,
98+
// so window depth spans the full [0, 1] over the ortho depth range
99+
var depthScale = 1.0f / depthRange;
100100

101101
Views[i] = new ShadowMapView(viewMatrix, projectionMatrix, FirstSlot + i, radius, texelWorldSize, depthScale, far);
102102
}

0 commit comments

Comments
 (0)