@@ -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}
0 commit comments