Merge pull request #357 from Areloch/ImageAssetHookins

Updates macromagic to properly set up for init'ing when image assets are set in material and terrain materials
This commit is contained in:
Brian Roberts 2020-10-17 16:21:24 -05:00 committed by GitHub
commit 072b5ecb19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 231 additions and 31 deletions

View file

@ -84,6 +84,34 @@ ConsoleSetType(TypeImageAssetPtr)
Con::warnf("(TypeImageAssetPtr) - Cannot set multiple args to a single asset.");
}
ConsoleType(assetIdString, TypeImageAssetId, String, ASSET_ID_FIELD_PREFIX)
ConsoleGetType(TypeImageAssetId)
{
// Fetch asset Id.
return *((const char**)(dptr));
}
ConsoleSetType(TypeImageAssetId)
{
// Was a single argument specified?
if (argc == 1)
{
// Yes, so fetch field value.
const char* pFieldValue = argv[0];
// Fetch asset Id.
StringTableEntry* assetId = (StringTableEntry*)(dptr);
// Update asset value.
*assetId = StringTable->insert(pFieldValue);
return;
}
// Warn.
Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset.");
}
//-----------------------------------------------------------------------------
ImplementEnumType(ImageAssetType,
@ -222,6 +250,22 @@ StringTableEntry ImageAsset::getAssetIdByFilename(StringTableEntry fileName)
return imageAssetId;
}
bool ImageAsset::getAssetById(StringTableEntry assetId, AssetPtr<ImageAsset>* imageAsset)
{
(*imageAsset) = assetId;
if (!imageAsset->isNull())
return true;
//Didn't work, so have us fall back to a placeholder asset
StringTableEntry noImageId = StringTable->insert("Core_Rendering:noMaterial");
imageAsset->setAssetId(noImageId);
if (!imageAsset->isNull())
return true;
return false;
}
//------------------------------------------------------------------------------
void ImageAsset::copyTo(SimObject* object)
{
@ -233,15 +277,15 @@ void ImageAsset::loadImage()
{
SAFE_DELETE(mImage);
if (mImageFileName)
if (mImagePath)
{
if (!Platform::isFile(mImageFileName))
if (!Platform::isFile(mImagePath))
{
Con::errorf("ImageAsset::initializeAsset: Attempted to load file %s but it was not valid!", mImageFileName);
return;
}
mImage.set(mImageFileName, &GFXStaticTextureSRGBProfile, avar("%s() - mImage (line %d)", __FUNCTION__, __LINE__));
mImage.set(mImagePath, &GFXStaticTextureSRGBProfile, avar("%s() - mImage (line %d)", __FUNCTION__, __LINE__));
if (mImage)
{
@ -292,6 +336,9 @@ GFXTexHandle ImageAsset::getImage(GFXTextureProfile requestedProfile)
return newImage;
}*/
if (mImage.isValid())
return mImage;
return nullptr;
}
@ -366,3 +413,100 @@ DefineEngineMethod(ImageAsset, getImageInfo, const char*, (), ,
{
return object->getImageInfo();
}
//-----------------------------------------------------------------------------
// GuiInspectorTypeAssetId
//-----------------------------------------------------------------------------
IMPLEMENT_CONOBJECT(GuiInspectorTypeImageAssetPtr);
ConsoleDocClass(GuiInspectorTypeImageAssetPtr,
"@brief Inspector field type for Shapes\n\n"
"Editor use only.\n\n"
"@internal"
);
void GuiInspectorTypeImageAssetPtr::consoleInit()
{
Parent::consoleInit();
ConsoleBaseType::getType(TypeImageAssetPtr)->setInspectorFieldType("GuiInspectorTypeImageAssetPtr");
}
GuiControl* GuiInspectorTypeImageAssetPtr::constructEditControl()
{
// Create base filename edit controls
GuiControl* retCtrl = Parent::constructEditControl();
if (retCtrl == NULL)
return retCtrl;
// Change filespec
char szBuffer[512];
dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"ImageAsset\", \"AssetBrowser.changeAsset\", %s, %s);",
mInspector->getInspectObject()->getIdString(), mCaption);
mBrowseButton->setField("Command", szBuffer);
const char* id = mInspector->getInspectObject()->getIdString();
setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
// Create "Open in ShapeEditor" button
mImageEdButton = new GuiBitmapButtonCtrl();
dSprintf(szBuffer, sizeof(szBuffer), "ShapeEditorPlugin.openShapeAssetId(%d.getText());", retCtrl->getId());
mImageEdButton->setField("Command", szBuffer);
char bitmapName[512] = "tools/worldEditor/images/toolbar/shape-editor";
mImageEdButton->setBitmap(bitmapName);
mImageEdButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
mImageEdButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
mImageEdButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
mImageEdButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this file in the Shape Editor");
mImageEdButton->registerObject();
addObject(mImageEdButton);
return retCtrl;
}
bool GuiInspectorTypeImageAssetPtr::updateRects()
{
S32 dividerPos, dividerMargin;
mInspector->getDivider(dividerPos, dividerMargin);
Point2I fieldExtent = getExtent();
Point2I fieldPos = getPosition();
mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
if (mBrowseButton != NULL)
{
mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
}
if (mImageEdButton != NULL)
{
RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
resized |= mImageEdButton->resize(shapeEdRect.point, shapeEdRect.extent);
}
return resized;
}
IMPLEMENT_CONOBJECT(GuiInspectorTypeImageAssetId);
ConsoleDocClass(GuiInspectorTypeImageAssetId,
"@brief Inspector field type for Shapes\n\n"
"Editor use only.\n\n"
"@internal"
);
void GuiInspectorTypeImageAssetId::consoleInit()
{
Parent::consoleInit();
ConsoleBaseType::getType(TypeImageAssetId)->setInspectorFieldType("GuiInspectorTypeImageAssetId");
}

View file

@ -45,6 +45,8 @@
#include "gfx/bitmap/gBitmap.h"
#include "gfx/gfxTextureHandle.h"
#include "gui/editor/guiInspectorTypes.h"
//-----------------------------------------------------------------------------
class ImageAsset : public AssetBase
{
@ -109,38 +111,63 @@ public:
void setImageType(ImageTypes type) { mImageType = type; }
bool getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAsset>* imageAsset);
StringTableEntry getAssetIdByFilename(StringTableEntry fileName);
static bool getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAsset>* imageAsset);
static StringTableEntry getAssetIdByFilename(StringTableEntry fileName);
static bool getAssetById(StringTableEntry assetId, AssetPtr<ImageAsset>* imageAsset);
protected:
virtual void initializeAsset(void);
virtual void onAssetRefresh(void);
static bool setImageFileName(void *obj, const char *index, const char *data) { static_cast<ImageAsset*>(obj)->setImageFileName(data); return false; }
static bool setImageFileName(void* obj, const char* index, const char* data) { static_cast<ImageAsset*>(obj)->setImageFileName(data); return false; }
static const char* getImageFileName(void* obj, const char* data) { return static_cast<ImageAsset*>(obj)->getImageFileName(); }
void loadImage();
};
DefineConsoleType(TypeImageAssetPtr, ImageAsset)
DefineConsoleType(TypeImageAssetId, String)
typedef ImageAsset::ImageTypes ImageAssetType;
DefineEnumType(ImageAssetType);
class GuiInspectorTypeImageAssetPtr : public GuiInspectorTypeFileName
{
typedef GuiInspectorTypeFileName Parent;
public:
GuiBitmapButtonCtrl* mImageEdButton;
DECLARE_CONOBJECT(GuiInspectorTypeImageAssetPtr);
static void consoleInit();
virtual GuiControl* constructEditControl();
virtual bool updateRects();
};
class GuiInspectorTypeImageAssetId : public GuiInspectorTypeImageAssetPtr
{
typedef GuiInspectorTypeImageAssetPtr Parent;
public:
DECLARE_CONOBJECT(GuiInspectorTypeImageAssetId);
static void consoleInit();
};
#define assetText(x,suff) std::string(std::string(#x) + std::string(#suff)).c_str()
#define initMapSlot(name) m##name##Filename = String::EmptyString; m##name##AssetId = StringTable->EmptyString(); m##name##Asset = NULL;
#define bindMapSlot(name) if (m##name##AssetId != String::EmptyString) m##name##Asset = m##name##AssetId;
#define scriptBindMapSlot(name, consoleClass, docs) addField(#name, TypeImageFilename, Offset(m##name##Filename, consoleClass), assetText(name, docs)); \
addField(assetText(name,Asset), TypeImageAssetPtr, Offset(m##name##AssetId, consoleClass), assetText(name,asset reference.));
addProtectedField(assetText(name, Asset), TypeImageAssetId, Offset(m##name##AssetId, consoleClass), consoleClass::_set##name##Asset, & defaultProtectedGetFn, assetText(name, asset reference.));
#define initMapArraySlot(name,id) m##name##Filename[id] = String::EmptyString; m##name##AssetId[id] = StringTable->EmptyString(); m##name##Asset[id] = NULL;
#define bindMapArraySlot(name,id) if (m##name##AssetId[id] != String::EmptyString) m##name##Asset[id] = m##name##AssetId[id];
#define scriptBindMapArraySlot(name, arraySize, consoleClass, docs) addField(#name, TypeImageFilename, Offset(m##name##Filename, consoleClass), arraySize, assetText(name, docs)); \
addField(assetText(name,Asset), TypeImageAssetPtr, Offset(m##name##AssetId, consoleClass), arraySize, assetText(name,asset reference.));
addProtectedField(assetText(name,Asset), TypeImageAssetId, Offset(m##name##AssetId, consoleClass), consoleClass::_set##name##AssetSlot, &defaultProtectedGetFn, arraySize, assetText(name,asset reference.));
#define DECLARE_TEXTUREMAP(name) protected: \
#define DECLARE_TEXTUREMAP(className,name) protected: \
FileName m##name##Filename;\
StringTableEntry m##name##AssetId;\
AssetPtr<ImageAsset> m##name##Asset;\
@ -148,16 +175,45 @@ DefineEnumType(ImageAssetType);
const String& get##name() const { return m##name##Filename; }\
void set##name(FileName _in) { m##name##Filename = _in; }\
const AssetPtr<ImageAsset> & get##name##Asset() const { return m##name##Asset; }\
void set##name##Asset(AssetPtr<ImageAsset>_in) { m##name##Asset = _in; }
void set##name##Asset(AssetPtr<ImageAsset>_in) { m##name##Asset = _in; }\
static bool _set##name##Asset(void* obj, const char* index, const char* data)\
{\
##className* mat = static_cast<##className*>(obj);\
mat->m##name##AssetId = StringTable->insert(data);\
if (ImageAsset::getAssetById(mat->m##name##AssetId, &mat->m##name##Asset))\
{\
if (mat->m##name##Asset.getAssetId() != StringTable->insert("Core_Rendering:noMaterial"))\
mat->m##name##Filename = StringTable->EmptyString();\
return true;\
}\
return true;\
}
#define GET_TEXTUREMAP(name) get##name()
#define SET_TEXTUREMAP(name,_in) set##name(_in)
#define GET_TEXTUREASSET(name) get##name##Asset()
#define SET_TEXTUREASSET(name,_in) set##name##Asset(_in)
#define DECLARE_TEXTUREARRAY(name,max) FileName m##name##Filename[max];\
#define DECLARE_TEXTUREARRAY(className,name,max) FileName m##name##Filename[max];\
StringTableEntry m##name##AssetId[max];\
AssetPtr<ImageAsset> m##name##Asset[max];
AssetPtr<ImageAsset> m##name##Asset[max];\
static bool _set##name##AssetSlot(void* obj, const char* index, const char* data)\
{\
##className* mat = static_cast<##className*>(obj);\
if (!index) return false;\
U32 idx = dAtoi(index);\
if (idx >= ##max)\
return false;\
mat->m##name##AssetId[idx] = StringTable->insert(data);\
if (ImageAsset::getAssetById(mat->m##name##AssetId[idx], &mat->m##name##Asset[idx]))\
{\
if (mat->m##name##Asset[idx].getAssetId() != StringTable->insert("Core_Rendering:noMaterial"))\
{\
mat->m##name##Filename[idx] = StringTable->EmptyString();\
}\
return true;\
}\
return true;\
}
#endif

View file

@ -121,7 +121,7 @@ Material::Material()
mRoughness[i] = 1.0f;
mMetalness[i] = 0.0f;
mIsSRGb[i] = true;
mIsSRGb[i] = false;
mInvertRoughness[i] = false;
mRoughnessChan[i] = 0;

View file

@ -204,27 +204,27 @@ public:
//-----------------------------------------------------------------------
// Data
//-----------------------------------------------------------------------
DECLARE_TEXTUREARRAY(DiffuseMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(Material, DiffuseMap, MAX_STAGES);
bool mDiffuseMapSRGB[MAX_STAGES]; // SRGB diffuse
DECLARE_TEXTUREARRAY(OverlayMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(LightMap, MAX_STAGES);;
DECLARE_TEXTUREARRAY(ToneMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(DetailMap, MAX_STAGES);;
DECLARE_TEXTUREARRAY(NormalMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(ORMConfigMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(Material, OverlayMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(Material, LightMap, MAX_STAGES);;
DECLARE_TEXTUREARRAY(Material, ToneMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(Material, DetailMap, MAX_STAGES);;
DECLARE_TEXTUREARRAY(Material, NormalMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(Material, ORMConfigMap, MAX_STAGES);
bool mIsSRGb[MAX_STAGES];
DECLARE_TEXTUREARRAY(RoughMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(Material, RoughMap, MAX_STAGES);
bool mInvertRoughness[MAX_STAGES];
F32 mRoughnessChan[MAX_STAGES];
DECLARE_TEXTUREARRAY(AOMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(Material, AOMap, MAX_STAGES);
F32 mAOChan[MAX_STAGES];
DECLARE_TEXTUREARRAY(MetalMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(Material, MetalMap, MAX_STAGES);
F32 mMetalChan[MAX_STAGES];
DECLARE_TEXTUREARRAY(GlowMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(Material, GlowMap, MAX_STAGES);
F32 mGlowMul[MAX_STAGES];
/// A second normal map which repeats at the detail map
/// scale and blended with the base normal map.
DECLARE_TEXTUREARRAY(DetailNormalMap, MAX_STAGES);
DECLARE_TEXTUREARRAY(Material, DetailNormalMap, MAX_STAGES);
/// The strength scalar for the detail normal map.
F32 mDetailNormalMapStrength[MAX_STAGES];

View file

@ -42,17 +42,17 @@ protected:
//AssetPtr<ImageAsset> mDiffuseAsset;
DECLARE_TEXTUREMAP(DiffuseMap);
DECLARE_TEXTUREMAP(TerrainMaterial, DiffuseMap);
/// The size of the diffuse base map in meters
/// used to generate its texture coordinates.
F32 mDiffuseSize;
///
DECLARE_TEXTUREMAP(NormalMap);
DECLARE_TEXTUREMAP(TerrainMaterial, NormalMap);
///
DECLARE_TEXTUREMAP(DetailMap);
DECLARE_TEXTUREMAP(TerrainMaterial, DetailMap);
/// The size of the detail map in meters used
/// to generate the texture coordinates for the
@ -66,7 +66,7 @@ protected:
F32 mDetailDistance;
///
DECLARE_TEXTUREMAP(ORMConfigMap);
DECLARE_TEXTUREMAP(TerrainMaterial, ORMConfigMap);
bool mIsSRGB;
bool mInvertRoughness;
@ -77,7 +77,7 @@ protected:
/// planes.
bool mSideProjection;
DECLARE_TEXTUREMAP(MacroMap);
DECLARE_TEXTUREMAP(TerrainMaterial, MacroMap);
F32 mMacroSize;
F32 mMacroStrength;
F32 mMacroDistance;