2018-01-28 14:48:02 -06:00
//-----------------------------------------------------------------------------
// Copyright (c) 2013 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
2020-12-27 23:24:29 -06:00
# pragma once
2018-01-28 14:48:02 -06:00
# ifndef MATERIALASSET_H
# define MATERIALASSET_H
# ifndef _ASSET_BASE_H_
# include "assets/assetBase.h"
# endif
# ifndef _ASSET_DEFINITION_H_
# include "assets/assetDefinition.h"
# endif
# ifndef _STRINGUNIT_H_
# include "string/stringUnit.h"
# endif
# ifndef _ASSET_FIELD_TYPES_H_
# include "assets/assetFieldTypes.h"
# endif
# ifndef _GFXDEVICE_H_
# include "gfx/gfxDevice.h"
# endif
2020-12-27 23:24:29 -06:00
# ifndef _NETCONNECTION_H_
# include "sim/netConnection.h"
# endif
2022-01-30 11:50:16 -06:00
# ifndef _GUI_INSPECTOR_TYPES_H_
2018-01-28 14:48:02 -06:00
# include "gui/editor/guiInspectorTypes.h"
2022-01-30 11:50:16 -06:00
# endif
2018-01-28 14:48:02 -06:00
# include "materials/matTextureTarget.h"
# include "materials/materialDefinition.h"
# include "materials/customMaterialDefinition.h"
2021-07-19 01:07:08 -05:00
# include "materials/materialManager.h"
2021-10-03 02:56:26 -05:00
# include "assetMacroHelpers.h"
2022-12-08 20:00:55 -06:00
# include <gui/controls/guiBitmapCtrl.h>
2018-01-28 14:48:02 -06:00
//-----------------------------------------------------------------------------
class MaterialAsset : public AssetBase
{
typedef AssetBase Parent ;
String mShaderGraphFile ;
2019-05-06 01:49:58 -05:00
StringTableEntry mScriptFile ;
2020-08-09 01:32:27 -05:00
StringTableEntry mScriptPath ;
2019-05-04 11:49:42 -05:00
StringTableEntry mMatDefinitionName ;
2018-01-28 14:48:02 -06:00
2021-07-19 01:07:08 -05:00
SimObjectPtr < Material > mMaterialDefinition ;
public :
static StringTableEntry smNoMaterialAssetFallback ;
2021-09-19 01:01:47 -05:00
enum MaterialAssetErrCode
{
ScriptLoaded = AssetErrCode : : Extended ,
2021-10-09 00:07:01 -05:00
DefinitionAlreadyExists ,
2022-01-30 11:50:16 -06:00
EmbeddedDefinition ,
2021-09-19 01:01:47 -05:00
Extended
} ;
2018-01-28 14:48:02 -06:00
public :
MaterialAsset ( ) ;
virtual ~ MaterialAsset ( ) ;
2021-07-19 01:07:08 -05:00
/// Set up some global script interface stuff.
static void consoleInit ( ) ;
2018-01-28 14:48:02 -06:00
/// Engine.
static void initPersistFields ( ) ;
virtual void copyTo ( SimObject * object ) ;
2021-07-19 01:07:08 -05:00
void loadMaterial ( ) ;
2018-01-28 14:48:02 -06:00
2019-05-04 11:49:42 -05:00
StringTableEntry getMaterialDefinitionName ( ) { return mMatDefinitionName ; }
2021-07-19 01:07:08 -05:00
SimObjectPtr < Material > getMaterialDefinition ( ) { return mMaterialDefinition ; }
2019-05-04 11:49:42 -05:00
void setScriptFile ( const char * pScriptFile ) ;
inline StringTableEntry getScriptFile ( void ) const { return mScriptFile ; } ;
2018-01-28 14:48:02 -06:00
2020-08-09 01:32:27 -05:00
inline StringTableEntry getScriptPath ( void ) const { return mScriptPath ; } ;
2021-07-19 01:07:08 -05:00
/// <summary>
/// Looks for any assets that uses the provided Material Definition name.
/// If none are found, attempts to auto-import the material definition if the
/// material definition exists.
/// </summary>
/// <param name="matName">Material Definition name to look for</param>
/// <returns>AssetId of matching asset.</returns>
static StringTableEntry getAssetIdByMaterialName ( StringTableEntry matName ) ;
static U32 getAssetById ( StringTableEntry assetId , AssetPtr < MaterialAsset > * materialAsset ) ;
2022-01-30 11:50:16 -06:00
static SimObjectPtr < Material > findMaterialDefinitionByAssetId ( StringTableEntry assetId ) ;
2021-07-19 01:07:08 -05:00
static U32 getAssetByMaterialName ( StringTableEntry matName , AssetPtr < MaterialAsset > * matAsset ) ;
2020-11-01 23:32:34 -06:00
2018-01-28 14:48:02 -06:00
/// Declare Console Object.
DECLARE_CONOBJECT ( MaterialAsset ) ;
2019-05-04 11:49:42 -05:00
protected :
virtual void initializeAsset ( ) ;
virtual void onAssetRefresh ( void ) ;
2021-08-21 23:12:37 -05:00
static bool setScriptFile ( void * obj , const char * index , const char * data )
{
static_cast < MaterialAsset * > ( obj ) - > setScriptFile ( data ) ;
return false ;
}
2019-05-04 11:49:42 -05:00
static const char * getScriptFile ( void * obj , const char * data ) { return static_cast < MaterialAsset * > ( obj ) - > getScriptFile ( ) ; }
2018-01-28 14:48:02 -06:00
} ;
DefineConsoleType ( TypeMaterialAssetPtr , MaterialAsset )
2020-11-01 23:32:34 -06:00
DefineConsoleType ( TypeMaterialAssetId , String )
2022-06-13 12:38:08 -05:00
# ifdef TORQUE_TOOLS
2018-01-28 14:48:02 -06:00
//-----------------------------------------------------------------------------
// TypeAssetId GuiInspectorField Class
//-----------------------------------------------------------------------------
2020-11-01 23:32:34 -06:00
class GuiInspectorTypeMaterialAssetPtr : public GuiInspectorTypeFileName
2018-01-28 14:48:02 -06:00
{
2020-11-01 23:32:34 -06:00
typedef GuiInspectorTypeFileName Parent ;
2018-01-28 14:48:02 -06:00
public :
2022-12-08 20:00:55 -06:00
GuiTextCtrl * mLabel ;
GuiBitmapButtonCtrl * mPreviewBorderButton ;
GuiBitmapCtrl * mPreviewImage ;
GuiButtonCtrl * mEditButton ;
2018-01-28 14:48:02 -06:00
DECLARE_CONOBJECT ( GuiInspectorTypeMaterialAssetPtr ) ;
static void consoleInit ( ) ;
virtual GuiControl * constructEditControl ( ) ;
virtual bool updateRects ( ) ;
2022-12-08 20:00:55 -06:00
virtual void updateValue ( ) ;
void updatePreviewImage ( ) ;
void setPreviewImage ( StringTableEntry assetId ) ;
2020-11-01 23:32:34 -06:00
} ;
class GuiInspectorTypeMaterialAssetId : public GuiInspectorTypeMaterialAssetPtr
{
typedef GuiInspectorTypeMaterialAssetPtr Parent ;
public :
DECLARE_CONOBJECT ( GuiInspectorTypeMaterialAssetId ) ;
static void consoleInit ( ) ;
2018-01-28 14:48:02 -06:00
} ;
2022-06-13 12:38:08 -05:00
# endif
2021-07-19 01:07:08 -05:00
# pragma region Singular Asset Macros
//Singular assets
/// <Summary>
/// Declares an material asset
/// This establishes the assetId, asset and legacy filepath fields, along with supplemental getter and setter functions
/// </Summary>
# define DECLARE_MATERIALASSET(className, name) public: \
StringTableEntry m # # name # # Name ; \
StringTableEntry m # # name # # AssetId ; \
AssetPtr < MaterialAsset > m # # name # # Asset ; \
SimObjectPtr < Material > m # # name ; \
public : \
const StringTableEntry get # # name # # File ( ) const { return m # # name # # Name ; } \
void set # # name # # Name ( const FileName & _in ) { m # # name # # Name = StringTable - > insert ( _in . c_str ( ) ) ; } \
const AssetPtr < MaterialAsset > & get # # name # # Asset ( ) const { return m # # name # # Asset ; } \
void set # # name # # Asset ( const AssetPtr < MaterialAsset > & _in ) { m # # name # # Asset = _in ; } \
2021-01-03 08:58:53 -06:00
\
2021-07-19 01:07:08 -05:00
bool _set # # name ( StringTableEntry _in ) \
2021-01-03 08:58:53 -06:00
{ \
2021-07-19 01:07:08 -05:00
if ( m # # name # # AssetId ! = _in | | m # # name # # Name ! = _in ) \
2021-01-03 08:58:53 -06:00
{ \
2021-08-29 17:41:20 -05:00
if ( _in = = NULL | | _in = = StringTable - > EmptyString ( ) ) \
2021-01-03 08:58:53 -06:00
{ \
2021-07-19 01:07:08 -05:00
m # # name # # Name = StringTable - > EmptyString ( ) ; \
m # # name # # AssetId = StringTable - > EmptyString ( ) ; \
m # # name # # Asset = NULL ; \
m # # name = NULL ; \
2021-01-03 08:58:53 -06:00
return true ; \
} \
2021-07-19 01:07:08 -05:00
\
if ( AssetDatabase . isDeclaredAsset ( _in ) ) \
2021-01-03 08:58:53 -06:00
{ \
2021-07-19 01:07:08 -05:00
m # # name # # AssetId = _in ; \
2021-01-03 08:58:53 -06:00
\
2021-07-19 01:07:08 -05:00
U32 assetState = MaterialAsset : : getAssetById ( m # # name # # AssetId , & m # # name # # Asset ) ; \
\
if ( MaterialAsset : : Ok = = assetState ) \
{ \
m # # name # # Name = StringTable - > EmptyString ( ) ; \
} \
2021-01-03 08:58:53 -06:00
} \
2021-07-19 01:07:08 -05:00
else \
{ \
StringTableEntry assetId = MaterialAsset : : getAssetIdByMaterialName ( _in ) ; \
if ( assetId ! = StringTable - > EmptyString ( ) ) \
{ \
m # # name # # AssetId = assetId ; \
if ( MaterialAsset : : getAssetById ( m # # name # # AssetId , & m # # name # # Asset ) = = MaterialAsset : : Ok ) \
{ \
m # # name # # Name = StringTable - > EmptyString ( ) ; \
} \
} \
else \
{ \
m # # name # # Name = _in ; \
m # # name # # AssetId = StringTable - > EmptyString ( ) ; \
m # # name # # Asset = NULL ; \
} \
} \
} \
if ( get # # name ( ) ! = StringTable - > EmptyString ( ) & & m # # name # # Asset . notNull ( ) ) \
{ \
if ( m # # name & & String ( m # # name # # Asset - > getMaterialDefinitionName ( ) ) . equal ( m # # name - > getName ( ) , String : : NoCase ) ) \
return false ; \
\
Material * tempMat = nullptr ; \
\
if ( ! Sim : : findObject ( m # # name # # Asset - > getMaterialDefinitionName ( ) , tempMat ) ) \
2021-07-22 12:02:23 -05:00
Con : : errorf ( " %s::_set%s() - Material %s was not found. " , macroText ( className ) , macroText ( name ) , m # # name # # Asset - > getMaterialDefinitionName ( ) ) ; \
2021-07-19 01:07:08 -05:00
m # # name = tempMat ; \
} \
else \
{ \
m # # name = NULL ; \
} \
\
if ( get # # name ( ) = = StringTable - > EmptyString ( ) ) \
return true ; \
\
if ( m # # name # # Asset . notNull ( ) & & m # # name # # Asset - > getStatus ( ) ! = MaterialAsset : : Ok ) \
{ \
Con : : errorf ( " %s::_set%s() - material asset failure \" %s \" due to [%s] " , macroText ( className ) , macroText ( name ) , _in , MaterialAsset : : getAssetErrstrn ( m # # name # # Asset - > getStatus ( ) ) . c_str ( ) ) ; \
return false ; \
2021-01-03 08:58:53 -06:00
} \
2021-09-09 13:29:31 -05:00
else if ( ! m # # name ) \
2021-07-19 01:07:08 -05:00
{ \
Con : : errorf ( " %s::_set%s() - Couldn't load material \" %s \" " , macroText ( className ) , macroText ( name ) , _in ) ; \
return false ; \
} \
return true ; \
2021-01-03 08:58:53 -06:00
} \
2021-07-19 01:07:08 -05:00
\
const StringTableEntry get # # name ( ) const \
2021-01-03 08:58:53 -06:00
{ \
2021-07-19 01:07:08 -05:00
if ( m # # name # # Asset & & ( m # # name # # Asset - > getMaterialDefinitionName ( ) ! = StringTable - > EmptyString ( ) ) ) \
return m # # name # # Asset - > getMaterialDefinitionName ( ) ; \
else if ( m # # name # # AssetId ! = StringTable - > EmptyString ( ) ) \
return m # # name # # AssetId ; \
else if ( m # # name # # Name ! = StringTable - > EmptyString ( ) ) \
return m # # name # # Name ; \
else \
return StringTable - > EmptyString ( ) ; \
2021-01-03 08:58:53 -06:00
} \
2021-07-19 01:07:08 -05:00
SimObjectPtr < Material > get # # name # # Resource ( ) \
{ \
2021-08-09 22:08:55 -05:00
return m # # name ; \
2021-09-28 18:16:14 -05:00
} \
2021-09-28 18:50:35 -05:00
bool is # # name # # Valid ( ) { return ( get # # name ( ) ! = StringTable - > EmptyString ( ) & & m # # name # # Asset - > getStatus ( ) = = AssetBase : : Ok ) ; }
2021-07-19 01:07:08 -05:00
# ifdef TORQUE_SHOW_LEGACY_FILE_FIELDS
# define INITPERSISTFIELD_MATERIALASSET(name, consoleClass, docs) \
addProtectedField ( # name , TypeMaterialName , Offset ( m # # name # # Name , consoleClass ) , _set # # name # # Data , & defaultProtectedGetFn , assetDoc ( name , docs ) ) ; \
addProtectedField ( assetText ( name , Asset ) , TypeMaterialAssetId , Offset ( m # # name # # AssetId , consoleClass ) , _set # # name # # Data , & defaultProtectedGetFn , assetDoc ( name , asset docs . ) ) ;
# else
# define INITPERSISTFIELD_MATERIALASSET(name, consoleClass, docs) \
addProtectedField ( # name , TypeMaterialName , Offset ( m # # name # # Name , consoleClass ) , _set # # name # # Data , & defaultProtectedGetFn , assetDoc ( name , docs ) , AbstractClassRep : : FIELD_HideInInspectors ) ; \
addProtectedField ( assetText ( name , Asset ) , TypeMaterialAssetId , Offset ( m # # name # # AssetId , consoleClass ) , _set # # name # # Data , & defaultProtectedGetFn , assetDoc ( name , asset docs . ) ) ;
# endif // SHOW_LEGACY_FILE_FIELDS
# define LOAD_MATERIALASSET(name)\
if ( m # # name # # AssetId ! = StringTable - > EmptyString ( ) ) \
2020-12-27 23:24:29 -06:00
{ \
2021-07-19 01:07:08 -05:00
S32 assetState = MaterialAsset : : getAssetById ( m # # name # # AssetId , & m # # name # # Asset ) ; \
if ( assetState = = MaterialAsset : : Ok ) \
2020-12-27 23:24:29 -06:00
{ \
2021-07-19 01:07:08 -05:00
m # # name # # Name = StringTable - > EmptyString ( ) ; \
2020-12-27 23:24:29 -06:00
} \
2021-07-19 01:07:08 -05:00
else Con : : warnf ( " Warning: %s::LOAD_MATERIALASSET(%s)-%s " , mClassName , m # # name # # AssetId , MaterialAsset : : getAssetErrstrn ( assetState ) . c_str ( ) ) ; \
}
# pragma endregion
2020-12-27 23:24:29 -06:00
2018-01-28 14:48:02 -06:00
# endif // _ASSET_BASE_H_