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
2018-01-28 14:48:02 -06:00
# include "gui/editor/guiInspectorTypes.h"
# include "materials/matTextureTarget.h"
# include "materials/materialDefinition.h"
# include "materials/customMaterialDefinition.h"
2021-07-19 01:07:08 -05:00
# include "materials/materialManager.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 ;
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 ) ;
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 )
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 :
2020-11-01 23:32:34 -06:00
GuiBitmapButtonCtrl * mEditButton ;
2018-01-28 14:48:02 -06:00
DECLARE_CONOBJECT ( GuiInspectorTypeMaterialAssetPtr ) ;
static void consoleInit ( ) ;
virtual GuiControl * constructEditControl ( ) ;
virtual bool updateRects ( ) ;
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
} ;
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-08-29 18:32:44 -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-07-19 01:07:08 -05:00
}
# define DECLARE_MATERIALASSET_SETGET(className, name)\
static bool _set # # name # # Data ( void * obj , const char * index , const char * data ) \
{ \
bool ret = false ; \
className * object = static_cast < className * > ( obj ) ; \
ret = object - > _set # # name ( StringTable - > insert ( data ) ) ; \
return ret ; \
}
# define DECLARE_MATERIALASSET_NET_SETGET(className, name, bitmask)\
static bool _set # # name # # Data ( void * obj , const char * index , const char * data ) \
{ \
bool ret = false ; \
className * object = static_cast < className * > ( obj ) ; \
ret = object - > _set # # name ( StringTable - > insert ( data ) ) ; \
if ( ret ) \
object - > setMaskBits ( bitmask ) ; \
return ret ; \
}
# define DEF_MATERIALASSET_BINDS(className,name)\
DefineEngineMethod ( className , get # # name , const char * , ( ) , , " get name " ) \
{ \
return object - > get # # name ( ) ; \
2021-01-03 08:58:53 -06:00
} \
2021-07-19 01:07:08 -05:00
DefineEngineMethod ( className , get # # name # # Asset , const char * , ( ) , , assetText ( name , asset reference ) ) \
2021-01-03 08:58:53 -06:00
{ \
2021-07-19 01:07:08 -05:00
return object - > m # # name # # AssetId ; \
2021-01-03 08:58:53 -06:00
} \
2021-07-19 01:07:08 -05:00
DefineEngineMethod ( className , set # # name , bool , ( const char * mat ) , , assetText ( name , assignment . first tries asset then material name . ) ) \
2021-01-03 08:58:53 -06:00
{ \
2021-08-07 19:27:01 -05:00
return object - > _set # # name ( StringTable - > insert ( mat ) ) ; \
2021-01-03 08:58:53 -06:00
}
2020-12-27 23:24:29 -06:00
2021-07-19 01:07:08 -05:00
# define INIT_MATERIALASSET(name) \
m # # name # # Name = StringTable - > EmptyString ( ) ; \
m # # name # # AssetId = StringTable - > EmptyString ( ) ; \
m # # name # # Asset = NULL ; \
m # # name = NULL ;
# 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 CLONE_MATERIALASSET(name) \
m # # name # # Name = other . m # # name # # Name ; \
m # # name # # AssetId = other . m # # name # # AssetId ; \
m # # name # # Asset = other . m # # name # # Asset ;
# 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 ( ) ) ; \
}
# define PACKDATA_MATERIALASSET(name)\
if ( stream - > writeFlag ( m # # name # # Asset . notNull ( ) ) ) \
2020-12-27 23:24:29 -06:00
{ \
2021-07-19 01:07:08 -05:00
stream - > writeString ( m # # name # # Asset . getAssetId ( ) ) ; \
2021-01-03 08:58:53 -06:00
} \
2021-07-19 01:07:08 -05:00
else \
stream - > writeString ( m # # name # # Name ) ;
# define UNPACKDATA_MATERIALASSET(name)\
if ( stream - > readFlag ( ) ) \
2021-01-03 08:58:53 -06:00
{ \
2021-07-19 01:07:08 -05:00
m # # name # # AssetId = stream - > readSTString ( ) ; \
_set # # name ( m # # name # # AssetId ) ; \
2020-12-27 23:24:29 -06:00
} \
2021-07-19 01:07:08 -05:00
else \
m # # name # # Name = stream - > readSTString ( ) ;
2021-01-03 08:58:53 -06:00
2021-07-19 01:07:08 -05:00
# define PACK_MATERIALASSET(netconn, name)\
2020-12-27 23:24:29 -06:00
if ( stream - > writeFlag ( m # # name # # Asset . notNull ( ) ) ) \
{ \
NetStringHandle assetIdStr = m # # name # # Asset . getAssetId ( ) ; \
2020-12-30 19:36:51 -06:00
netconn - > packNetStringHandleU ( stream , assetIdStr ) ; \
2020-12-27 23:24:29 -06:00
} \
else \
stream - > writeString ( m # # name # # Name ) ;
2021-07-19 01:07:08 -05:00
# define UNPACK_MATERIALASSET(netconn, name)\
2020-12-27 23:24:29 -06:00
if ( stream - > readFlag ( ) ) \
{ \
2020-12-30 19:36:51 -06:00
m # # name # # AssetId = StringTable - > insert ( netconn - > unpackNetStringHandleU ( stream ) . getString ( ) ) ; \
2021-07-19 01:07:08 -05:00
_set # # name ( m # # name # # AssetId ) ; \
2020-12-27 23:24:29 -06:00
} \
else \
2021-07-19 01:07:08 -05:00
m # # name # # Name = stream - > readSTString ( ) ;
# pragma endregion
2020-12-27 23:24:29 -06:00
2018-01-28 14:48:02 -06:00
# endif // _ASSET_BASE_H_