Merge pull request #185 from SHADE-DP/SP3-6-CSharpLights

Add interface for C# light class and modified Color to match Unity's interface
This commit is contained in:
XiaoQiDigipen 2022-11-08 21:51:04 +08:00 committed by GitHub
commit 7ac910149e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 384 additions and 89 deletions

View File

@ -106,6 +106,7 @@ namespace Sandbox
SHSystemManager::RegisterRoutine<SHTransformSystem, SHTransformSystem::TransformPostPhysicsUpdate>(); SHSystemManager::RegisterRoutine<SHTransformSystem, SHTransformSystem::TransformPostPhysicsUpdate>();
SHSystemManager::RegisterRoutine<SHDebugDrawSystem, SHDebugDrawSystem::ProcessPointsRoutine>(); SHSystemManager::RegisterRoutine<SHDebugDrawSystem, SHDebugDrawSystem::ProcessPointsRoutine>();
SHSystemManager::RegisterRoutine<SHScriptEngine, SHScriptEngine::GizmosDrawRoutine>();
SHSystemManager::RegisterRoutine<SHGraphicsSystem, SHGraphicsSystem::BatcherDispatcherRoutine>(); SHSystemManager::RegisterRoutine<SHGraphicsSystem, SHGraphicsSystem::BatcherDispatcherRoutine>();
SHSystemManager::RegisterRoutine<SHGraphicsSystem, SHGraphicsSystem::BeginRoutine>(); SHSystemManager::RegisterRoutine<SHGraphicsSystem, SHGraphicsSystem::BeginRoutine>();

View File

@ -379,6 +379,12 @@ namespace SHADE
DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore", DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"ExecuteLateUpdate" "ExecuteLateUpdate"
);
csScriptsExecuteDrawGizmos = dotNet.GetFunctionPtr<CsFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".ScriptStore",
"ExecuteOnDrawGizmos"
); );
csScriptsExecutePhysicsEvents = dotNet.GetFunctionPtr<CsFuncPtr> csScriptsExecutePhysicsEvents = dotNet.GetFunctionPtr<CsFuncPtr>
( (

View File

@ -55,6 +55,12 @@ namespace SHADE
LateUpdateRoutine(); LateUpdateRoutine();
void Execute(double dt) noexcept override final; void Execute(double dt) noexcept override final;
}; };
class SH_API GizmosDrawRoutine final : public SHSystemRoutine
{
public:
GizmosDrawRoutine();
void Execute(double dt) noexcept override final;
};
class SH_API FrameCleanUpRoutine final : public SHSystemRoutine class SH_API FrameCleanUpRoutine final : public SHSystemRoutine
{ {
public: public:
@ -250,6 +256,7 @@ namespace SHADE
CsFuncPtr csScriptsExecuteFixedUpdate = nullptr; CsFuncPtr csScriptsExecuteFixedUpdate = nullptr;
CsFuncPtr csScriptsExecuteUpdate = nullptr; CsFuncPtr csScriptsExecuteUpdate = nullptr;
CsFuncPtr csScriptsExecuteLateUpdate = nullptr; CsFuncPtr csScriptsExecuteLateUpdate = nullptr;
CsFuncPtr csScriptsExecuteDrawGizmos = nullptr;
CsFuncPtr csScriptsExecutePhysicsEvents = nullptr; CsFuncPtr csScriptsExecutePhysicsEvents = nullptr;
CsFuncPtr csScriptsFrameCleanUp = nullptr; CsFuncPtr csScriptsFrameCleanUp = nullptr;
CsScriptManipFuncPtr csScriptsAdd = nullptr; CsScriptManipFuncPtr csScriptsAdd = nullptr;

View File

@ -50,6 +50,17 @@ namespace SHADE
reinterpret_cast<SHScriptEngine*>(system)->csScriptsExecuteLateUpdate(); reinterpret_cast<SHScriptEngine*>(system)->csScriptsExecuteLateUpdate();
} }
/*-----------------------------------------------------------------------------------*/
/* System Routine Functions - GizmosDrawRoutine */
/*-----------------------------------------------------------------------------------*/
SHScriptEngine::GizmosDrawRoutine::GizmosDrawRoutine()
: SHSystemRoutine("Script Engine Gizmos Draw", true)
{}
void SHScriptEngine::GizmosDrawRoutine::Execute(double dt) noexcept
{
reinterpret_cast<SHScriptEngine*>(system)->csScriptsExecuteDrawGizmos();
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* System Routine Functions - FrameCleanUpRoutine */ /* System Routine Functions - FrameCleanUpRoutine */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -0,0 +1,79 @@
/************************************************************************************//*!
\file Light.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Nov 8, 2022
\brief Contains the definition of the functions of the managed Light class.
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.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "Light.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
Light::Light(Entity entity)
: Component(entity)
{}
/*---------------------------------------------------------------------------------*/
/* Properties */
/*---------------------------------------------------------------------------------*/
Vector3 Light::Position::get()
{
return Convert::ToCLI(GetNativeComponent()->GetPosition());
}
void Light::Position::set(Vector3 value)
{
GetNativeComponent()->SetPosition(Convert::ToNative(value));
}
Light::Type Light::LightType::get()
{
return static_cast<Type>(GetNativeComponent()->GetType());
}
void Light::LightType::set(Light::Type value)
{
GetNativeComponent()->SetType(static_cast<SH_LIGHT_TYPE>(value));
}
Vector3 Light::Direction::get()
{
return Convert::ToCLI(GetNativeComponent()->GetDirection());
}
void Light::Direction::set(Vector3 value)
{
GetNativeComponent()->SetDirection(Convert::ToNative(value));
}
Color Light::Color::get()
{
return Convert::ToCLI(SHColour(GetNativeComponent()->GetColor()));
}
void Light::Color::set(SHADE::Color value)
{
GetNativeComponent()->SetColor(Convert::ToNative(value));
}
System::UInt32 Light::CullingMask::get()
{
return GetNativeComponent()->GetCullingMask();
}
void Light::CullingMask::set(System::UInt32 value)
{
GetNativeComponent()->SetCullingMask(value);
}
float Light::Strength::get()
{
return GetNativeComponent()->GetStrength();
}
void Light::Strength::set(float value)
{
GetNativeComponent()->SetStrength(value);
}
}

View File

@ -0,0 +1,125 @@
/************************************************************************************//*!
\file Light.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Nov 8, 2022
\brief Contains the definition of the managed Light 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/Vector3.hxx"
// External Dependencies
#include "Graphics/MiddleEnd/Lights/SHLightComponent.h"
namespace SHADE
{
/// <summary>
/// CLR version of the SHADE Engine's SHLightComponent.
/// </summary>
public ref class Light : public Component<SHLightComponent>
{
internal:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructs a Light Component that represents a native Light component tied to
/// the specified Entity.
/// </summary>
/// <param name="entity">Entity that this Component will be tied to.</param>
Light(Entity entity);
public:
/*-----------------------------------------------------------------------------*/
/* Constants */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Supported types of the Light Component.
/// </summary>
enum class Type
{
/// <summary>
/// Light applied uniformly across the scene at a specified direction.
/// </summary>
Directional,
/// <summary>
/// Light that originates from a certain point in all directions.
/// Not implemented yet.
/// </summary>
Point,
/// <summary>
/// Light that originates from a certain point within a angle.
/// Not implemented yet.
/// </summary>
Spot,
/// <summary>
/// Light applied to all objects. Has no source point.
/// </summary>
Ambient
};
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Position of the light. Only works for Point Light (unimplemented).
/// </summary>
[System::ObsoleteAttribute("Not implemented yet.", true)]
property Vector3 Position
{
Vector3 get();
void set(Vector3 val);
}
/// <summary>
/// Type of lighting that this Light component will apply onto the scene.
/// </summary>
property Type LightType
{
Type get();
void set(Type val);
}
/// <summary>
/// Direction of the light. Only applicable for Directional Lights.
/// </summary>
property Vector3 Direction
{
Vector3 get();
void set(Vector3 val);
}
/// <summary>
/// Colour of the Light.
/// </summary>
property SHADE::Color Color
{
SHADE::Color get();
void set(SHADE::Color val);
}
/// <summary>
/// Culling mask that is used to control what types of Materials would be
/// affected by this Light.
/// </summary>
property System::UInt32 CullingMask
{
System::UInt32 get();
void set(System::UInt32 val);
}
/// <summary>
/// Intensity of the Light
/// </summary>
property float Strength
{
float get();
void set(float val);
}
};
}

View File

@ -22,8 +22,8 @@ of DigiPen Institute of Technology is prohibited.
// External Dependencies // External Dependencies
#include "ECS_Base/Managers/SHEntityManager.h" #include "ECS_Base/Managers/SHEntityManager.h"
#include "Math/Transform/SHTransformComponent.h" #include "Math/Transform/SHTransformComponent.h"
#include "Physics\Components\SHColliderComponent.h" #include "Physics/Components/SHColliderComponent.h"
#include "Physics\Components\SHRigidBodyComponent.h" #include "Physics/Components/SHRigidBodyComponent.h"
#include "Scene/SHSceneManager.h" #include "Scene/SHSceneManager.h"
#include "Scene/SHSceneGraph.h" #include "Scene/SHSceneGraph.h"
#include "Tools/SHLog.h" #include "Tools/SHLog.h"
@ -31,10 +31,11 @@ of DigiPen Institute of Technology is prohibited.
#include "Utility/Convert.hxx" #include "Utility/Convert.hxx"
#include "Utility/Debug.hxx" #include "Utility/Debug.hxx"
#include "Components/Transform.hxx" #include "Components/Transform.hxx"
#include "Components\RigidBody.hxx" #include "Components/RigidBody.hxx"
#include "Components\Collider.hxx" #include "Components/Collider.hxx"
#include "Components/Camera.hxx" #include "Components/Camera.hxx"
#include "Components/CameraArm.hxx" #include "Components/CameraArm.hxx"
#include "Components/Light.hxx"
namespace SHADE namespace SHADE
{ {
@ -252,6 +253,7 @@ namespace SHADE
componentMap.Add(createComponentSet<SHRigidBodyComponent, RigidBody>()); componentMap.Add(createComponentSet<SHRigidBodyComponent, RigidBody>());
componentMap.Add(createComponentSet<SHCameraComponent, Camera>()); componentMap.Add(createComponentSet<SHCameraComponent, Camera>());
componentMap.Add(createComponentSet<SHCameraArmComponent, CameraArm>()); componentMap.Add(createComponentSet<SHCameraArmComponent, CameraArm>());
componentMap.Add(createComponentSet<SHLightComponent, Light>());
} }
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -25,95 +25,85 @@ namespace SHADE
{ {
public: public:
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Type Definitions */ /* Properties */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/// <summary> /// <summary>
/// A static class that contains a set of default Colors. /// Pure black.
/// </summary> /// </summary>
ref class Defaults abstract sealed static property Color Black
{ {
public: Color get() { return Color(0.0f, 0.0f, 0.0f); }
/*-------------------------------------------------------------------------*/ }
/* Properties */ /// <summary>
/*-------------------------------------------------------------------------*/ /// Light Gray, lighter than gray.
/// <summary> /// </summary>
/// Pure black. static property Color LightGray
/// </summary> {
static property Color Black Color get() { return Color(0.827451f, 0.827451f, 0.827451f); }
{ }
Color get() { return Color(0.0f, 0.0f, 0.0f); } /// <summary>
} /// Gray, halfway between black and white.
/// <summary> /// </summary>
/// Light Gray, lighter than gray. static property Color Gray
/// </summary> {
static property Color LightGray Color get() { return Color(0.5f, 0.5f, 0.5f); }
{ }
Color get() { return Color(0.827451f, 0.827451f, 0.827451f); } /// <summary>
} /// Dark Gray, darker than gray.
/// <summary> /// </summary>
/// Gray, halfway between black and white. static property Color DarkGray
/// </summary> {
static property Color Gray Color get() { return Color(0.622f, 0.622f, 0.622f); }
{ }
Color get() { return Color(0.5f, 0.5f, 0.5f); } /// <summary>
} /// Pure white.
/// <summary> /// </summary>
/// Dark Gray, darker than gray. static property Color White
/// </summary> {
static property Color DarkGray Color get() { return Color(1.0f, 1.0f, 1.0f); }
{ }
Color get() { return Color(0.622f, 0.622f, 0.622f); } /// <summary>
} /// Pure red.
/// <summary> /// </summary>
/// Pure white. static property Color Red
/// </summary> {
static property Color White Color get() { return Color(1.0f, 0.0f, 0.0f); }
{ }
Color get() { return Color(1.0f, 1.0f, 1.0f); } /// <summary>
} /// Pure green.
/// <summary> /// </summary>
/// Pure red. static property Color Green
/// </summary> {
static property Color Red Color get() { return Color(0.0f, 1.0f, 0.0f); }
{ }
Color get() { return Color(1.0f, 0.0f, 0.0f); } /// <summary>
} /// Pure blue.
/// <summary> /// </summary>
/// Pure green. static property Color Blue
/// </summary> {
static property Color Green Color get() { return Color(0.0f, 0.0f, 1.0f); }
{ }
Color get() { return Color(0.0f, 1.0f, 0.0f); } /// <summary>
} /// Pure cyan, mix of pure green and blue.
/// <summary> /// </summary>
/// Pure blue. static property Color Cyan
/// </summary> {
static property Color Blue Color get() { return Color(0.0f, 1.0f, 1.0f); }
{ }
Color get() { return Color(0.0f, 0.0f, 1.0f); } /// <summary>
} /// Pure magenta, mix of pure red and blue.
/// <summary> /// </summary>
/// Pure cyan, mix of pure green and blue. static property Color Magenta
/// </summary> {
static property Color Cyan Color get() { return Color(1.0f, 0.0f, 1.0f); }
{ }
Color get() { return Color(0.0f, 1.0f, 1.0f); } /// <summary>
} /// Pure yellow, mix of pure red and green.
/// <summary> /// </summary>
/// Pure magenta, mix of pure red and blue. static property Color Yellow
/// </summary> {
static property Color Magenta Color get() { return Color(1.0f, 1.0f, 0.0f); }
{ }
Color get() { return Color(1.0f, 0.0f, 1.0f); }
}
/// <summary>
/// Pure yellow, mix of pure red and green.
/// </summary>
static property Color Yellow
{
Color get() { return Color(1.0f, 1.0f, 0.0f); }
}
};
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Constructors */ /* Constructors */

View File

@ -140,6 +140,13 @@ namespace SHADE
lateUpdate(); lateUpdate();
SAFE_NATIVE_CALL_END(this) SAFE_NATIVE_CALL_END(this)
} }
void Script::OnDrawGizmos()
{
SAFE_NATIVE_CALL_BEGIN
OnGizmosDrawOverriden = true;
onDrawGizmos();
SAFE_NATIVE_CALL_END(this)
}
void Script::OnDestroy() void Script::OnDestroy()
{ {
SAFE_NATIVE_CALL_BEGIN SAFE_NATIVE_CALL_BEGIN
@ -194,6 +201,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
Script::Script(GameObject gameObj) Script::Script(GameObject gameObj)
: owner { gameObj } : owner { gameObj }
, OnGizmosDrawOverriden { false }
{} {}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -210,6 +218,10 @@ namespace SHADE
void Script::fixedUpdate() {} void Script::fixedUpdate() {}
void Script::update() {} void Script::update() {}
void Script::lateUpdate() {} void Script::lateUpdate() {}
void Script::onDrawGizmos()
{
OnGizmosDrawOverriden = false;
}
void Script::onDestroy() {} void Script::onDestroy() {}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -164,6 +164,14 @@ namespace SHADE
static operator bool(Script^ s); static operator bool(Script^ s);
internal: internal:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// If true, the OnGizmosDraw function was overridden.
/// </summary>
bool OnGizmosDrawOverriden;
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* "All-Time" Lifecycle Functions */ /* "All-Time" Lifecycle Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
@ -208,6 +216,11 @@ namespace SHADE
/// </summary> /// </summary>
void LateUpdate(); void LateUpdate();
/// <summary> /// <summary>
/// Used to call onDrawGizmos(). This should be called just before rendering
/// the scene. This will only be called when working in the editor.
/// </summary>
void OnDrawGizmos();
/// <summary>
/// Used to call onDestroy(). This should be called at the end of the frame /// Used to call onDestroy(). This should be called at the end of the frame
/// where the attached GameObject or this script is destroyed directly or /// where the attached GameObject or this script is destroyed directly or
/// indirectly due to destruction of the owner. /// indirectly due to destruction of the owner.
@ -308,6 +321,10 @@ namespace SHADE
/// </summary> /// </summary>
virtual void lateUpdate(); virtual void lateUpdate();
/// <summary> /// <summary>
/// Called every frame just before rendering but only if working in the editor.
/// </summary>
virtual void onDrawGizmos();
/// <summary>
/// Called just before the end of the frame where the attached GameObject or /// Called just before the end of the frame where the attached GameObject or
/// this script is destroyed directly or indirectly due to destruction of the /// this script is destroyed directly or indirectly due to destruction of the
/// owner. /// owner.

View File

@ -478,6 +478,24 @@ namespace SHADE
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
} }
void ScriptStore::ExecuteOnDrawGizmos()
{
SAFE_NATIVE_CALL_BEGIN
for each (System::Collections::Generic::KeyValuePair<Entity, ScriptList^> entity in scripts)
{
// Check active state
if (!isEntityActive(entity.Key))
continue;
// Update each script
for each (Script^ script in entity.Value)
{
script->OnDrawGizmos();
}
}
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
}
void ScriptStore::ExecuteCollisionFunctions() void ScriptStore::ExecuteCollisionFunctions()
{ {
SAFE_NATIVE_CALL_BEGIN SAFE_NATIVE_CALL_BEGIN

View File

@ -234,6 +234,10 @@ namespace SHADE
/// </summary> /// </summary>
static void ExecuteLateUpdate(); static void ExecuteLateUpdate();
/// <summary> /// <summary>
/// Executes OnDrawGizmos() for all scripts.
/// </summary>
static void ExecuteOnDrawGizmos();
/// <summary>
/// Executes OnCollision*() and OnTrigger*() for all scripts. /// Executes OnCollision*() and OnTrigger*() for all scripts.
/// </summary> /// </summary>
static void ExecuteCollisionFunctions(); static void ExecuteCollisionFunctions();

View File

@ -72,6 +72,16 @@ namespace SHADE
return Ray(ToCLI(vec.position), ToCLI(vec.direction)); return Ray(ToCLI(vec.position), ToCLI(vec.direction));
} }
SHColour Convert::ToNative(Color col)
{
return SHColour(col.r, col.g, col.b, col.a);
}
Color Convert::ToCLI(const SHColour& vec)
{
return Color(vec.x, vec.y, vec.z, vec.w);
}
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* String Conversions */ /* String Conversions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -29,6 +29,8 @@ of DigiPen Institute of Technology is prohibited.
#include "Math/Quaternion.hxx" #include "Math/Quaternion.hxx"
#include "Math/Ray.hxx" #include "Math/Ray.hxx"
#include "Engine/GenericHandle.hxx" #include "Engine/GenericHandle.hxx"
#include "Math/SHColour.h"
#include "Graphics/Color.hxx"
namespace SHADE namespace SHADE
{ {
@ -104,6 +106,17 @@ namespace SHADE
/// <param name="vec">The native Vector2 to convert from.</param> /// <param name="vec">The native Vector2 to convert from.</param>
/// <returns>Managed copy of a native Vector2.</returns> /// <returns>Managed copy of a native Vector2.</returns>
static Ray ToCLI(const SHRay& vec); static Ray ToCLI(const SHRay& vec);
/// Converts from a managed Color to a native Colour.
/// </summary>
/// <param name="vec">The managed Color to convert from.</param>
/// <returns>Native copy of a managed Color.</returns>
static SHColour ToNative(Color col);
/// <summary>
/// Converts from a native Colour to a managed Color.
/// </summary>
/// <param name="vec">The native Colour to convert from.</param>
/// <returns>Managed copy of a native Colour.</returns>
static Color ToCLI(const SHColour& vec);
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* String Conversions */ /* String Conversions */