From 7d692061fbbf2e64930ea580c67193cbef60bcd5 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Tue, 22 Nov 2022 17:23:50 +0800 Subject: [PATCH] Reworked how Materials are handled on C# --- .../MiddleEnd/Interface/SHRenderable.cpp | 6 ++ .../MiddleEnd/Interface/SHRenderable.h | 1 + SHADE_Managed/src/Assets/MaterialAsset.cxx | 70 +++++++++++++++ SHADE_Managed/src/Assets/MaterialAsset.hxx | 89 +++++++++++++++++++ SHADE_Managed/src/Components/Renderable.cxx | 10 ++- SHADE_Managed/src/Components/Renderable.hxx | 13 ++- .../src/Components/TextRenderable.cxx | 2 +- .../src/{Assets => Graphics}/Material.cxx | 26 ++---- .../src/{Assets => Graphics}/Material.hxx | 18 +--- 9 files changed, 197 insertions(+), 38 deletions(-) create mode 100644 SHADE_Managed/src/Assets/MaterialAsset.cxx create mode 100644 SHADE_Managed/src/Assets/MaterialAsset.hxx rename SHADE_Managed/src/{Assets => Graphics}/Material.cxx (90%) rename SHADE_Managed/src/{Assets => Graphics}/Material.hxx (86%) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.cpp index e22de5ab..c7578137 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.cpp @@ -76,6 +76,12 @@ namespace SHADE sharedMaterial = materialInstance; } + void SHRenderable::SetMaterial(Handle material) + { + SHGraphicsSystem* gfxSystem = SHSystemManager::GetSystem(); + SetMaterial(gfxSystem->AddOrGetBaseMaterialInstance(material)); + } + Handle SHRenderable::GetMaterial() const { if (material) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.h index f1455ef4..39132ca0 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderable.h @@ -48,6 +48,7 @@ namespace SHADE /*-------------------------------------------------------------------------------*/ /* Material Functions */ /*-------------------------------------------------------------------------------*/ + void SetMaterial(Handle material); void SetMaterial(Handle materialInstance); Handle GetMaterial() const; Handle GetModifiableMaterial(); diff --git a/SHADE_Managed/src/Assets/MaterialAsset.cxx b/SHADE_Managed/src/Assets/MaterialAsset.cxx new file mode 100644 index 00000000..16f6a929 --- /dev/null +++ b/SHADE_Managed/src/Assets/MaterialAsset.cxx @@ -0,0 +1,70 @@ +/************************************************************************************//*! +\file MaterialAsset.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 22, 2022 +\brief Contains the implementation of the functions of the managed Material + struct. + + 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 "MaterialAsset.hxx" +// External Dependencies +#include "Resource/SHResourceManagerInterface.h" +// Project Headers +#include "Utility/Convert.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + Handle MaterialAsset::NativeObject::get() + try + { + return SHResourceManagerInterface::LoadOrGetMaterial(asset.NativeAssetID); + } + catch (const BadHandleCastException&) + { + return Handle(); + } + AssetID MaterialAsset::NativeAssetID::get() + { + return asset.NativeAssetID; + } + + /*---------------------------------------------------------------------------------*/ + /* Constructors/Destructor */ + /*---------------------------------------------------------------------------------*/ + MaterialAsset::MaterialAsset(AssetID MaterialId) + : asset { MaterialId } + {} + + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + MaterialAsset::operator bool(MaterialAsset asset) + { + return asset; + } + + /*---------------------------------------------------------------------------------*/ + /* Conversion Operators */ + /*---------------------------------------------------------------------------------*/ + MaterialAsset::operator Asset(MaterialAsset nativeAsset) + { + return nativeAsset.asset; + } + + MaterialAsset::operator MaterialAsset(Asset asset) + { + return MaterialAsset(asset.NativeAssetID); + } +} diff --git a/SHADE_Managed/src/Assets/MaterialAsset.hxx b/SHADE_Managed/src/Assets/MaterialAsset.hxx new file mode 100644 index 00000000..5380b286 --- /dev/null +++ b/SHADE_Managed/src/Assets/MaterialAsset.hxx @@ -0,0 +1,89 @@ +/************************************************************************************//*! +\file MaterialAsset.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 22, 2022 +\brief Contains the definition of the managed MaterialAsset struct. + + 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 + +// External Dependencies +#include "Resource/SHHandle.h" +#include "Graphics/MiddleEnd/Interface/SHMaterial.h" +// Project Includes +#include "NativeAsset.hxx" +#include "Engine/GenericHandle.hxx" + +namespace SHADE +{ + /// + /// Managed counterpart of the native Material object that can be fed to TextRenderables + /// for rendering. + /// + public value struct MaterialAsset + { + internal: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Copy of the Handle to the native object. + /// + property Handle NativeObject + { + Handle get(); + } + /// + /// The raw asset ID of the asset. + /// + property AssetID NativeAssetID + { + AssetID get(); + } + + /*-----------------------------------------------------------------------------*/ + /* Constructors/Destructor */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructor for the Material. + /// + /// AssetID to the Material asset. + MaterialAsset(AssetID MaterialId); + + /*-----------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*-----------------------------------------------------------------------------*/ + /// + /// Implicit conversion operator to enable checking if a Material is valid. + /// + /// Asset to check. + /// True if the Asset is valid. + static operator bool(MaterialAsset asset); + + /*-----------------------------------------------------------------------------*/ + /* Conversion Operators */ + /*-----------------------------------------------------------------------------*/ + /// + /// Conversion operator to enable casting from a Material to an Asset. + /// + /// Vector3 to convert from. + static explicit operator Asset(MaterialAsset nativeAsset); + /// + /// Conversion operator to enable casting from a Asset to a Material. + /// + /// + static explicit operator MaterialAsset(Asset asset); + + protected: + /*-----------------------------------------------------------------------------*/ + /* Data Members */ + /*-----------------------------------------------------------------------------*/ + Asset asset; + }; +} diff --git a/SHADE_Managed/src/Components/Renderable.cxx b/SHADE_Managed/src/Components/Renderable.cxx index b4efe26e..819760e6 100644 --- a/SHADE_Managed/src/Components/Renderable.cxx +++ b/SHADE_Managed/src/Components/Renderable.cxx @@ -42,7 +42,7 @@ namespace SHADE } else { - GetNativeComponent()->SetMesh(Handle(Convert::ToNative(value->NativeObjectHandle))); + GetNativeComponent()->SetMesh(value->NativeObject); } } SHADE::Material^ Renderable::Material::get() @@ -64,4 +64,12 @@ namespace SHADE { return GetNativeComponent()->GetLightLayer(); } + + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + void Renderable::SetMaterial(MaterialAsset materialAsset) + { + GetNativeComponent()->SetMaterial(materialAsset.NativeObject); + } } diff --git a/SHADE_Managed/src/Components/Renderable.hxx b/SHADE_Managed/src/Components/Renderable.hxx index be7d107f..d52a01e5 100644 --- a/SHADE_Managed/src/Components/Renderable.hxx +++ b/SHADE_Managed/src/Components/Renderable.hxx @@ -21,7 +21,8 @@ of DigiPen Institute of Technology is prohibited. // External Dependencies #include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Assets/MeshAsset.hxx" -#include "Assets/Material.hxx" +#include "Graphics/Material.hxx" +#include "Assets/MaterialAsset.hxx" namespace SHADE { @@ -68,6 +69,16 @@ namespace SHADE { System::Byte get(); } + + /*-----------------------------------------------------------------------------*/ + /* Usage functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Sets the Material used by this Renderable to be an instance of the specified + /// base MaterialAsset. + /// + /// Material to set. + void SetMaterial(MaterialAsset materialAsset); }; } diff --git a/SHADE_Managed/src/Components/TextRenderable.cxx b/SHADE_Managed/src/Components/TextRenderable.cxx index e34592ae..88e43823 100644 --- a/SHADE_Managed/src/Components/TextRenderable.cxx +++ b/SHADE_Managed/src/Components/TextRenderable.cxx @@ -51,7 +51,7 @@ namespace SHADE } else { - GetNativeComponent()->SetFont(Handle(Convert::ToNative(value->NativeObjectHandle))); + GetNativeComponent()->SetFont(value->NativeObject); } } } diff --git a/SHADE_Managed/src/Assets/Material.cxx b/SHADE_Managed/src/Graphics/Material.cxx similarity index 90% rename from SHADE_Managed/src/Assets/Material.cxx rename to SHADE_Managed/src/Graphics/Material.cxx index 3caa5b22..e646b34d 100644 --- a/SHADE_Managed/src/Assets/Material.cxx +++ b/SHADE_Managed/src/Graphics/Material.cxx @@ -20,6 +20,7 @@ of DigiPen Institute of Technology is prohibited. #include // Project Includes #include "Utility/Convert.hxx" +#include "Resource/SHResourceManagerInterface.h" namespace SHADE { @@ -58,7 +59,7 @@ namespace SHADE Handle Material::NativeObject::get() try { - return Handle(Convert::ToNative(asset.NativeObjectHandle)); + return Handle(Convert::ToNative(matInstHandle)); } catch (const BadHandleCastException&) { @@ -66,18 +67,18 @@ namespace SHADE } GenericHandle Material::NativeObjectHandle::get() { - return asset.NativeObjectHandle; + return matInstHandle; } AssetID Material::NativeAssetID::get() { - return asset.NativeAssetID; + return SHResourceManagerInterface::GetAssetID(Convert::ToNative(matInstHandle)).value_or(INVALID_ASSET_ID); } /*---------------------------------------------------------------------------------*/ /* Constructors/Destructor */ /*---------------------------------------------------------------------------------*/ Material::Material(Handle material) - : asset { Handle(material) } + : matInstHandle{ Handle(material) } {} /*---------------------------------------------------------------------------------*/ @@ -136,21 +137,8 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - Material::operator bool(Material asset) + Material::operator bool(Material materialInstance) { - return asset; - } - - /*---------------------------------------------------------------------------------*/ - /* Conversion Operators */ - /*---------------------------------------------------------------------------------*/ - Material::operator Asset(Material nativeAsset) - { - return nativeAsset.asset; - } - - Material::operator Material(Asset asset) - { - return Material(Handle(Convert::ToNative(asset.NativeObjectHandle))); + return materialInstance; } } diff --git a/SHADE_Managed/src/Assets/Material.hxx b/SHADE_Managed/src/Graphics/Material.hxx similarity index 86% rename from SHADE_Managed/src/Assets/Material.hxx rename to SHADE_Managed/src/Graphics/Material.hxx index 14cf557e..12664658 100644 --- a/SHADE_Managed/src/Assets/Material.hxx +++ b/SHADE_Managed/src/Graphics/Material.hxx @@ -16,8 +16,8 @@ of DigiPen Institute of Technology is prohibited. // External Dependencies #include "Resource/SHHandle.h" #include "Graphics/MiddleEnd/Interface/SHMaterialInstance.h" +#include "Assets/SHAssetMacros.h" // Project Includes -#include "NativeAsset.hxx" #include "Engine/GenericHandle.hxx" namespace SHADE @@ -113,24 +113,10 @@ namespace SHADE /// True if the Asset is valid. static operator bool(Material asset); - /*-----------------------------------------------------------------------------*/ - /* Conversion Operators */ - /*-----------------------------------------------------------------------------*/ - /// - /// Conversion operator to enable casting from a Material to an Asset. - /// - /// Vector3 to convert from. - static explicit operator Asset(Material nativeAsset); - /// - /// Conversion operator to enable casting from a Asset to a Material. - /// - /// Vector2 to convert from. - static explicit operator Material(Asset vec); - protected: /*-----------------------------------------------------------------------------*/ /* Data Members */ /*-----------------------------------------------------------------------------*/ - Asset asset; + GenericHandle matInstHandle; }; }