Implemented serialization for text color and trajectory rendering #381

Merged
Xenosas1337 merged 10 commits from SP3-1-Rendering into main 2023-03-02 12:47:35 +08:00
8 changed files with 261 additions and 18 deletions
Showing only changes of commit 87c04436b3 - Show all commits

View File

@ -180,6 +180,10 @@ namespace SHADE
{
DrawComponent(listenerComponent);
}
if (auto trajectoryComponent = SHComponentManager::GetComponent_s<SHTrajectoryRenderableComponent>(eid))
{
DrawComponent(trajectoryComponent);
}
ImGui::Separator();
// Render Scripts
SHScriptEngine* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>());
@ -195,6 +199,7 @@ namespace SHADE
DrawAddComponentButton<SHButtonComponent>(eid);
DrawAddComponentButton<SHToggleButtonComponent>(eid);
DrawAddComponentButton<SHSliderComponent>(eid);
DrawAddComponentButton<SHTrajectoryRenderableComponent>(eid);
// Components that require Transforms

View File

@ -42,16 +42,26 @@ namespace SHADE
return mesh;
}
SHVec4 const& SHTrajectoryRenderableComponent::GetStartColor(void) const noexcept
SHVec3 const& SHTrajectoryRenderableComponent::GetStartColor(void) const noexcept
{
return startColor;
}
SHVec4 const& SHTrajectoryRenderableComponent::GetEndColor(void) const noexcept
SHVec3 const& SHTrajectoryRenderableComponent::GetEndColor(void) const noexcept
{
return endColor;
}
float SHTrajectoryRenderableComponent::GetStartAlpha(void) const noexcept
{
return startAlpha;
}
float SHTrajectoryRenderableComponent::GetEndAlpha(void) const noexcept
{
return endAlpha;
}
float SHTrajectoryRenderableComponent::GetColorEvolveRate(void) const noexcept
{
return colorEvolveRate;
@ -67,17 +77,27 @@ namespace SHADE
positions = inPositions;
}
void SHTrajectoryRenderableComponent::SetStartColor(SHVec4 color) noexcept
void SHTrajectoryRenderableComponent::SetStartColor(SHVec3 color) noexcept
{
startColor = color;
}
void SHTrajectoryRenderableComponent::SetEndColor(SHVec4 color) noexcept
void SHTrajectoryRenderableComponent::SetEndColor(SHVec3 color) noexcept
{
endColor = color;
}
void SHTrajectoryRenderableComponent::SetStartAlpha(float a) noexcept
{
startAlpha = a;
}
void SHTrajectoryRenderableComponent::SetEndAlpha(float a) noexcept
{
endAlpha = a;
}
void SHTrajectoryRenderableComponent::SetColorEvolveRate(float rate) noexcept
{
colorEvolveRate = rate;

View File

@ -10,7 +10,7 @@ namespace SHADE
{
class SHMesh;
class SHTrajectoryRenderableComponent : public SHComponent
class SH_API SHTrajectoryRenderableComponent : public SHComponent
{
private:
@ -21,10 +21,16 @@ namespace SHADE
std::vector<SHVec3> positions;
//! Starting color of the trajectory
SHVec4 startColor;
SHVec3 startColor;
//! Color the trajectory should evolve to the longer it is
SHVec4 endColor;
SHVec3 endColor;
//! Starting alpha of the trajectory
float startAlpha;
//! end alpha of the trajectory
float endAlpha;
//! evolving rate of the color
float colorEvolveRate;
@ -35,14 +41,18 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
void SetMesh(Handle<SHMesh> newMesh) noexcept;
void SetPositions (std::vector<SHVec3> const& inPositions) noexcept;
void SetStartColor(SHVec4 startColor) noexcept;
void SetEndColor (SHVec4 endColor) noexcept;
void SetStartColor(SHVec3 startColor) noexcept;
void SetEndColor (SHVec3 endColor) noexcept;
void SetStartAlpha(float a) noexcept;
void SetEndAlpha (float a) noexcept;
void SetColorEvolveRate(float rate) noexcept;
std::vector<SHVec3> GetPositions (void) const noexcept;
Handle<SHMesh> GetMesh (void) const noexcept;
SHVec4 const& GetStartColor (void) const noexcept;
SHVec4 const& GetEndColor (void) const noexcept;
SHVec3 const& GetStartColor (void) const noexcept;
SHVec3 const& GetEndColor (void) const noexcept;
float GetStartAlpha (void) const noexcept;
float GetEndAlpha (void) const noexcept;
float GetColorEvolveRate (void) const noexcept;
void OnCreate(void) override final;

View File

@ -59,8 +59,8 @@ namespace SHADE
.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha,
.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha,
.colorBlendOp = vk::BlendOp::eAdd,
.srcAlphaBlendFactor = vk::BlendFactor::eOne,
.dstAlphaBlendFactor = vk::BlendFactor::eZero,
.srcAlphaBlendFactor = vk::BlendFactor::eSrcAlpha,
.dstAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha,
.alphaBlendOp = vk::BlendOp::eAdd,
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA,
}
@ -88,8 +88,8 @@ namespace SHADE
if (transform)
{
// convenient variable
SHVec4 const& startColor = comp.GetStartColor();
SHVec4 const& endColor = comp.GetEndColor();
SHVec3 const& startColor = comp.GetStartColor();
SHVec3 const& endColor = comp.GetEndColor();
float colorEvolveRate = comp.GetColorEvolveRate();
// trs to be reused
@ -118,7 +118,8 @@ namespace SHADE
colorData.push_back(currentColor);
// evolve color
currentColor = SHVec4::Lerp(startColor, endColor, lerpValue);
currentColor = SHVec3::Lerp(startColor, endColor, lerpValue);
currentColor.w = SHMath::Lerp (comp.GetStartAlpha(), comp.GetEndAlpha(), lerpValue);
// evolve lerp value and clamp to 1
lerpValue = std::max (1.0f, lerpValue + colorEvolveRate);

View File

@ -15,6 +15,7 @@
#include "Graphics/MiddleEnd/TextRendering/SHFont.h"
#include "Animation/SHAnimatorComponent.h"
#include "Physics/Collision/CollisionTags/SHCollisionTagMatrix.h"
#include "Graphics/MiddleEnd/TrajectoryRendering/SHTrajectoryRenderableComponent.h"
namespace YAML
{
@ -424,4 +425,37 @@ namespace YAML
}
};
template<>
struct convert<SHTrajectoryRenderableComponent>
{
static constexpr std::string_view MESH_YAML_TAG = "Mesh";
static constexpr std::string_view START_COLOR_YAML_TAG = "Start Color";
static constexpr std::string_view END_COLOR_YAML_TAG = "End Color";
static constexpr std::string_view COLOR_EVAL_RATE_YAML_TAG = "Color Eval Rate ";
static YAML::Node encode(SHTrajectoryRenderableComponent const& rhs)
{
YAML::Node node;
node[MESH_YAML_TAG.data()] = SHResourceManager::GetAssetID<SHMesh>(rhs.GetMesh()).value_or(0);
node[START_COLOR_YAML_TAG.data()] = SHVec3(rhs.GetStartColor());
node[END_COLOR_YAML_TAG.data()] = SHVec3(rhs.GetEndColor());
node[COLOR_EVAL_RATE_YAML_TAG.data()] = rhs.GetColorEvolveRate();
return node;
}
static bool decode(YAML::Node const& node, SHTrajectoryRenderableComponent& rhs)
{
if (node[MESH_YAML_TAG.data()].IsDefined())
rhs.SetMesh(SHResourceManager::LoadOrGet<SHMesh>(node[MESH_YAML_TAG.data()].as<AssetID>()));
if (node[START_COLOR_YAML_TAG.data()].IsDefined())
rhs.SetStartColor(node[START_COLOR_YAML_TAG.data()].as<SHVec3>());
if (node[END_COLOR_YAML_TAG.data()].IsDefined())
rhs.SetEndColor(node[END_COLOR_YAML_TAG.data()].as<SHVec3>());
if (node[COLOR_EVAL_RATE_YAML_TAG.data()].IsDefined())
rhs.SetColorEvolveRate(node[COLOR_EVAL_RATE_YAML_TAG.data()].as<float>());
return true;
}
};
}

View File

@ -0,0 +1,83 @@
#include "SHpch.h"
#include "TrajectoryRenderable.hxx"
#include "Assets/NativeAsset.hxx"
#include "Utility/Convert.hxx"
namespace SHADE
{
TrajectoryRenderable::TrajectoryRenderable(Entity entity)
:Component(entity)
{
}
MeshAsset TrajectoryRenderable::Mesh::get()
{
auto mesh = GetNativeComponent()->GetMesh();
return mesh ? MeshAsset(mesh) : MeshAsset();
}
void TrajectoryRenderable::Mesh::set(MeshAsset value)
{
if (value)
{
GetNativeComponent()->SetMesh(Handle<SHMesh>());
}
else
{
GetNativeComponent()->SetMesh(value.NativeObject);
}
}
void TrajectoryRenderable::StartColor::set(Vector3 val)
{
GetNativeComponent()->SetStartColor(Convert::ToNative(val));
}
Vector3 TrajectoryRenderable::StartColor::get()
{
return Convert::ToCLI(GetNativeComponent()->GetStartColor());
}
void TrajectoryRenderable::EndColor::set(Vector3 val)
{
GetNativeComponent()->SetEndColor(Convert::ToNative(val));
}
Vector3 TrajectoryRenderable::EndColor::get()
{
return Convert::ToCLI(GetNativeComponent()->GetEndColor());
}
void TrajectoryRenderable::StartAlpha::set(float val)
{
GetNativeComponent()->SetStartAlpha(val);
}
float TrajectoryRenderable::StartAlpha::get()
{
return GetNativeComponent()->GetStartAlpha();
}
void TrajectoryRenderable::EndAlpha::set(float val)
{
GetNativeComponent()->SetEndAlpha(val);
}
float TrajectoryRenderable::EndAlpha::get()
{
return GetNativeComponent()->GetEndAlpha();
}
void TrajectoryRenderable::ColorEvolveRate::set(float val)
{
GetNativeComponent()->SetColorEvolveRate(val);
}
float TrajectoryRenderable::ColorEvolveRate::get()
{
return GetNativeComponent()->GetColorEvolveRate();
}
}

View File

@ -0,0 +1,87 @@
/************************************************************************************//*!
\file TrajectoryRenderable.hxx
\author Brandon Mak, brandon.hao,
\par email: brandon.hao\@digipen.edu
\date 1st March, 2023
\brief Contains the definition of the managed Renderable class with the
declaration of functions for working with it.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
// Project Includes
#include "Components/Component.hxx"
#include "Math/Quaternion.hxx"
// External Dependencies
#include "Graphics/MiddleEnd/TrajectoryRendering/SHTrajectoryRenderableComponent.h"
#include "Graphics/Color.hxx"
#include "Assets/MeshAsset.hxx"
namespace SHADE
{
/// <summary>
/// CLR version of the SHADE Engine's SHRenderableComponent.
/// </summary>
public ref class TrajectoryRenderable : public Component<SHTrajectoryRenderableComponent>
{
internal:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructs a Trajectory Renderable Component that represents a native Trajectory Renderable
/// component tied to the specified Entity.
/// </summary>
/// <param name="entity">Entity that this Component will be tied to.</param>
TrajectoryRenderable(Entity entity);
public:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Mesh used to render this Renderable.
/// </summary>
property MeshAsset Mesh
{
MeshAsset get();
void set(MeshAsset value);
}
property Vector3 StartColor
{
Vector3 get();
void set (Vector3 val);
}
property Vector3 EndColor
{
Vector3 get();
void set(Vector3 val);
}
property float StartAlpha
{
float get();
void set (float val);
}
property float EndAlpha
{
float get();
void set(float val);
}
property float ColorEvolveRate
{
float get();
void set(float val);
}
};
}

View File

@ -46,6 +46,8 @@ of DigiPen Institute of Technology is prohibited.
#include "Components\UIElement.hxx"
#include "Components\Canvas.hxx"
#include "Components\Slider.hxx"
#include "Components\TrajectoryRenderable.hxx"
#include "Graphics\MiddleEnd\TrajectoryRendering\SHTrajectoryRenderableComponent.h"
@ -335,6 +337,7 @@ namespace SHADE
componentMap.Add(createComponentSet<SHUIComponent, UIElement>());
componentMap.Add(createComponentSet<SHCanvasComponent, Canvas>());
componentMap.Add(createComponentSet<SHSliderComponent, Slider>());
componentMap.Add(createComponentSet<SHTrajectoryRenderableComponent, TrajectoryRenderable>());
}
/*---------------------------------------------------------------------------------*/