private void ReccursiveDrawWithClipping(UIElement element) { // if the element is not visible, we also remove all its children if (!element.IsVisible) { return; } var renderer = rendererManager.GetRenderer(element); renderingContext.DepthBias = element.DepthBias; // render the clipping region of the element if (element.ClipToBounds) { // flush current elements batch.End(); // render the clipping region batch.Begin(ref uiSystem.ViewProjectionInternal, GraphicsDevice.BlendStates.ColorDisabled, uiSystem.IncreaseStencilValueState, renderingContext.StencilTestReferenceValue); renderer.RenderClipping(element, renderingContext); batch.End(); // update context and restart the batch renderingContext.StencilTestReferenceValue += 1; batch.Begin(ref uiSystem.ViewProjectionInternal, GraphicsDevice.BlendStates.AlphaBlend, uiSystem.KeepStencilValueState, renderingContext.StencilTestReferenceValue); } // render the design of the element renderer.RenderColor(element, renderingContext); // render the children foreach (var child in element.VisualChildrenCollection) { ReccursiveDrawWithClipping(child); } // clear the element clipping region from the stencil buffer if (element.ClipToBounds) { // flush current elements batch.End(); renderingContext.DepthBias = element.MaxChildrenDepthBias; // render the clipping region batch.Begin(ref uiSystem.ViewProjectionInternal, GraphicsDevice.BlendStates.ColorDisabled, uiSystem.DecreaseStencilValueState, renderingContext.StencilTestReferenceValue); renderer.RenderClipping(element, renderingContext); batch.End(); // update context and restart the batch renderingContext.StencilTestReferenceValue -= 1; batch.Begin(ref uiSystem.ViewProjectionInternal, GraphicsDevice.BlendStates.AlphaBlend, uiSystem.KeepStencilValueState, renderingContext.StencilTestReferenceValue); } }