Przeglądaj źródła

Adds console function to compare file modified times
Adds console function to save a scaled image
Improved logic of generating previews for shape, material and image assets to regen if original asset loose file was modified
Added logic to generate scaled preview image for material and image assets to improve load times of AB

Areloch 4 lat temu
rodzic
commit
34f0f01cea

+ 7 - 0
Engine/source/T3D/assets/MaterialAsset.cpp

@@ -346,6 +346,13 @@ DefineEngineStaticMethod(MaterialAsset, getAssetIdByMaterialName, const char*, (
 {
    return MaterialAsset::getAssetIdByMaterialName(StringTable->insert(materialName));
 }
+
+DefineEngineMethod(MaterialAsset, getScriptPath, const char*, (), ,
+   "Queries the Asset Database to see if any asset exists that is associated with the provided material name.\n"
+   "@return The AssetId of the associated asset, if any.")
+{
+   return object->getScriptPath();
+}
 #endif
 
 //-----------------------------------------------------------------------------

+ 21 - 0
Engine/source/console/fileSystemFunctions.cpp

@@ -580,6 +580,27 @@ DefineEngineFunction( fileCreatedTime, String, ( const char* fileName ),,
    return buffer;
 }
 
+DefineEngineFunction(compareFileTimes, S32, (const char* fileA, const char* fileB), ("", ""),
+   "@brief Compares 2 files' modified file times."
+
+   "@param fileName Name and path of first file to compare\n"
+   "@param fileName Name and path of second file to compare\n"
+   "@return S32. If value is 1, then fileA is newer. If value is -1, then fileB is newer. If value is 0, they are equal.\n"
+   "@ingroup FileSystem")
+{
+   Con::expandScriptFilename(sgScriptFilenameBuffer, sizeof(sgScriptFilenameBuffer), fileA);
+
+   FileTime fileATime = { 0 };
+   Platform::getFileTimes(sgScriptFilenameBuffer, NULL, &fileATime);
+
+   Con::expandScriptFilename(sgScriptFilenameBuffer, sizeof(sgScriptFilenameBuffer), fileB);
+
+   FileTime fileBTime = { 0 };
+   Platform::getFileTimes(sgScriptFilenameBuffer, NULL, &fileBTime);
+
+   return Platform::compareFileTimes(fileATime, fileBTime);
+}
+
 DefineEngineFunction(fileDelete, bool, ( const char* path ),,
    "@brief Delete a file from the hard drive\n\n"
 

+ 52 - 0
Engine/source/gfx/bitmap/gBitmap.cpp

@@ -1360,3 +1360,55 @@ DefineEngineFunction( getBitmapInfo, String, ( const char *filename ),,
                                           image->getBytesPerPixel(),
                                           image->getFormat());
 }
+
+DefineEngineFunction(saveScaledImage, bool, (const char* bitmapSource, const char* bitmapDest, S32 resolutionSize), ("", "", 512),
+   "Returns image info in the following format: width TAB height TAB bytesPerPixel TAB format. "
+   "It will return an empty string if the file is not found.\n"
+   "@ingroup Rendering\n")
+{
+   Resource<GBitmap> image = GBitmap::load(bitmapSource);
+   if (!image)
+      return false;
+
+   Torque::Path sourcePath = Torque::Path(bitmapSource);
+
+   /*if (String("dds").equal(sourcePath.getExtension(), String::NoCase))
+   {
+      dds = DDSFile::load(correctPath, scalePower);
+      if (dds != NULL)
+      {
+         if (!dds->decompressToGBitmap(image))
+         {
+            delete image;
+            image = NULL;
+            return false;
+         }
+      }
+   }*/
+
+   image->extrudeMipLevels();
+
+   U32 mipCount = image->getNumMipLevels();
+   U32 targetMips = mFloor(mLog2((F32)resolutionSize)) + 1;
+
+   if (mipCount > targetMips)
+   {
+      image->chopTopMips(mipCount - targetMips);
+   }
+
+   // Open up the file on disk.
+   FileStream fs;
+   if (!fs.open(bitmapDest, Torque::FS::File::Write))
+   {
+      Con::errorf("saveScaledImage() - Failed to open output file '%s'!", bitmapDest);
+      return false;
+   }
+   else
+   {
+      image->writeBitmap("png", fs);
+
+      fs.close();
+   }
+
+   return true;
+}

+ 46 - 6
Templates/BaseGame/game/tools/assetBrowser/scripts/assetTypes/image.tscript

@@ -172,15 +172,55 @@ function AssetBrowser::importImageAsset(%this, %assetItem)
 
 function AssetBrowser::buildImageAssetPreview(%this, %assetDef, %previewData)
 {
+   %module = %this.dirHandler.getModuleFromAddress(makeRelativePath(filePath(%assetDef.getImagePath())));
+   %previewPath = "tools/resources/previewCache/" @ %module.moduleId @ "/";
+   
+   if(!IsDirectory(%previewPath))
+   {
+      %this.dirHandler.createFolder(%previewPath);
+   }
+   
+   %generatePreview = false;
+   
+   %previewFilePath = %previewPath @ %assetDef.assetName @ "_Preview.png";
+   if(!isFile(%previewFilePath) || (compareFileTimes(%assetDef.getImagePath(), %previewFilePath) == 1))
+   {
+      %generatePreview = true;
+   }
+   
+   %previewAssetName = %module.moduleId @ "_" @ %assetDef.assetName @ "_PreviewImage";
+   
+   if(%generatePreview)
+   {
+      displayEditorLoadingGui("Generating Image Asset Preview...");
+      
+      %success = saveScaledImage(%assetDef.getImagePath(), %previewFilePath);
+      
+      %previewAsset = new ImageAsset()
+      {
+         assetName = %previewAssetName;
+         versionId = 1;
+         imageFile = fileName(%previewFilePath);
+      };
+      
+      %previewImgAssetPath = %previewPath @ %previewAsset.assetName @ ".asset.taml";
+      %assetImportSuccessful = TAMLWrite(%previewAsset, %previewImgAssetPath); 
+      
+      %toolsModuleDef = ModuleDatabase.findModule("ToolsModule",1);
+         
+      %success = AssetDatabase.addDeclaredAsset(%toolsModuleDef, %previewImgAssetPath);
+      
+      hideEditorLoadingGui();
+   }
+   
+   //Revalidate. If it didn't work, just use the default placeholder one
+   if(!isFile(%previewFilePath))
+      %previewAssetName = "ToolsModule:genericAssetIcon_image";
+   
    %previewData.assetName = %assetDef.assetName;
    %previewData.assetPath = %assetDef.scriptFile;
-   //%previewData.doubleClickCommand = "EditorOpenFileInTorsion( "@%previewData.assetPath@", 0 );";
    
-   %imageFilePath = %assetDef.getImagePath();
-   if(isFile(%imageFilePath))
-      %previewData.previewImage = %imageFilePath;
-   else
-      %previewData.previewImage = "core/rendering/images/unavailable";
+   %previewData.previewImage = "ToolsModule:" @ %previewAssetName;//%assetDef.fileName;
    
    %previewData.assetFriendlyName = %assetDef.assetName;
    %previewData.assetDesc = %assetDef.description;

+ 57 - 21
Templates/BaseGame/game/tools/assetBrowser/scripts/assetTypes/material.tscript

@@ -423,41 +423,77 @@ function AssetBrowser::importMaterialAsset(%this, %assetItem)
 
 function AssetBrowser::buildMaterialAssetPreview(%this, %assetDef, %previewData)
 {
-   %previewData.assetName = %assetDef.assetName;
-   %previewData.assetPath = %assetDef.scriptFile;
+   %module = %this.dirHandler.getModuleFromAddress(makeRelativePath(filePath(%assetDef.getScriptPath())));
+   %previewPath = "tools/resources/previewCache/" @ %module.moduleId @ "/";
+
+   if(!IsDirectory(%previewPath))
+   {
+      %this.dirHandler.createFolder(%previewPath);
+   }
+
+   %generatePreview = false;
+
+   %previewFilePath = %previewPath @ %assetDef.assetName @ "_Preview.png";
+   if(!isFile(%previewFilePath) || (compareFileTimes(%assetDef.getImagePath(), %previewFilePath) == 1))
+   {
+      %generatePreview = true;
+   }
 
-   //Lotta prepwork
-   /*%previewData.doubleClickCommand = %assetDef@".materialDefinitionName.reload(); "
-                                   @ "$Tools::materialEditorList = \"\";"
-                                   @ "EWorldEditor.clearSelection();"
-                                   @ "MaterialEditorGui.currentObject = 0;"
-                                   @ "MaterialEditorGui.currentMode = \"asset\";"
-                                   @ "MaterialEditorGui.currentMaterial = "@%assetDef@".materialDefinitionName;"
-                                   @ "MaterialEditorGui.setActiveMaterial( "@%assetDef@".materialDefinitionName );"
-                                   @ "EditorGui.setEditor(MaterialEditorPlugin); "
-                                   @ "AssetBrowser.hideDialog();";*/
+   %previewAssetName = %module.moduleId @ "_" @ %assetDef.assetName @ "_PreviewImage";
                                    
-   if(%this.selectMode)
-      %previewData.doubleClickCommand = "AssetBrowser.selectAsset( AssetBrowser.selectedAsset );";
-   else
-      %previewData.doubleClickCommand = "AssetBrowser.editAsset( "@%assetDef@" );";
-   
-   %previewData.previewImage = "ToolsModule:materialIcon_image";
+   if(%generatePreview)
+   {
+      displayEditorLoadingGui("Generating Material Asset Preview...");
    
    if(isObject(%assetDef.materialDefinitionName))
    {
       if(isFile(%assetDef.materialDefinitionName.getDiffuseMap(0)))
-         %previewData.previewImage = %assetDef.materialDefinitionName.getDiffuseMap(0);
+         {
+            %difMap = %assetDef.materialDefinitionName.getDiffuseMap(0);
+         }
       else if(%assetDef.materialDefinitionName.getDiffuseMapAsset(0) !$= "")
       {
          %imgAsset = AssetDatabase.acquireAsset(%assetDef.materialDefinitionName.getDiffuseMapAsset(0));
-      %previewData.previewImage = %imgAsset.getImagePath();
+            %difMap = %imgAsset.getImagePath();
    }
-      //add cubemap preview here(for skybox materials)
+      
+         //%success = saveScaledImage(%difMap, %previewFilePath);
+
+         %previewAsset = new ImageAsset()
+         {
+            assetName = %previewAssetName;
+            versionId = 1;
+            imageFile = fileName(%previewFilePath);
+         };
+         
+         %previewImgAssetPath = %previewPath @ %previewAsset.assetName @ ".asset.taml";
+         %assetImportSuccessful = TAMLWrite(%previewAsset, %previewImgAssetPath); 
+         
+         %toolsModuleDef = ModuleDatabase.findModule("ToolsModule",1);
+            
+         %success = AssetDatabase.addDeclaredAsset(%toolsModuleDef, %previewImgAssetPath);
+      }
+      
+      hideEditorLoadingGui();
    }
    
+   //Revalidate. If it didn't work, just use the default placeholder one
+   if(!isFile(%previewFilePath))
+      %previewAssetName = "ToolsModule:genericAssetIcon_image";
+      
+   %previewData.assetName = %assetDef.assetName;
+   %previewData.assetPath = %assetDef.scriptFile;
+   
+   %previewData.previewImage = "ToolsModule:" @ %previewAssetName;//%assetDef.fileName;
+   
    %previewData.assetFriendlyName = %assetDef.assetName;
    %previewData.assetDesc = %assetDef.description;
+
+   if(%this.selectMode)
+      %previewData.doubleClickCommand = "AssetBrowser.selectAsset( AssetBrowser.selectedAsset );";
+   else
+      %previewData.doubleClickCommand = "AssetBrowser.editAsset( "@%assetDef@" );";
+   
    %previewData.tooltip = "Asset Name: " @ %assetDef.assetName @ "\n" @ 
                            "Asset Type: Material Asset\n" @  
                            "Asset Definition ID: " @  %assetDef;

+ 17 - 8
Templates/BaseGame/game/tools/assetBrowser/scripts/assetTypes/shape.tscript

@@ -245,15 +245,24 @@ function AssetBrowser::importShapeAsset(%this, %assetItem)
 function AssetBrowser::buildShapeAssetPreview(%this, %assetDef, %previewData)
 {
    %module = %this.dirHandler.getModuleFromAddress(makeRelativePath(filePath(%assetDef.getShapeFile())));
-   %previewPath = "tools/resources/shapePreviewCache/" @ %module.moduleId @ "/";
+   %previewPath = "tools/resources/previewCache/" @ %module.moduleId @ "/";
    
    if(!IsDirectory(%previewPath))
    {
       %this.dirHandler.createFolder(%previewPath);
    }
    
+   %generatePreview = false;
+   
    %previewFilePath = %previewPath @ %assetDef.assetName @ "_Preview.png";
-   if(!isFile(%previewFilePath))
+   if(!isFile(%previewFilePath) || (compareFileTimes(%assetDef.getShapeFile(), %previewFilePath) == 1))
+   {
+      %generatePreview = true;
+   }
+   
+   %previewAssetName = %module.moduleId @ "_" @ %assetDef.assetName @ "_PreviewImage";
+   
+   if(%generatePreview)
    {
       displayEditorLoadingGui("Generating Shape Asset Preview...");
       
@@ -275,15 +284,15 @@ function AssetBrowser::buildShapeAssetPreview(%this, %assetDef, %previewData)
       
       $TSLastDetail::dumpImposters = %oldImposterSetting;
       
-      %newAsset = new ImageAsset()
+      %previewAsset = new ImageAsset()
       {
-         assetName = %assetDef.assetName @ "_PreviewImage";
+         assetName = %previewAssetName;
          versionId = 1;
          imageFile = fileName(%previewFilePath);
       };
       
-      %previewImgAssetPath = %previewPath @ %newAsset.assetName @ ".asset.taml";
-      %assetImportSuccessful = TAMLWrite(%newAsset, %previewImgAssetPath); 
+      %previewImgAssetPath = %previewPath @ %previewAsset.assetName @ ".asset.taml";
+      %assetImportSuccessful = TAMLWrite(%previewAsset, %previewImgAssetPath); 
       
       %toolsModuleDef = ModuleDatabase.findModule("ToolsModule",1);
          
@@ -294,12 +303,12 @@ function AssetBrowser::buildShapeAssetPreview(%this, %assetDef, %previewData)
    
    //Revalidate. If it didn't work, just use the default placeholder one
    if(!isFile(%previewFilePath))
-      %previewFilePath = "ToolsModule:genericAssetIcon_image";
+      %previewAssetName = "ToolsModule:genericAssetIcon_image";
    
    %previewData.assetName = %assetDef.assetName;
    %previewData.assetPath = %assetDef.fileName;
 
-   %previewData.previewImage = "ToolsModule:" @ %assetDef.assetName @ "_PreviewImage";//%assetDef.fileName;
+   %previewData.previewImage = "ToolsModule:" @ %previewAssetName;//%assetDef.fileName;
    
    %previewData.assetFriendlyName = %assetDef.assetName;
    %previewData.assetDesc = %assetDef.description;