// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Core/Math/Color.h"
#include "Engine/Scripting/ScriptingType.h"
struct SpriteHandle;
struct TextLayoutOptions;
struct Matrix;
struct Matrix3x3;
struct Viewport;
struct TextRange;
class Font;
class GPUPipelineState;
class GPUTexture;
class GPUTextureView;
class GPUContext;
class RenderTask;
class MaterialBase;
class TextureBase;
///
/// Rendering 2D shapes and text using Graphics Device.
///
API_CLASS(Static) class FLAXENGINE_API Render2D
{
DECLARE_SCRIPTING_TYPE_NO_SPAWN(Render2D);
public:
///
/// The rendering features and options flags.
///
API_ENUM(Attributes="Flags") enum class RenderingFeatures
{
///
/// The none.
///
None = 0,
///
/// Enables automatic geometry vertices snapping to integer coordinates in screen space. Reduces aliasing and sampling artifacts. Might be disabled for 3D projection viewport or for complex UI transformations.
///
VertexSnapping = 1,
};
public:
///
/// Checks if interface is during rendering phrase (Draw calls may be performed without failing).
///
/// True if is during rendering, otherwise false/
static bool IsRendering();
///
/// Gets the current rendering viewport.
///
/// The viewport
static const Viewport& GetViewport();
///
/// The active rendering features flags.
///
API_FIELD() static RenderingFeatures Features;
public:
///
/// Called when frame rendering begins by the graphics device.
///
static void BeginFrame();
///
/// Begins the rendering phrase.
///
/// The GPU commands context to use.
/// The output target.
/// The depth buffer.
API_FUNCTION() static void Begin(GPUContext* context, GPUTexture* output, GPUTexture* depthBuffer = nullptr);
///
/// Begins the rendering phrase.
///
/// The GPU commands context to use.
/// The output target.
/// The depth buffer.
/// The View*Projection matrix. Allows to render GUI in 3D or with custom transformations.
API_FUNCTION() static void Begin(GPUContext* context, GPUTexture* output, GPUTexture* depthBuffer, API_PARAM(Ref) const Matrix& viewProjection);
///
/// Begins the rendering phrase.
///
/// The GPU commands context to use.
/// The output target.
/// The depth buffer.
/// The output viewport.
API_FUNCTION() static void Begin(GPUContext* context, GPUTextureView* output, GPUTextureView* depthBuffer, API_PARAM(Ref) const Viewport& viewport);
///
/// Begins the rendering phrase.
///
/// The GPU commands context to use.
/// The output target.
/// The depth buffer.
/// The output viewport.
/// The View*Projection matrix. Allows to render GUI in 3D or with custom transformations.
API_FUNCTION() static void Begin(GPUContext* context, GPUTextureView* output, GPUTextureView* depthBuffer, API_PARAM(Ref) const Viewport& viewport, API_PARAM(Ref) const Matrix& viewProjection);
///
/// Ends the rendering phrase.
///
API_FUNCTION() static void End();
///
/// Called when frame rendering ends by the graphics device.
///
static void EndFrame();
public:
///
/// Pushes transformation layer.
///
/// The transformation to apply.
API_FUNCTION() static void PushTransform(API_PARAM(Ref) const Matrix3x3& transform);
///
/// Peeks the current transformation layer.
///
/// The output transformation to apply combined from all the transformations together (pushed into the transformation stack).
API_FUNCTION() static void PeekTransform(API_PARAM(Out) Matrix3x3& transform);
///
/// Pops transformation layer.
///
API_FUNCTION() static void PopTransform();
///
/// Pushes clipping rectangle mask.
///
/// The axis aligned clipping mask rectangle.
API_FUNCTION() static void PushClip(API_PARAM(Ref) const Rectangle& clipRect);
///
/// Peeks the current clipping rectangle mask.
///
/// The output axis aligned clipping mask rectangle combined from all the masks together (pushed into the masking stack).
API_FUNCTION() static void PeekClip(API_PARAM(Out) Rectangle& clipRect);
///
/// Pops clipping rectangle mask.
///
API_FUNCTION() static void PopClip();
public:
///
/// Draws a text.
///
/// The font to use.
/// The text to render.
/// The text color.
/// The text location.
/// The custom material for font characters rendering. It must contain texture parameter named Font used to sample font texture.
API_FUNCTION() static void DrawText(Font* font, const StringView& text, const Color& color, const Vector2& location, MaterialBase* customMaterial = nullptr);
///
/// Draws a text.
///
/// The font to use.
/// The text to render.
/// The input text range (substring range of the input text parameter).
/// The text color.
/// The text location.
/// The custom material for font characters rendering. It must contain texture parameter named Font used to sample font texture.
API_FUNCTION() static void DrawText(Font* font, const StringView& text, API_PARAM(Ref) const TextRange& textRange, const Color& color, const Vector2& location, MaterialBase* customMaterial = nullptr);
///
/// Draws a text with formatting.
///
/// The font to use.
/// The text to render.
/// The text color.
/// The text layout properties.
/// The custom material for font characters rendering. It must contain texture parameter named Font used to sample font texture.
API_FUNCTION() static void DrawText(Font* font, const StringView& text, const Color& color, API_PARAM(Ref) const TextLayoutOptions& layout, MaterialBase* customMaterial = nullptr);
///
/// Draws a text with formatting.
///
/// The font to use.
/// The text to render.
/// The input text range (substring range of the input text parameter).
/// The text color.
/// The text layout properties.
/// The custom material for font characters rendering. It must contain texture parameter named Font used to sample font texture.
API_FUNCTION() static void DrawText(Font* font, const StringView& text, API_PARAM(Ref) const TextRange& textRange, const Color& color, API_PARAM(Ref) const TextLayoutOptions& layout, MaterialBase* customMaterial = nullptr);
///
/// Fills a rectangle area.
///
/// The rectangle to fill.
/// The color to use.
API_FUNCTION() static void FillRectangle(const Rectangle& rect, const Color& color);
///
/// Fills a rectangle area.
///
/// The rectangle to fill.
/// The color to use for upper left vertex.
/// The color to use for upper right vertex.
/// The color to use for bottom right vertex.
/// The color to use for bottom left vertex.
API_FUNCTION() static void FillRectangle(const Rectangle& rect, const Color& color1, const Color& color2, const Color& color3, const Color& color4);
///
/// Draws a rectangle borders.
///
/// The rectangle to draw.
/// The color to use.
/// The line thickness.
API_FUNCTION() FORCE_INLINE static void DrawRectangle(const Rectangle& rect, const Color& color, float thickness = 1.0f)
{
DrawRectangle(rect, color, color, color, color, thickness);
}
///
/// Draws a rectangle borders.
///
/// The rectangle to fill.
/// The color to use for upper left vertex.
/// The color to use for upper right vertex.
/// The color to use for bottom right vertex.
/// The color to use for bottom left vertex.
/// The line thickness.
API_FUNCTION() static void DrawRectangle(const Rectangle& rect, const Color& color1, const Color& color2, const Color& color3, const Color& color4, float thickness = 1.0f);
///
/// Draws the render target.
///
/// The render target handle to draw.
/// The rectangle to draw.
/// The color to multiply all texture pixels.
API_FUNCTION() static void DrawTexture(GPUTextureView* rt, const Rectangle& rect, const Color& color = Color::White);
///
/// Draws the texture.
///
/// The texture to draw.
/// The rectangle to draw.
/// The color to multiply all texture pixels.
API_FUNCTION() static void DrawTexture(GPUTexture* t, const Rectangle& rect, const Color& color = Color::White);
///
/// Draws the texture.
///
/// The texture to draw.
/// The rectangle to draw.
/// The color to multiply all texture pixels.
API_FUNCTION() static void DrawTexture(TextureBase* t, const Rectangle& rect, const Color& color = Color::White);
///
/// Draws a sprite.
///
/// The sprite to draw.
/// The rectangle to draw.
/// The color to multiply all texture pixels.
API_FUNCTION() static void DrawSprite(const SpriteHandle& spriteHandle, const Rectangle& rect, const Color& color = Color::White);
///
/// Draws the texture (uses point sampler).
///
/// The texture to draw.
/// The rectangle to draw.
/// The color to multiply all texture pixels.
API_FUNCTION() static void DrawTexturePoint(GPUTexture* t, const Rectangle& rect, const Color& color = Color::White);
///
/// Draws a sprite (uses point sampler).
///
/// The sprite to draw.
/// The rectangle to draw.
/// The color to multiply all texture pixels.
API_FUNCTION() static void DrawSpritePoint(const SpriteHandle& spriteHandle, const Rectangle& rect, const Color& color = Color::White);
///
/// Performs custom rendering.
///
/// The texture to use.
/// The rectangle area to draw.
/// The custom pipeline state to use (input must match default Render2D vertex shader and can use single texture).
/// The color to multiply all texture pixels.
API_FUNCTION() static void DrawCustom(GPUTexture* t, const Rectangle& rect, GPUPipelineState* ps, const Color& color = Color::White);
///
/// Draws a line.
///
/// The start point.
/// The end point.
/// The line color.
/// The line thickness.
API_FUNCTION() FORCE_INLINE static void DrawLine(const Vector2& p1, const Vector2& p2, const Color& color, float thickness = 1.0f)
{
DrawLine(p1, p2, color, color, thickness);
}
///
/// Draws a line.
///
/// The start point.
/// The end point.
/// The line start color.
/// The line end color.
/// The line thickness.
API_FUNCTION() static void DrawLine(const Vector2& p1, const Vector2& p2, const Color& color1, const Color& color2, float thickness = 1.0f);
///
/// Draws a Bezier curve.
///
/// The start point.
/// The first control point.
/// The second control point.
/// The end point.
/// The line color
/// The line thickness.
API_FUNCTION() static void DrawBezier(const Vector2& p1, const Vector2& p2, const Vector2& p3, const Vector2& p4, const Color& color, float thickness = 1.0f);
///
/// Draws the GUI material.
///
/// The material to render. Must be a GUI material type.
/// The target rectangle to draw.
/// The color to use.
API_FUNCTION() static void DrawMaterial(MaterialBase* material, const Rectangle& rect, const Color& color);
///
/// Draws the background blur.
///
/// The target rectangle to draw (blurs its background).
/// The blur strength defines how blurry the background is. Larger numbers increase blur, resulting in a larger runtime cost on the GPU.
API_FUNCTION() static void DrawBlur(const Rectangle& rect, float blurStrength);
};