Reworked how Materials are handled on C#

This commit is contained in:
Kah Wei 2022-11-22 17:23:50 +08:00
parent 719d29dec3
commit 7d692061fb
9 changed files with 197 additions and 38 deletions

View File

@ -76,6 +76,12 @@ namespace SHADE
sharedMaterial = materialInstance;
}
void SHRenderable::SetMaterial(Handle<SHMaterial> material)
{
SHGraphicsSystem* gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
SetMaterial(gfxSystem->AddOrGetBaseMaterialInstance(material));
}
Handle<SHMaterialInstance> SHRenderable::GetMaterial() const
{
if (material)

View File

@ -48,6 +48,7 @@ namespace SHADE
/*-------------------------------------------------------------------------------*/
/* Material Functions */
/*-------------------------------------------------------------------------------*/
void SetMaterial(Handle<SHMaterial> material);
void SetMaterial(Handle<SHMaterialInstance> materialInstance);
Handle<SHMaterialInstance> GetMaterial() const;
Handle<SHMaterialInstance> GetModifiableMaterial();

View File

@ -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<SHMaterial> MaterialAsset::NativeObject::get()
try
{
return SHResourceManagerInterface::LoadOrGetMaterial(asset.NativeAssetID);
}
catch (const BadHandleCastException&)
{
return Handle<SHMaterial>();
}
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);
}
}

View File

@ -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
{
/// <summary>
/// Managed counterpart of the native Material object that can be fed to TextRenderables
/// for rendering.
/// </summary>
public value struct MaterialAsset
{
internal:
/*-----------------------------------------------------------------------------*/
/* Properties */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Copy of the Handle to the native object.
/// </summary>
property Handle<SHMaterial> NativeObject
{
Handle<SHMaterial> get();
}
/// <summary>
/// The raw asset ID of the asset.
/// </summary>
property AssetID NativeAssetID
{
AssetID get();
}
/*-----------------------------------------------------------------------------*/
/* Constructors/Destructor */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor for the Material.
/// </summary>
/// <param name="MaterialId">AssetID to the Material asset.</param>
MaterialAsset(AssetID MaterialId);
/*-----------------------------------------------------------------------------*/
/* Operator Overloads */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Implicit conversion operator to enable checking if a Material is valid.
/// </summary>
/// <param name="gameObj">Asset to check.</param>
/// <returns>True if the Asset is valid.</returns>
static operator bool(MaterialAsset asset);
/*-----------------------------------------------------------------------------*/
/* Conversion Operators */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Conversion operator to enable casting from a Material to an Asset.
/// </summary>
/// <param name="vec">Vector3 to convert from.</param>
static explicit operator Asset(MaterialAsset nativeAsset);
/// <summary>
/// Conversion operator to enable casting from a Asset to a Material.
/// </summary>
/// <param name="asset"></param>
static explicit operator MaterialAsset(Asset asset);
protected:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
Asset asset;
};
}

View File

@ -42,7 +42,7 @@ namespace SHADE
}
else
{
GetNativeComponent()->SetMesh(Handle<SHMesh>(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);
}
}

View File

@ -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 */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Sets the Material used by this Renderable to be an instance of the specified
/// base MaterialAsset.
/// </summary>
/// <param name="asset">Material to set.</param>
void SetMaterial(MaterialAsset materialAsset);
};
}

View File

@ -51,7 +51,7 @@ namespace SHADE
}
else
{
GetNativeComponent()->SetFont(Handle<SHFont>(Convert::ToNative(value->NativeObjectHandle)));
GetNativeComponent()->SetFont(value->NativeObject);
}
}
}

View File

@ -20,6 +20,7 @@ of DigiPen Institute of Technology is prohibited.
#include <stdexcept>
// Project Includes
#include "Utility/Convert.hxx"
#include "Resource/SHResourceManagerInterface.h"
namespace SHADE
{
@ -58,7 +59,7 @@ namespace SHADE
Handle<SHMaterialInstance> Material::NativeObject::get()
try
{
return Handle<SHMaterialInstance>(Convert::ToNative(asset.NativeObjectHandle));
return Handle<SHMaterialInstance>(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<SHMaterialInstance> material)
: asset { Handle<void>(material) }
: matInstHandle{ Handle<void>(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<SHMaterialInstance>(Convert::ToNative(asset.NativeObjectHandle)));
return materialInstance;
}
}

View File

@ -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
/// <returns>True if the Asset is valid.</returns>
static operator bool(Material asset);
/*-----------------------------------------------------------------------------*/
/* Conversion Operators */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Conversion operator to enable casting from a Material to an Asset.
/// </summary>
/// <param name="vec">Vector3 to convert from.</param>
static explicit operator Asset(Material nativeAsset);
/// <summary>
/// Conversion operator to enable casting from a Asset to a Material.
/// </summary>
/// <param name="vec">Vector2 to convert from.</param>
static explicit operator Material(Asset vec);
protected:
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
Asset asset;
GenericHandle matInstHandle;
};
}