// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Core/Math/Color.h"
#include "Engine/Scripting/ScriptingType.h"
#include "Engine/Core/Types/Span.h"
#include "Engine/Core/Math/Vector2.h"
#include "Engine/Core/Math/Matrix.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);
///
/// 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,
};
struct CustomData
{
Matrix ViewProjection;
Float2 ViewSize;
};
public:
///
/// Checks if interface is during rendering phrase (Draw calls may be performed without failing).
///
static bool IsRendering();
///
/// Gets the current rendering viewport.
///
static const Viewport& GetViewport();
///
/// The active rendering features flags.
///
API_FIELD() static RenderingFeatures Features;
///
/// 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();
///
/// Pushes tint color.
///
/// The tint color.
/// Multiply by the last tint on the stack.
API_FUNCTION() static void PushTint(API_PARAM(Ref) const Color& tint, bool inherit = true);
///
/// Peeks the current tint color.
///
/// The output tint color.
API_FUNCTION() static void PeekTint(API_PARAM(Out) Color& tint);
///
/// Pops tint color.
///
API_FUNCTION() static void PopTint();
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 Float2& 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 Float2& 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);
///
/// Draws the texture using 9-slicing.
///
/// The texture to draw.
/// The rectangle to draw.
/// The borders for 9-slicing (inside rectangle, ordered: left, right, top, bottom).
/// The borders UVs for 9-slicing (inside rectangle UVs, ordered: left, right, top, bottom).
/// The color to multiply all texture pixels.
API_FUNCTION() static void Draw9SlicingTexture(TextureBase* t, const Rectangle& rect, const Float4& border, const Float4& borderUVs, const Color& color = Color::White);
///
/// Draws the texture using 9-slicing (uses point sampler).
///
/// The texture to draw.
/// The rectangle to draw.
/// The borders for 9-slicing (inside rectangle, ordered: left, right, top, bottom).
/// The borders UVs for 9-slicing (inside rectangle UVs, ordered: left, right, top, bottom).
/// The color to multiply all texture pixels.
API_FUNCTION() static void Draw9SlicingTexturePoint(TextureBase* t, const Rectangle& rect, const Float4& border, const Float4& borderUVs, const Color& color = Color::White);
///
/// Draws a sprite using 9-slicing.
///
/// The sprite to draw.
/// The rectangle to draw.
/// The borders for 9-slicing (inside rectangle, ordered: left, right, top, bottom).
/// The borders UVs for 9-slicing (inside rectangle UVs, ordered: left, right, top, bottom).
/// The color to multiply all texture pixels.
API_FUNCTION() static void Draw9SlicingSprite(const SpriteHandle& spriteHandle, const Rectangle& rect, const Float4& border, const Float4& borderUVs, const Color& color = Color::White);
///
/// Draws a sprite using 9-slicing (uses point sampler).
///
/// The sprite to draw.
/// The rectangle to draw.
/// The borders for 9-slicing (inside rectangle, ordered: left, right, top, bottom).
/// The borders UVs for 9-slicing (inside rectangle UVs, ordered: left, right, top, bottom).
/// The color to multiply all texture pixels.
API_FUNCTION() static void Draw9SlicingSpritePoint(const SpriteHandle& spriteHandle, const Rectangle& rect, const Float4& border, const Float4& borderUVs, 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 Float2& p1, const Float2& 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 Float2& p1, const Float2& 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 Float2& p1, const Float2& p2, const Float2& p3, const Float2& 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);
///
/// Draws vertices array.
///
/// The texture.
/// The vertices array.
/// The uvs array.
API_FUNCTION() static void DrawTexturedTriangles(GPUTexture* t, const Span& vertices, const Span& uvs);
///
/// Draws vertices array.
///
/// The texture.
/// The vertices array.
/// The uvs array.
/// The color.
API_FUNCTION() static void DrawTexturedTriangles(GPUTexture* t, const Span& vertices, const Span& uvs, const Color& color);
///
/// Draws vertices array.
///
/// The texture.
/// The vertices array.
/// The uvs array.
/// The colors array.
API_FUNCTION() static void DrawTexturedTriangles(GPUTexture* t, const Span& vertices, const Span& uvs, const Span& colors);
///
/// Draws indexed vertices array.
///
/// The texture.
/// The indices array.
/// The vertices array.
/// The uvs array.
/// The colors array.
API_FUNCTION() static void DrawTexturedTriangles(GPUTexture* t, const Span& indices, const Span& vertices, const Span& uvs, const Span& colors);
///
/// Draws vertices array.
///
/// The vertices array.
/// The colors array.
/// If true alpha blending will be enabled.
API_FUNCTION() static void FillTriangles(const Span& vertices, const Span& colors, bool useAlpha);
///
/// Fills a triangular area.
///
/// The first point.
/// The second point.
/// The third point.
/// The color.
API_FUNCTION() static void FillTriangle(const Float2& p0, const Float2& p1, const Float2& p2, const Color& color);
};