Add Sprite Render actor for sprites drawing

This commit is contained in:
Wojtek Figat
2021-02-23 13:23:22 +01:00
parent 7fd542950f
commit 19c83e3b6e
13 changed files with 377 additions and 9 deletions

BIN
Content/Editor/SpriteMaterial.flax (Stored with Git LFS) Normal file

Binary file not shown.

View File

@@ -97,6 +97,11 @@ namespace FlaxEditor
/// </summary>
public static string DefaultSkyCubeTexture = "Editor/SimplySky";
/// <summary>
/// The default sprite material.
/// </summary>
public static string DefaultSpriteMaterial = "Editor/SpriteMaterial";
/// <summary>
/// The IES Profile assets preview material.
/// </summary>
@@ -112,6 +117,16 @@ namespace FlaxEditor
/// </summary>
public static string VertexColorsPreviewMaterial = "Editor/Gizmo/VertexColorsPreviewMaterial";
/// <summary>
/// The Flax icon texture.
/// </summary>
public static string FlaxIconTexture = "Engine/Textures/FlaxIcon";
/// <summary>
/// The Flax icon (blue) texture.
/// </summary>
public static string FlaxIconBlueTexture = "Engine/Textures/FlaxIconBlue";
/// <summary>
/// The icon lists used by editor from the SegMDL2 font.
/// </summary>

View File

@@ -0,0 +1,63 @@
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using FlaxEngine;
namespace FlaxEditor.SceneGraph.Actors
{
/// <summary>
/// Scene tree node for <see cref="SpriteRender"/> actor type.
/// </summary>
/// <seealso cref="ActorNode" />
[HideInEditor]
public sealed class SpriteRenderNode : ActorNode
{
/// <inheritdoc />
public SpriteRenderNode(Actor actor)
: base(actor)
{
}
/// <inheritdoc />
public override bool RayCastSelf(ref RayCastData ray, out float distance, out Vector3 normal)
{
SpriteRender sprite = (SpriteRender)Actor;
Vector3 viewPosition = ray.View.Position;
Vector3 viewDirection = ray.View.Direction;
Matrix m1, m2, m3, world;
var size = sprite.Size;
Matrix.Scaling(size.X, size.Y, 1.0f, out m1);
var transform = sprite.Transform;
if (sprite.FaceCamera)
{
var up = Vector3.Up;
Matrix.Billboard(ref transform.Translation, ref viewPosition, ref up, ref viewDirection, out m2);
Matrix.Multiply(ref m1, ref m2, out m3);
Matrix.Scaling(ref transform.Scale, out m1);
Matrix.Multiply(ref m1, ref m3, out world);
}
else
{
transform.GetWorld(out m2);
Matrix.Multiply(ref m1, ref m2, out world);
}
OrientedBoundingBox bounds;
bounds.Extents = Vector3.Half;
bounds.Transformation = world;
normal = -ray.Ray.Direction;
return bounds.Intersects(ref ray.Ray, out distance);
}
/// <inheritdoc />
public override void PostSpawn()
{
base.PostSpawn();
// Setup for default values
var text = (SpriteRender)Actor;
text.Material = FlaxEngine.Content.LoadAsyncInternal<MaterialBase>(EditorAssets.DefaultSpriteMaterial);
text.Image = FlaxEngine.Content.LoadAsyncInternal<Texture>(EditorAssets.FlaxIconTexture);
}
}
}

View File

@@ -70,6 +70,7 @@ namespace FlaxEditor.SceneGraph
CustomNodesTypes.Add(typeof(SplineCollider), typeof(ColliderNode));
CustomNodesTypes.Add(typeof(SplineRopeBody), typeof(ActorNode));
CustomNodesTypes.Add(typeof(NavMesh), typeof(ActorNode));
CustomNodesTypes.Add(typeof(SpriteRender), typeof(SpriteRenderNode));
}
/// <summary>

View File

@@ -197,12 +197,12 @@ namespace FlaxEditor.SceneGraph
}
/// <summary>
/// The ray.
/// The ray (for intersection raycasting).
/// </summary>
public Ray Ray;
/// <summary>
/// The camera view ray.
/// The camera view ray (camera position and direction).
/// </summary>
public Ray View;

View File

@@ -110,6 +110,7 @@ namespace FlaxEditor.Windows
new KeyValuePair<string, Type>("UI Control", typeof(UIControl)),
new KeyValuePair<string, Type>("UI Canvas", typeof(UICanvas)),
new KeyValuePair<string, Type>("Text Render", typeof(TextRender)),
new KeyValuePair<string, Type>("Sprite Render", typeof(SpriteRender)),
}
},
};

View File

@@ -1,7 +1,6 @@
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using System;
using System.Collections.Generic;
using FlaxEditor.GUI.ContextMenu;
using FlaxEditor.SceneGraph;
using FlaxEngine;

View File

@@ -173,6 +173,7 @@ namespace FlaxEditor.Windows
groupGui.AddChild(CreateActorItem("UI Control", typeof(UIControl)));
groupGui.AddChild(CreateActorItem("UI Canvas", typeof(UICanvas)));
groupGui.AddChild(CreateActorItem("Text Render", typeof(TextRender)));
groupGui.AddChild(CreateActorItem("Sprite Render", typeof(SpriteRender)));
actorGroups.SelectedTabIndex = 1;
}

View File

@@ -372,7 +372,7 @@ void Mesh::Render(GPUContext* context) const
context->DrawIndexedInstanced(_triangles * 3, 1, 0, 0, 0);
}
void Mesh::Draw(const RenderContext& renderContext, MaterialBase* material, const Matrix& world, StaticFlags flags, bool receiveDecals) const
void Mesh::Draw(const RenderContext& renderContext, MaterialBase* material, const Matrix& world, StaticFlags flags, bool receiveDecals, DrawPass drawModes, float perInstanceRandom) const
{
if (!material || !material->IsSurface())
return;
@@ -399,8 +399,8 @@ void Mesh::Draw(const RenderContext& renderContext, MaterialBase* material, cons
drawCall.Surface.Skinning = nullptr;
drawCall.Surface.LODDitherFactor = 0.0f;
drawCall.WorldDeterminantSign = Math::FloatSelect(world.RotDeterminant(), 1, -1);
drawCall.PerInstanceRandom = 0.0f;
renderContext.List->AddDrawCall(DrawPass::Default, flags, drawCall, receiveDecals);
drawCall.PerInstanceRandom = perInstanceRandom;
renderContext.List->AddDrawCall(drawModes, flags, drawCall, receiveDecals);
}
void Mesh::Draw(const RenderContext& renderContext, const DrawInfo& info, float lodDitherFactor) const

View File

@@ -459,7 +459,9 @@ public:
/// <param name="world">The world transformation of the model.</param>
/// <param name="flags">The object static flags.</param>
/// <param name="receiveDecals">True if rendered geometry can receive decals, otherwise false.</param>
API_FUNCTION() void Draw(API_PARAM(Ref) const RenderContext& renderContext, MaterialBase* material, API_PARAM(Ref) const Matrix& world, StaticFlags flags = StaticFlags::None, bool receiveDecals = true) const;
/// <param name="drawModes">The draw passes to use for rendering this object.</param>
/// <param name="perInstanceRandom">The random per-instance value (normalized to range 0-1).</param>
API_FUNCTION() void Draw(API_PARAM(Ref) const RenderContext& renderContext, MaterialBase* material, API_PARAM(Ref) const Matrix& world, StaticFlags flags = StaticFlags::None, bool receiveDecals = true, DrawPass drawModes = DrawPass::Default, float perInstanceRandom = 0.0f) const;
/// <summary>
/// Draws the mesh.

View File

@@ -122,11 +122,13 @@ public:
/// <param name="world">The world transformation of the model.</param>
/// <param name="flags">The object static flags.</param>
/// <param name="receiveDecals">True if rendered geometry can receive decals, otherwise false.</param>
API_FUNCTION() void Draw(API_PARAM(Ref) const RenderContext& renderContext, MaterialBase* material, API_PARAM(Ref) const Matrix& world, StaticFlags flags = StaticFlags::None, bool receiveDecals = true) const
/// <param name="drawModes">The draw passes to use for rendering this object.</param>
/// <param name="perInstanceRandom">The random per-instance value (normalized to range 0-1).</param>
API_FUNCTION() void Draw(API_PARAM(Ref) const RenderContext& renderContext, MaterialBase* material, API_PARAM(Ref) const Matrix& world, StaticFlags flags = StaticFlags::None, bool receiveDecals = true, DrawPass drawModes = DrawPass::Default, float perInstanceRandom = 0.0f) const
{
for (int32 i = 0; i < Meshes.Count(); i++)
{
Meshes[i].Draw(renderContext, material, world, flags, receiveDecals);
Meshes[i].Draw(renderContext, material, world, flags, receiveDecals, drawModes, perInstanceRandom);
}
}

View File

@@ -0,0 +1,188 @@
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
#include "SpriteRender.h"
#include "Engine/Core/Types/Variant.h"
#include "Engine/Core/Math/OrientedBoundingBox.h"
#include "Engine/Level/Scene/SceneRendering.h"
#include "Engine/Content/Content.h"
#include "Engine/Content/Assets/Model.h"
#include "Engine/Content/Assets/MaterialInstance.h"
#include "Engine/Level/Actors/Camera.h"
#include "Engine/Serialization/Serialization.h"
SpriteRender::SpriteRender(const SpawnParams& params)
: Actor(params)
, _color(Color::White)
, _size(100.0f)
{
_quadModel = Content::LoadAsyncInternal<Model>(TEXT("Engine/Models/Quad"));
Material.Loaded.Bind<SpriteRender, &SpriteRender::OnMaterialLoaded>(this);
Image.Changed.Bind<SpriteRender, &SpriteRender::OnImageChanged>(this);
}
Vector2 SpriteRender::GetSize() const
{
return _size;
}
void SpriteRender::SetSize(const Vector2& value)
{
if (_size == value)
return;
_size = value;
OnTransformChanged();
}
Color SpriteRender::GetColor() const
{
return _color;
}
void SpriteRender::SetColor(const Color& value)
{
_color = value;
if (_paramColor)
_paramColor->SetValue(value);
}
void SpriteRender::OnMaterialLoaded()
{
// Setup material instance
if (!_materialInstance)
{
_materialInstance = Content::CreateVirtualAsset<MaterialInstance>();
_materialInstance->AddReference();
}
_materialInstance->SetBaseMaterial(Material);
// Cache parameters
_paramImage = _materialInstance->GetParameter(TEXT("Image"));
if (_paramImage && _paramImage->GetParameterType() != MaterialParameterType::Texture)
_paramImage = nullptr;
else if (_paramImage)
_paramImage->SetValue(Image.Get());
_paramColor = _materialInstance->GetParameter(TEXT("Color"));
if (_paramColor && _paramColor->GetParameterType() != MaterialParameterType::Color && _paramColor->GetParameterType() != MaterialParameterType::Vector4 && _paramColor->GetParameterType() != MaterialParameterType::Vector3)
_paramColor = nullptr;
else if (_paramColor)
_paramColor->SetValue(_color);
}
void SpriteRender::OnImageChanged()
{
if (_paramImage)
_paramImage->SetValue(Image.Get());
}
bool SpriteRender::HasContentLoaded() const
{
return (Material == nullptr || Material->IsLoaded()) && (Image == nullptr || Image->IsLoaded());
}
void SpriteRender::Draw(RenderContext& renderContext)
{
if (!Material || !Material->IsLoaded() || !_quadModel || !_quadModel->IsLoaded())
return;
auto model = _quadModel.As<Model>();
if (model->GetLoadedLODs() == 0)
return;
auto& view = renderContext.View;
Matrix m1, m2, m3, world;
Matrix::Scaling(_size.X, _size.Y, 1.0f, m2);
Matrix::RotationY(PI, m3);
Matrix::Multiply(m2, m3, m1);
if (FaceCamera)
{
Matrix::Billboard(_transform.Translation, view.Position, Vector3::Up, view.Direction, m2);
Matrix::Multiply(m1, m2, m3);
Matrix::Scaling(_transform.Scale, m1);
Matrix::Multiply(m1, m3, world);
}
else
{
_transform.GetWorld(m2);
Matrix::Multiply(m1, m2, world);
}
model->LODs[0].Draw(renderContext, _materialInstance, world, GetStaticFlags(), false, DrawModes, GetPerInstanceRandom());
}
void SpriteRender::DrawGeneric(RenderContext& renderContext)
{
Draw(renderContext);
}
void SpriteRender::Serialize(SerializeStream& stream, const void* otherObj)
{
// Base
Actor::Serialize(stream, otherObj);
SERIALIZE_GET_OTHER_OBJ(SpriteRender);
SERIALIZE_MEMBER(Size, _size);
SERIALIZE_MEMBER(Color, _color);
SERIALIZE(Image);
SERIALIZE(Material);
SERIALIZE(FaceCamera);
SERIALIZE(DrawModes);
}
void SpriteRender::Deserialize(DeserializeStream& stream, ISerializeModifier* modifier)
{
// Base
Actor::Deserialize(stream, modifier);
DESERIALIZE_MEMBER(Size, _size);
DESERIALIZE_MEMBER(Color, _color);
DESERIALIZE(Image);
DESERIALIZE(Material);
DESERIALIZE(FaceCamera);
DESERIALIZE(DrawModes);
if (_paramImage)
_paramImage->SetValue(Image.Get());
if (_paramColor)
_paramColor->SetValue(_color);
}
void SpriteRender::OnEndPlay()
{
// Base
Actor::OnEndPlay();
// Release material instance
if (_materialInstance)
{
_materialInstance->SetBaseMaterial(nullptr);
_materialInstance->Params.Resize(0);
_materialInstance->RemoveReference();
_materialInstance = nullptr;
}
}
void SpriteRender::OnEnable()
{
GetSceneRendering()->AddGeometry(this);
// Base
Actor::OnEnable();
}
void SpriteRender::OnDisable()
{
GetSceneRendering()->RemoveGeometry(this);
// Base
Actor::OnDisable();
}
void SpriteRender::OnTransformChanged()
{
// Base
Actor::OnTransformChanged();
const BoundingSphere localSphere(Vector3::Zero, _size.Length());
Matrix world;
_transform.GetWorld(world);
BoundingSphere::Transform(localSphere, world, _sphere);
BoundingBox::FromSphere(_sphere, _box);
}

View File

@@ -0,0 +1,93 @@
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Level/Actor.h"
#include "Engine/Content/Assets/MaterialBase.h"
#include "Engine/Content/Assets/Texture.h"
/// <summary>
/// Sprite rendering object.
/// </summary>
API_CLASS() class FLAXENGINE_API SpriteRender : public Actor
{
DECLARE_SCENE_OBJECT(SpriteRender);
private:
Color _color;
Vector2 _size;
MaterialInstance* _materialInstance = nullptr;
MaterialParameter* _paramImage = nullptr;
MaterialParameter* _paramColor = nullptr;
AssetReference<Asset> _quadModel;
public:
/// <summary>
/// Gets the size of the sprite.
/// </summary>
API_PROPERTY(Attributes="EditorOrder(0), EditorDisplay(\"Sprite\")")
Vector2 GetSize() const;
/// <summary>
/// Sets the size of the sprite.
/// </summary>
API_PROPERTY() void SetSize(const Vector2& value);
/// <summary>
/// Gets the color of the sprite. Passed to the sprite material in parameter named `Color`.
/// </summary>
API_PROPERTY(Attributes="EditorOrder(10), DefaultValue(typeof(Color), \"1,1,1,1\"), EditorDisplay(\"Sprite\")")
Color GetColor() const;
/// <summary>
/// Sets the color of the sprite. Passed to the sprite material in parameter named `Color`.
/// </summary>
API_PROPERTY() void SetColor(const Color& value);
/// <summary>
/// The sprite texture to draw.
/// </summary>
API_FIELD(Attributes="EditorOrder(20), DefaultValue(null), EditorDisplay(\"Sprite\")")
AssetReference<Texture> Image;
/// <summary>
/// The material used for the sprite rendering. It should contain texture parameter named Image and color parameter named Color.
/// </summary>
API_FIELD(Attributes="EditorOrder(20), DefaultValue(null), EditorDisplay(\"Sprite\")")
AssetReference<MaterialBase> Material;
/// <summary>
/// If checked, the sprite will automatically face the view camera, otherwise it will be oriented as an actor.
/// </summary>
API_FIELD(Attributes="EditorOrder(30), EditorDisplay(\"Sprite\")")
bool FaceCamera = true;
/// <summary>
/// The draw passes to use for rendering this object. Uncheck `Depth` to disable sprite casting shadows.
/// </summary>
API_FIELD(Attributes="EditorOrder(40), DefaultValue(DrawPass.Default), EditorDisplay(\"Sprite\")")
DrawPass DrawModes = DrawPass::Default;
private:
void OnMaterialLoaded();
void OnImageChanged();
public:
// [Actor]
bool HasContentLoaded() const override;
void Draw(RenderContext& renderContext) override;
void DrawGeneric(RenderContext& renderContext) override;
void Serialize(SerializeStream& stream, const void* otherObj) override;
void Deserialize(DeserializeStream& stream, ISerializeModifier* modifier) override;
void OnEndPlay() override;
protected:
// [Actor]
void OnEnable() override;
void OnDisable() override;
void OnTransformChanged() override;
};