Merge pull request #246 from Areloch/AssetImporterUpdate

Various improvements, fixes and expansions for Asset Importing
This commit is contained in:
Brian Roberts 2020-07-12 14:08:02 -05:00 committed by GitHub
commit 3cd5932cef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 899 additions and 172 deletions

View file

@ -45,6 +45,8 @@
// Debug Profiling. // Debug Profiling.
#include "platform/profiler.h" #include "platform/profiler.h"
#include "T3D/assets/assetImporter.h"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
IMPLEMENT_CONOBJECT(ImageAsset); IMPLEMENT_CONOBJECT(ImageAsset);
@ -89,7 +91,7 @@ ImplementEnumType(ImageAssetType,
"@ingroup gameObjects") "@ingroup gameObjects")
{ ImageAsset::Albedo, "Albedo", "" }, { ImageAsset::Albedo, "Albedo", "" },
{ ImageAsset::Normal, "Normal", "" }, { ImageAsset::Normal, "Normal", "" },
{ ImageAsset::Composite, "Composite", "" }, { ImageAsset::PBRConfig, "PBRConfig", "" },
{ ImageAsset::GUI, "GUI", "" }, { ImageAsset::GUI, "GUI", "" },
{ ImageAsset::Roughness, "Roughness", "" }, { ImageAsset::Roughness, "Roughness", "" },
{ ImageAsset::AO, "AO", "" }, { ImageAsset::AO, "AO", "" },
@ -138,8 +140,31 @@ bool ImageAsset::getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAss
S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName); S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
if (foundAssetcount == 0) if (foundAssetcount == 0)
{ {
//Didn't find any assets, so have us fall back to a placeholder asset //Didn't find any assets
imageAsset->setAssetId(StringTable->insert("Core_Rendering:noshape")); //If possible, see if we can run an in-place import and the get the asset from that
#if TORQUE_DEBUG
Con::warnf("ImageAsset::getAssetByFilename - Attempted to in-place import a image file(%s) that had no associated asset", fileName);
#endif
AssetImporter* autoAssetImporter;
if (!Sim::findObject("autoAssetImporter", autoAssetImporter))
{
autoAssetImporter = new AssetImporter();
autoAssetImporter->registerObject("autoAssetImporter");
}
StringTableEntry resultingAssetId = autoAssetImporter->autoImportFile(fileName);
if (resultingAssetId != StringTable->EmptyString())
{
imageAsset->setAssetId(resultingAssetId);
if (!imageAsset->isNull())
return true;
}
//Didn't work, so have us fall back to a placeholder asset
imageAsset->setAssetId(StringTable->insert("Core_Rendering:noImage"));
if (!imageAsset->isNull()) if (!imageAsset->isNull())
return true; return true;
@ -154,6 +179,48 @@ bool ImageAsset::getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAss
return true; return true;
} }
} }
StringTableEntry ImageAsset::getAssetIdByFilename(StringTableEntry fileName)
{
StringTableEntry imageAssetId = StringTable->EmptyString();
AssetQuery query;
S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
if (foundAssetcount == 0)
{
//Didn't find any assets
//If possible, see if we can run an in-place import and the get the asset from that
#if TORQUE_DEBUG
Con::warnf("ImageAsset::getAssetByFilename - Attempted to in-place import a image file(%s) that had no associated asset", fileName);
#endif
AssetImporter* autoAssetImporter;
if (!Sim::findObject("autoAssetImporter", autoAssetImporter))
{
autoAssetImporter = new AssetImporter();
autoAssetImporter->registerObject("autoAssetImporter");
}
StringTableEntry resultingAssetId = autoAssetImporter->autoImportFile(fileName);
if (resultingAssetId != StringTable->EmptyString())
{
imageAssetId = resultingAssetId;
return imageAssetId;
}
//Didn't work, so have us fall back to a placeholder asset
imageAssetId = StringTable->insert("Core_Rendering:noImage");
}
else
{
//acquire and bind the asset, and return it out
imageAssetId = query.mAssetList[0];
}
return imageAssetId;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void ImageAsset::copyTo(SimObject* object) void ImageAsset::copyTo(SimObject* object)
{ {
@ -243,16 +310,16 @@ const char* ImageAsset::getImageTypeNameFromType(ImageAsset::ImageTypes type)
{ {
// must match ImageTypes order // must match ImageTypes order
static const char* _names[] = { static const char* _names[] = {
"Albedo" "Albedo",
"Normal" "Normal",
"Composite" "PBRConfig",
"GUI" "GUI",
"Roughness" "Roughness",
"AO" "AO",
"Metalness" "Metalness",
"Glow" "Glow",
"Particle" "Particle",
"Decal" "Decal",
"Cubemap" "Cubemap"
}; };

View file

@ -56,7 +56,7 @@ public:
{ {
Albedo = 0, Albedo = 0,
Normal = 1, Normal = 1,
Composite = 2, PBRConfig = 2,
GUI = 3, GUI = 3,
Roughness = 4, Roughness = 4,
AO = 5, AO = 5,
@ -106,6 +106,9 @@ public:
void setImageType(ImageTypes type) { mImageType = type; } void setImageType(ImageTypes type) { mImageType = type; }
bool getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAsset>* imageAsset);
StringTableEntry getAssetIdByFilename(StringTableEntry fileName);
protected: protected:
virtual void initializeAsset(void); virtual void initializeAsset(void);
virtual void onAssetRefresh(void); virtual void onAssetRefresh(void);
@ -114,8 +117,6 @@ protected:
static const char* getImageFileName(void* obj, const char* data) { return static_cast<ImageAsset*>(obj)->getImageFileName(); } static const char* getImageFileName(void* obj, const char* data) { return static_cast<ImageAsset*>(obj)->getImageFileName(); }
void loadImage(); void loadImage();
bool getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAsset>* imageAsset);
}; };
DefineConsoleType(TypeImageAssetPtr, ImageAsset) DefineConsoleType(TypeImageAssetPtr, ImageAsset)

View file

@ -298,6 +298,19 @@ bool GuiInspectorTypeMaterialAssetPtr::updateRects()
return resized; return resized;
} }
bool GuiInspectorTypeMaterialAssetPtr::resize(const Point2I& newPosition, const Point2I& newExtent)
{
if (!Parent::resize(newPosition, newExtent))
return false;
if (mMatEdContainer != NULL)
{
return updateRects();
}
return false;
}
void GuiInspectorTypeMaterialAssetPtr::setMaterialAsset(String assetId) void GuiInspectorTypeMaterialAssetPtr::setMaterialAsset(String assetId)
{ {
mTargetObject->setDataField(mCaption, "", assetId); mTargetObject->setDataField(mCaption, "", assetId);

View file

@ -104,6 +104,7 @@ public:
virtual GuiControl* constructEditControl(); virtual GuiControl* constructEditControl();
virtual bool updateRects(); virtual bool updateRects();
virtual bool resize(const Point2I& newPosition, const Point2I& newExtent);
void setMaterialAsset(String assetId); void setMaterialAsset(String assetId);
}; };

View file

@ -129,7 +129,7 @@ void ParticleAsset::copyTo(SimObject* object)
IMPLEMENT_CONOBJECT(GuiInspectorTypeParticleAssetPtr); IMPLEMENT_CONOBJECT(GuiInspectorTypeParticleAssetPtr);
ConsoleDocClass(GuiInspectorTypeParticleAssetPtr, ConsoleDocClass(GuiInspectorTypeParticleAssetPtr,
"@brief Inspector field type for Partial Asset Objects\n\n" "@brief Inspector field type for Particle Asset Objects\n\n"
"Editor use only.\n\n" "Editor use only.\n\n"
"@internal" "@internal"
); );

View file

@ -93,6 +93,8 @@ ConsoleSetType(TypePostEffectAssetPtr)
PostEffectAsset::PostEffectAsset() PostEffectAsset::PostEffectAsset()
{ {
mScriptFile = StringTable->EmptyString(); mScriptFile = StringTable->EmptyString();
mHLSLShaderFile = StringTable->EmptyString();
mGLSLShaderFile = StringTable->EmptyString();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -110,6 +112,10 @@ void PostEffectAsset::initPersistFields()
addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, PostEffectAsset), addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, PostEffectAsset),
&setScriptFile, &getScriptFile, "Path to the script file."); &setScriptFile, &getScriptFile, "Path to the script file.");
addProtectedField("hlslShader", TypeAssetLooseFilePath, Offset(mHLSLShaderFile, PostEffectAsset),
&setHLSLShaderFile, &getHLSLShaderFile, "Path to the hlsl shader file.");
addProtectedField("glslShader", TypeAssetLooseFilePath, Offset(mGLSLShaderFile, PostEffectAsset),
&setGLSLShaderFile, &getGLSLShaderFile, "Path to the glsl shader file.");
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -123,6 +129,8 @@ void PostEffectAsset::copyTo(SimObject* object)
void PostEffectAsset::initializeAsset() void PostEffectAsset::initializeAsset()
{ {
mScriptFile = expandAssetFilePath(mScriptFile); mScriptFile = expandAssetFilePath(mScriptFile);
mHLSLShaderFile = expandAssetFilePath(mHLSLShaderFile);
mGLSLShaderFile = expandAssetFilePath(mGLSLShaderFile);
if (Platform::isFile(mScriptFile)) if (Platform::isFile(mScriptFile))
Con::executeFile(mScriptFile, false, false); Con::executeFile(mScriptFile, false, false);
@ -131,6 +139,8 @@ void PostEffectAsset::initializeAsset()
void PostEffectAsset::onAssetRefresh() void PostEffectAsset::onAssetRefresh()
{ {
mScriptFile = expandAssetFilePath(mScriptFile); mScriptFile = expandAssetFilePath(mScriptFile);
mHLSLShaderFile = expandAssetFilePath(mHLSLShaderFile);
mGLSLShaderFile = expandAssetFilePath(mGLSLShaderFile);
if (Platform::isFile(mScriptFile)) if (Platform::isFile(mScriptFile))
Con::executeFile(mScriptFile, false, false); Con::executeFile(mScriptFile, false, false);
@ -154,3 +164,41 @@ void PostEffectAsset::setScriptFile(const char* pScriptFile)
// Refresh the asset. // Refresh the asset.
refreshAsset(); refreshAsset();
} }
void PostEffectAsset::setHLSLShaderFile(const char* pShaderFile)
{
// Sanity!
AssertFatal(pShaderFile != NULL, "Cannot use a NULL shader file.");
// Fetch image file.
pShaderFile = StringTable->insert(pShaderFile);
// Ignore no change,
if (pShaderFile == mHLSLShaderFile)
return;
// Update.
mHLSLShaderFile = pShaderFile;
// Refresh the asset.
refreshAsset();
}
void PostEffectAsset::setGLSLShaderFile(const char* pShaderFile)
{
// Sanity!
AssertFatal(pShaderFile != NULL, "Cannot use a NULL shader file.");
// Fetch image file.
pShaderFile = StringTable->insert(pShaderFile);
// Ignore no change,
if (pShaderFile == mGLSLShaderFile)
return;
// Update.
mGLSLShaderFile = pShaderFile;
// Refresh the asset.
refreshAsset();
}

View file

@ -47,6 +47,8 @@ class PostEffectAsset : public AssetBase
typedef AssetBase Parent; typedef AssetBase Parent;
StringTableEntry mScriptFile; StringTableEntry mScriptFile;
StringTableEntry mHLSLShaderFile;
StringTableEntry mGLSLShaderFile;
public: public:
PostEffectAsset(); PostEffectAsset();
@ -59,6 +61,11 @@ public:
void setScriptFile(const char* pScriptFile); void setScriptFile(const char* pScriptFile);
inline StringTableEntry getScriptFile(void) const { return mScriptFile; }; inline StringTableEntry getScriptFile(void) const { return mScriptFile; };
void setHLSLShaderFile(const char* pShaderFile);
inline StringTableEntry getHLSLShaderFile(void) const { return mHLSLShaderFile; };
void setGLSLShaderFile(const char* pShaderFile);
inline StringTableEntry getGLSLShaderFile(void) const { return mGLSLShaderFile; };
/// Declare Console Object. /// Declare Console Object.
DECLARE_CONOBJECT(PostEffectAsset); DECLARE_CONOBJECT(PostEffectAsset);
@ -68,6 +75,11 @@ protected:
static bool setScriptFile(void *obj, const char *index, const char *data) { static_cast<PostEffectAsset*>(obj)->setScriptFile(data); return false; } static bool setScriptFile(void *obj, const char *index, const char *data) { static_cast<PostEffectAsset*>(obj)->setScriptFile(data); return false; }
static const char* getScriptFile(void* obj, const char* data) { return static_cast<PostEffectAsset*>(obj)->getScriptFile(); } static const char* getScriptFile(void* obj, const char* data) { return static_cast<PostEffectAsset*>(obj)->getScriptFile(); }
static bool setHLSLShaderFile(void* obj, const char* index, const char* data) { static_cast<PostEffectAsset*>(obj)->setHLSLShaderFile(data); return false; }
static const char* getHLSLShaderFile(void* obj, const char* data) { return static_cast<PostEffectAsset*>(obj)->getHLSLShaderFile(); }
static bool setGLSLShaderFile(void* obj, const char* index, const char* data) { static_cast<PostEffectAsset*>(obj)->setGLSLShaderFile(data); return false; }
static const char* getGLSLShaderFile(void* obj, const char* data) { return static_cast<PostEffectAsset*>(obj)->getGLSLShaderFile(); }
}; };
DefineConsoleType(TypePostEffectAssetPtr, PostEffectAsset) DefineConsoleType(TypePostEffectAssetPtr, PostEffectAsset)

File diff suppressed because it is too large Load diff

View file

@ -403,6 +403,11 @@ public:
/// </summary> /// </summary>
Torque::Path filePath; Torque::Path filePath;
/// <summary>
/// What is the source file path of the importing asset in string form
/// </summary>
StringTableEntry filePathString;
/// <summary> /// <summary>
/// What is the asset's name /// What is the asset's name
/// </summary> /// </summary>
@ -486,11 +491,23 @@ public:
AssetImportObject(); AssetImportObject();
virtual ~AssetImportObject(); virtual ~AssetImportObject();
bool onAdd();
void onRemove();
/// Engine. /// Engine.
static void initPersistFields(); static void initPersistFields();
/// Declare Console Object. /// Declare Console Object.
DECLARE_CONOBJECT(AssetImportObject); DECLARE_CONOBJECT(AssetImportObject);
static bool _setFilePath(void* obj, const char* index, const char* data);
void setFilePath(StringTableEntry pFilePath);
bool operator == (const AssetImportObject& o) const
{
return o.getId() == this->getId();
}
}; };
/// <summary> /// <summary>
@ -551,6 +568,11 @@ class AssetImporter : public SimObject
/// </summary> /// </summary>
char importLogBuffer[1024]; char importLogBuffer[1024];
/// <summary>
/// only used for passing up the result of an import action for a script-side handled type
/// </summary>
String finalImportedAssetPath;
public: public:
AssetImporter(); AssetImporter();
virtual ~AssetImporter(); virtual ~AssetImporter();
@ -574,6 +596,13 @@ public:
/// </summary> /// </summary>
AssetImportObject* addImportingFile(Torque::Path filePath); AssetImportObject* addImportingFile(Torque::Path filePath);
/// <summary>
/// Adds an existing AssetImportObject to our import session. Generally this would be created in a script somewhere
/// <para>@param assetItem, The asset item to be added to the import session</para>
/// <para>@param parentItem (Optional), The asset item that will be the parent of the assetItem being added</para>
/// </summary>
void addImportingAssetItem(AssetImportObject* assetItem, AssetImportObject* parentItem);
/// <summary> /// <summary>
/// Adds an importing asset to the current session /// Adds an importing asset to the current session
/// <para>@param assetType, Type of the asset being imported</para> /// <para>@param assetType, Type of the asset being imported</para>
@ -623,8 +652,9 @@ public:
/// <summary> /// <summary>
/// Resets the import session to a clean slate. This will clear all existing AssetImportObjects and the activity log /// Resets the import session to a clean slate. This will clear all existing AssetImportObjects and the activity log
/// and then re-process the original filePaths again. /// and then re-process the original filePaths again.
/// <para>@param hardClearSession, Defaults to false. If true, will also clear the original filePaths</para>
/// </summary> /// </summary>
void resetImportSession(); void resetImportSession(bool hardClearSession = false);
/// <summary> /// <summary>
/// Get the number of lines in the activity log /// Get the number of lines in the activity log
@ -774,4 +804,32 @@ public:
/// <para>@return Current AssetImportConfig the importer is using</para> /// <para>@return Current AssetImportConfig the importer is using</para>
/// </summary> /// </summary>
AssetImportConfig* getImportConfig() { return &activeImportConfig; } AssetImportConfig* getImportConfig() { return &activeImportConfig; }
//
/// <summary>
/// </summary>
static inline String findImagePath(const String &testPath)
{
String imagePath;
if (Platform::isFile(testPath + String(".jpg")))
imagePath = testPath + String(".jpg");
else if (Platform::isFile(testPath + String(".png")))
imagePath = testPath + String(".png");
else if (Platform::isFile(testPath + String(".dds")))
imagePath = testPath + String(".dds");
else if (Platform::isFile(testPath + String(".tif")))
imagePath = testPath + String(".tif");
return imagePath;
}
static inline const char* makeFullPath(const String& path)
{
char qualifiedFilePath[2048];
Platform::makeFullPathName(path.c_str(), qualifiedFilePath, sizeof(qualifiedFilePath));
return qualifiedFilePath;
}
}; };

View file

@ -19,11 +19,11 @@ DefineEngineMethod(AssetImporter, setTargetPath, void, (String path), (""),
return object->setTargetPath(path); return object->setTargetPath(path);
} }
DefineEngineMethod(AssetImporter, resetImportSession, void, (), , DefineEngineMethod(AssetImporter, resetImportSession, void, (bool forceResetSession), (false),
"Creates a new script asset using the targetFilePath.\n" "Creates a new script asset using the targetFilePath.\n"
"@return The bool result of calling exec") "@return The bool result of calling exec")
{ {
return object->resetImportSession(); return object->resetImportSession(forceResetSession);
} }
DefineEngineMethod(AssetImporter, dumpActivityLog, void, (), , DefineEngineMethod(AssetImporter, dumpActivityLog, void, (), ,
@ -61,6 +61,13 @@ DefineEngineMethod(AssetImporter, addImportingFile, AssetImportObject*, (String
return object->addImportingFile(path); return object->addImportingFile(path);
} }
DefineEngineMethod(AssetImporter, addImportingAssetItem, void, (AssetImportObject* assetItem, AssetImportObject* parentItem), (nullAsType< AssetImportObject*>(), nullAsType< AssetImportObject*>()),
"Creates a new script asset using the targetFilePath.\n"
"@return The bool result of calling exec")
{
return object->addImportingAssetItem(assetItem, parentItem);
}
DefineEngineMethod(AssetImporter, processImportingAssets, void, (), , DefineEngineMethod(AssetImporter, processImportingAssets, void, (), ,
"Creates a new script asset using the targetFilePath.\n" "Creates a new script asset using the targetFilePath.\n"
"@return The bool result of calling exec") "@return The bool result of calling exec")
@ -123,6 +130,12 @@ DefineEngineMethod(AssetImporter, getAssetItemChild, AssetImportObject*, (AssetI
return object->getAssetItemChild(assetItem, index); return object->getAssetItemChild(assetItem, index);
} }
DefineEngineMethod(AssetImporter, deleteImportingAsset, void, (AssetImportObject* assetItem), (nullAsType< AssetImportObject*>()),
"Creates a new script asset using the targetFilePath.\n"
"@return The bool result of calling exec")
{
return object->deleteImportingAsset(assetItem);
}
/*DefineEngineFunction(enumColladaForImport, bool, (const char* shapePath, const char* ctrl, bool loadCachedDts), ("", "", true), /*DefineEngineFunction(enumColladaForImport, bool, (const char* shapePath, const char* ctrl, bool loadCachedDts), ("", "", true),
"(string shapePath, GuiTreeViewCtrl ctrl) Collect scene information from " "(string shapePath, GuiTreeViewCtrl ctrl) Collect scene information from "