Skip to content

Commit 346db8b

Browse files
committed
Fixing shadow rendering
Excluding option
1 parent b9271fa commit 346db8b

4 files changed

Lines changed: 110 additions & 20 deletions

File tree

RenderGl/NativeSceneRenderer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public sealed class MeshRenderCommand
6363

6464
public bool BlendTexture { get; init; } = true;
6565

66+
public bool CastsBedShadow { get; init; } = true;
67+
6668
public Color Color { get; init; }
6769

6870
public bool ForceCullBackFaces { get; init; } = true;

RenderGl/RenderHelper.cs

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public static class RenderHelper
5454

5555
private const float GL_REPLACE = (float)0x1E01;
5656

57+
private static int suppressBedShadowCastingDepth;
58+
5759
public static void ExtendLineEnds(ref Vector3 start, ref Vector3 end, double length)
5860
{
5961
// extend both sides
@@ -85,6 +87,78 @@ public static void PrepareFor3DLineRender(bool doDepthTest)
8587
}
8688
}
8789

90+
public static MeshRenderCommand CreateBedShadowCommand(MeshRenderCommand command)
91+
{
92+
if (command == null)
93+
{
94+
return null;
95+
}
96+
97+
return new MeshRenderCommand
98+
{
99+
Mesh = command.Mesh,
100+
Color = command.Color,
101+
Transform = command.Transform,
102+
RenderType = command.RenderType,
103+
MeshToViewTransform = command.MeshToViewTransform,
104+
WireFrameColor = command.WireFrameColor,
105+
MeshChanged = command.MeshChanged,
106+
BlendTexture = command.BlendTexture,
107+
AllowBspRendering = command.AllowBspRendering,
108+
ForceCullBackFaces = false,
109+
IsSelected = command.IsSelected,
110+
OverrideFaceColors = command.OverrideFaceColors,
111+
AlphaMultiplier = command.AlphaMultiplier,
112+
Unlit = command.Unlit,
113+
CastsBedShadow = command.CastsBedShadow,
114+
};
115+
}
116+
117+
public static bool ResolveBedShadowCasting(bool castsBedShadow)
118+
{
119+
return castsBedShadow
120+
&& suppressBedShadowCastingDepth == 0;
121+
}
122+
123+
public static bool ShouldRenderInBedShadow(MeshRenderCommand command, RectangleDouble bedBounds)
124+
{
125+
if (command?.Mesh == null
126+
|| !command.CastsBedShadow)
127+
{
128+
return false;
129+
}
130+
131+
switch (command.RenderType)
132+
{
133+
case RenderTypes.Shaded:
134+
case RenderTypes.Outlines:
135+
case RenderTypes.NonManifold:
136+
case RenderTypes.Wireframe:
137+
case RenderTypes.Polygons:
138+
break;
139+
140+
default:
141+
return false;
142+
}
143+
144+
var bounds = command.Mesh.GetAxisAlignedBoundingBox(command.Transform);
145+
if (bounds.MaxXYZ.Z <= 0)
146+
{
147+
return false;
148+
}
149+
150+
return !(bounds.MaxXYZ.X < bedBounds.Left
151+
|| bounds.MinXYZ.X > bedBounds.Right
152+
|| bounds.MaxXYZ.Y < bedBounds.Bottom
153+
|| bounds.MinXYZ.Y > bedBounds.Top);
154+
}
155+
156+
public static IDisposable SuppressBedShadowCasting()
157+
{
158+
suppressBedShadowCastingDepth++;
159+
return new DisposableScope(() => suppressBedShadowCastingDepth--);
160+
}
161+
88162
public static void Render(Mesh meshToRender,
89163
Color partColor,
90164
RenderTypes renderType = RenderTypes.Shaded,
@@ -93,11 +167,12 @@ public static void Render(Mesh meshToRender,
93167
Action meshChanged = null,
94168
bool blendTexture = true,
95169
bool forceCullBackFaces = true,
170+
bool castsBedShadow = true,
96171
bool isSelected = false,
97172
bool overrideFaceColors = false,
98173
float alphaMultiplier = 1.0f)
99174
{
100-
Render(meshToRender, partColor, Matrix4X4.Identity, renderType, meshToViewTransform, wireFrameColor, meshChanged, blendTexture, forceCullBackFaces: forceCullBackFaces, isSelected: isSelected, overrideFaceColors: overrideFaceColors, alphaMultiplier: alphaMultiplier);
175+
Render(meshToRender, partColor, Matrix4X4.Identity, renderType, meshToViewTransform, wireFrameColor, meshChanged, blendTexture, forceCullBackFaces: forceCullBackFaces, castsBedShadow: castsBedShadow, isSelected: isSelected, overrideFaceColors: overrideFaceColors, alphaMultiplier: alphaMultiplier);
101176
}
102177

103178
public static void Render(Mesh meshToRender,
@@ -110,6 +185,7 @@ public static void Render(Mesh meshToRender,
110185
bool blendTexture = true,
111186
bool allowBspRendering = false,
112187
bool forceCullBackFaces = true,
188+
bool castsBedShadow = true,
113189
bool isSelected = false,
114190
bool overrideFaceColors = false,
115191
float alphaMultiplier = 1.0f)
@@ -130,6 +206,7 @@ public static void Render(Mesh meshToRender,
130206
BlendTexture = blendTexture,
131207
AllowBspRendering = allowBspRendering,
132208
ForceCullBackFaces = forceCullBackFaces,
209+
CastsBedShadow = ResolveBedShadowCasting(castsBedShadow),
133210
IsSelected = isSelected,
134211
OverrideFaceColors = overrideFaceColors,
135212
AlphaMultiplier = alphaMultiplier,
@@ -469,5 +546,27 @@ public static void UnsetGlContext()
469546

470547
GL.PopAttrib();
471548
}
549+
550+
private sealed class DisposableScope : IDisposable
551+
{
552+
private readonly Action onDispose;
553+
private bool disposed;
554+
555+
public DisposableScope(Action onDispose)
556+
{
557+
this.onDispose = onDispose;
558+
}
559+
560+
public void Dispose()
561+
{
562+
if (disposed)
563+
{
564+
return;
565+
}
566+
567+
disposed = true;
568+
onDispose?.Invoke();
569+
}
570+
}
472571
}
473572
}

RenderGl/Renderer/Graphics2DGpu.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ public void RenderTransformedPath(Matrix4X4 transform, IVertexSource path, Color
600600
RenderType = RenderTypes.Shaded,
601601
BlendTexture = false,
602602
ForceCullBackFaces = false,
603+
CastsBedShadow = false,
603604
Unlit = true,
604605
};
605606

VorticeD3D/NativeSceneEffects.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ namespace MatterHackers.RenderGl
4848
public partial class VorticeD3DGl
4949
{
5050
private const int BedCompositeTextureSize = 2048;
51-
private const int BedShadowTextureSize = 512;
52-
private const float BedShadowStrength = .35f;
51+
private const int BedShadowTextureSize = 2048;
52+
private const float BedShadowStrength = .70f;
5353
private const float BedShadowViewDistance = 1000;
5454
private const int SceneEffectVertexFloatStride = SceneEdgeShaderDataPlugin.TotalVertexFloatStride;
5555
private const int SceneEffectVertexStride = SceneEffectVertexFloatStride * sizeof(float);
@@ -758,30 +758,18 @@ private void RenderBedShadowMask(BedRenderCommand bedCommand)
758758
continue;
759759
}
760760

761-
RenderFlatMask(command, command.Transform * shadowView, shadowProjection, AggColor.Black, enableDepthTest: false);
761+
// Glyph meshes can have mixed winding on their caps and sides; forcing the
762+
// shadow mask to render without culling avoids clipped letter silhouettes.
763+
var shadowCommand = RenderHelper.CreateBedShadowCommand(command);
764+
RenderFlatMask(shadowCommand, shadowCommand.Transform * shadowView, shadowProjection, AggColor.Black, enableDepthTest: false);
762765
}
763766

764767
UnbindSceneTextures();
765768
}
766769

767770
private static bool ShouldRenderCommandIntoBedShadow(MeshRenderCommand command, RectangleDouble bedBounds)
768771
{
769-
if (command?.Mesh == null
770-
|| !SceneRenderModeUtilities.RequiresSceneMeshPass(command.RenderType))
771-
{
772-
return false;
773-
}
774-
775-
var bounds = command.Mesh.GetAxisAlignedBoundingBox(command.Transform);
776-
if (bounds.MaxXYZ.Z <= 0)
777-
{
778-
return false;
779-
}
780-
781-
return !(bounds.MaxXYZ.X < bedBounds.Left
782-
|| bounds.MinXYZ.X > bedBounds.Right
783-
|| bounds.MaxXYZ.Y < bedBounds.Bottom
784-
|| bounds.MinXYZ.Y > bedBounds.Top);
772+
return RenderHelper.ShouldRenderInBedShadow(command, bedBounds);
785773
}
786774

787775
private void RenderBedBlurPass(ID3D11ShaderResourceView sourceTexture, ColorTextureTarget destinationTarget, float directionX, float directionY)

0 commit comments

Comments
 (0)