2022-11-14 13:18:14 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
# include "AssetDefinition_Texture.h"
2023-01-10 15:24:20 -05:00
# include "AssetRegistry/AssetIdentifier.h"
2022-11-14 13:18:14 -05:00
# include "ContentBrowserMenuContexts.h"
# include "Engine/Texture2D.h"
# include "Materials/Material.h"
2022-12-12 13:40:46 -05:00
# include "Materials/MaterialFunction.h"
2022-11-14 13:18:14 -05:00
# include "Factories/MaterialFactoryNew.h"
# include "Interfaces/ITextureEditorModule.h"
2023-01-10 15:24:20 -05:00
# include "ToolMenu.h"
2022-11-14 13:18:14 -05:00
# include "ToolMenus.h"
# include "IAssetTools.h"
2023-01-10 15:24:20 -05:00
# include "ToolMenuSection.h"
2022-11-14 13:18:14 -05:00
# include "VirtualTexturingEditorModule.h"
# include "Algo/AnyOf.h"
# include "AssetRegistry/AssetRegistryHelpers.h"
2023-11-07 13:52:43 -05:00
# include "TextureAssetActions.h"
2022-11-14 13:18:14 -05:00
# define LOCTEXT_NAMESPACE "UAssetDefinition_Texture"
EAssetCommandResult UAssetDefinition_Texture : : OpenAssets ( const FAssetOpenArgs & OpenArgs ) const
{
for ( UTexture * Texture : OpenArgs . LoadObjects < UTexture > ( ) )
{
ITextureEditorModule * TextureEditorModule = & FModuleManager : : LoadModuleChecked < ITextureEditorModule > ( " TextureEditor " ) ;
2022-11-15 19:40:38 -05:00
TextureEditorModule - > CreateTextureEditor ( OpenArgs . GetToolkitMode ( ) , OpenArgs . ToolkitHost , Texture ) ;
2022-11-14 13:18:14 -05:00
}
return EAssetCommandResult : : Handled ;
}
2023-09-05 10:01:26 -04:00
FAssetOpenSupport UAssetDefinition_Texture : : GetAssetOpenSupport ( const FAssetOpenSupportArgs & OpenSupportArgs ) const
{
return FAssetOpenSupport ( OpenSupportArgs . OpenMethod , OpenSupportArgs . OpenMethod = = EAssetOpenMethod : : Edit | | OpenSupportArgs . OpenMethod = = EAssetOpenMethod : : View ) ;
}
2022-11-14 13:18:14 -05:00
// Menu Extensions
//--------------------------------------------------------------------
namespace MenuExtension_Texture
{
2023-11-18 17:08:36 -05:00
static void ExecuteCreateMaterial ( const FToolMenuContext & InContext )
2022-11-14 13:18:14 -05:00
{
const UContentBrowserAssetContextMenuContext * CBContext = UContentBrowserAssetContextMenuContext : : FindContextWithAssets ( InContext ) ;
2023-01-12 14:42:57 -05:00
IAssetTools : : Get ( ) . CreateAssetsFrom < UTexture > (
CBContext - > LoadSelectedObjects < UTexture > ( ) , UMaterial : : StaticClass ( ) , TEXT ( " _Mat " ) , [ ] ( UTexture * SourceObject )
2022-11-14 13:18:14 -05:00
{
UMaterialFactoryNew * Factory = NewObject < UMaterialFactoryNew > ( ) ;
2023-01-12 14:42:57 -05:00
Factory - > InitialTexture = SourceObject ;
return Factory ;
2022-11-14 13:18:14 -05:00
}
2023-01-12 14:42:57 -05:00
) ;
2022-11-14 13:18:14 -05:00
}
2023-11-07 13:52:43 -05:00
static void ExecuteResizeTextureSource ( const FToolMenuContext & InContext )
{
if ( const UContentBrowserAssetContextMenuContext * CBContext = UContentBrowserAssetContextMenuContext : : FindContextWithAssets ( InContext ) )
{
2023-11-16 15:36:39 -05:00
UE : : TextureAssetActions : : TextureSource_Resize_WithDialog ( CBContext - > LoadSelectedObjects < UTexture > ( ) ) ;
}
}
2023-11-22 18:08:33 -05:00
static void ExecuteResizeToPowerOfTwoTextureSource ( const FToolMenuContext & InContext )
{
if ( const UContentBrowserAssetContextMenuContext * CBContext = UContentBrowserAssetContextMenuContext : : FindContextWithAssets ( InContext ) )
{
UE : : TextureAssetActions : : TextureSource_ResizeToPowerOfTwo_WithDialog ( CBContext - > LoadSelectedObjects < UTexture > ( ) ) ;
}
}
2023-11-16 15:36:39 -05:00
static void Execute8bitTextureSource ( const FToolMenuContext & InContext )
{
if ( const UContentBrowserAssetContextMenuContext * CBContext = UContentBrowserAssetContextMenuContext : : FindContextWithAssets ( InContext ) )
{
UE : : TextureAssetActions : : TextureSource_ConvertTo8bit_WithDialog ( CBContext - > LoadSelectedObjects < UTexture > ( ) ) ;
2023-11-07 13:52:43 -05:00
}
}
2023-11-18 17:08:36 -05:00
static void ExecuteJPEGTextureSource ( const FToolMenuContext & InContext )
{
if ( const UContentBrowserAssetContextMenuContext * CBContext = UContentBrowserAssetContextMenuContext : : FindContextWithAssets ( InContext ) )
{
UE : : TextureAssetActions : : TextureSource_JPEG_WithDialog ( CBContext - > LoadSelectedObjects < UTexture > ( ) ) ;
}
}
2023-11-07 13:52:43 -05:00
2022-11-14 13:18:14 -05:00
static void ExecuteConvertToVirtualTexture ( const FToolMenuContext & InContext )
{
if ( const UContentBrowserAssetContextMenuContext * CBContext = UContentBrowserAssetContextMenuContext : : FindContextWithAssets ( InContext ) )
{
IVirtualTexturingEditorModule * Module = FModuleManager : : Get ( ) . GetModulePtr < IVirtualTexturingEditorModule > ( " VirtualTexturingEditor " ) ;
Module - > ConvertVirtualTexturesWithDialog ( CBContext - > LoadSelectedObjects < UTexture2D > ( ) , false ) ;
}
}
static void ExecuteConvertToRegularTexture ( const FToolMenuContext & InContext )
{
if ( const UContentBrowserAssetContextMenuContext * CBContext = UContentBrowserAssetContextMenuContext : : FindContextWithAssets ( InContext ) )
{
IVirtualTexturingEditorModule * Module = FModuleManager : : Get ( ) . GetModulePtr < IVirtualTexturingEditorModule > ( " VirtualTexturingEditor " ) ;
Module - > ConvertVirtualTexturesWithDialog ( CBContext - > LoadSelectedObjects < UTexture2D > ( ) , true ) ;
}
}
static void ExecuteFindMaterials ( const FToolMenuContext & InContext )
{
if ( const UContentBrowserAssetContextMenuContext * CBContext = UContentBrowserAssetContextMenuContext : : FindContextWithAssets ( InContext ) )
{
TArray < FAssetData > MaterialsUsingTexture ;
// This finds "Material like" objects. It's called material in the UI string but this generally
// seems more useful
if ( const FAssetData * TextureAsset = CBContext - > GetSingleSelectedAssetOfType ( UTexture : : StaticClass ( ) ) )
{
UAssetRegistryHelpers : : FindReferencersOfAssetOfClass ( TextureAsset - > PackageName , { UMaterialInterface : : StaticClass ( ) , UMaterialFunction : : StaticClass ( ) } , MaterialsUsingTexture ) ;
}
if ( MaterialsUsingTexture . Num ( ) > 0 )
{
IAssetTools : : Get ( ) . SyncBrowserToAssets ( MaterialsUsingTexture ) ;
}
}
}
static FDelayedAutoRegisterHelper DelayedAutoRegister ( EDelayedRegisterRunPhase : : EndOfEngineInit , [ ] {
UToolMenus : : RegisterStartupCallback ( FSimpleMulticastDelegate : : FDelegate : : CreateLambda ( [ ] ( )
{
FToolMenuOwnerScoped OwnerScoped ( UE_MODULE_NAME ) ;
2023-11-07 13:52:43 -05:00
{
UToolMenu * Menu = UE : : ContentBrowser : : ExtendToolMenu_AssetContextMenu ( UTexture : : StaticClass ( ) ) ;
2022-11-14 13:18:14 -05:00
2023-11-07 13:52:43 -05:00
FToolMenuSection & Section = Menu - > FindOrAddSection ( " GetAssetActions " ) ;
2023-01-10 15:49:07 -05:00
Section . AddDynamicEntry ( NAME_None , FNewToolMenuSectionDelegate : : CreateLambda ( [ ] ( FToolMenuSection & InSection )
2022-11-14 13:18:14 -05:00
{
if ( const UContentBrowserAssetContextMenuContext * Context = UContentBrowserAssetContextMenuContext : : FindContextWithAssets ( InSection ) )
{
{
const TAttribute < FText > Label = LOCTEXT ( " Texture_CreateMaterial " , " Create Material " ) ;
const TAttribute < FText > ToolTip = LOCTEXT ( " Texture_CreateMaterialTooltip " , " Creates a new material using this texture. " ) ;
const FSlateIcon Icon = FSlateIcon ( FAppStyle : : GetAppStyleSetName ( ) , " ClassIcon.Material " ) ;
const FToolMenuExecuteAction UIAction = FToolMenuExecuteAction : : CreateStatic ( & ExecuteCreateMaterial ) ;
InSection . AddMenuEntry ( " Texture_CreateMaterial " , Label , ToolTip , Icon , UIAction ) ;
}
2023-11-07 13:52:43 -05:00
if ( Context - > SelectedAssets . Num ( ) = = 1 )
{
const TAttribute < FText > Label = LOCTEXT ( " Texture_FindMaterials " , " Find Materials Using This " ) ;
const TAttribute < FText > ToolTip = LOCTEXT ( " Texture_FindMaterialsTooltip " , " Finds all materials that use this material in the content browser. " ) ;
const FSlateIcon Icon = FSlateIcon ( FAppStyle : : GetAppStyleSetName ( ) , " Icons.Find " ) ;
const FToolMenuExecuteAction UIAction = FToolMenuExecuteAction : : CreateStatic ( & ExecuteFindMaterials ) ;
InSection . AddMenuEntry ( " Texture_FindMaterials " , Label , ToolTip , Icon , UIAction ) ;
}
2023-11-22 18:08:33 -05:00
{
const TAttribute < FText > Label = LOCTEXT ( " Texture_ResizeSourceToPowerOfTwo " , " Texture Source Resize To Power of Two " ) ;
const TAttribute < FText > ToolTip = LOCTEXT ( " Texture_ResizeSourceToPowerOfTwoTooltip " , " Change texture source dimensions to the nearest power of two. " ) ;
const FSlateIcon Icon = FSlateIcon ( FAppStyle : : GetAppStyleSetName ( ) , " ClassIcon.Texture2D " ) ;
const FToolMenuExecuteAction UIAction = FToolMenuExecuteAction : : CreateStatic ( & ExecuteResizeToPowerOfTwoTextureSource ) ;
InSection . AddMenuEntry ( " Texture_ResizeSourceToPowerOfTwo " , Label , ToolTip , Icon , UIAction ) ;
}
2023-11-07 13:52:43 -05:00
{
2023-11-16 15:36:39 -05:00
const TAttribute < FText > Label = LOCTEXT ( " Texture_ResizeSource " , " Texture Source Reduce Size " ) ;
2023-11-07 13:52:43 -05:00
const TAttribute < FText > ToolTip = LOCTEXT ( " Texture_ResizeSourceTooltip " , " Reduce texture asset size by shrinking the texture source dimensions. " ) ;
const FSlateIcon Icon = FSlateIcon ( FAppStyle : : GetAppStyleSetName ( ) , " ClassIcon.Texture2D " ) ;
const FToolMenuExecuteAction UIAction = FToolMenuExecuteAction : : CreateStatic ( & ExecuteResizeTextureSource ) ;
InSection . AddMenuEntry ( " Texture_ResizeSource " , Label , ToolTip , Icon , UIAction ) ;
}
2023-11-16 15:36:39 -05:00
{
const TAttribute < FText > Label = LOCTEXT ( " Texture_ConvertTo8bit " , " Texture Source Convert To 8 bit or minimum bit depth " ) ;
const TAttribute < FText > ToolTip = LOCTEXT ( " Texture_ConvertTo8bitTooltip " , " Reduce texture asset size by converting 16/32 bit source data to 8 bit or minimum compatible bit depth. " ) ;
const FSlateIcon Icon = FSlateIcon ( FAppStyle : : GetAppStyleSetName ( ) , " ClassIcon.Texture2D " ) ;
const FToolMenuExecuteAction UIAction = FToolMenuExecuteAction : : CreateStatic ( & Execute8bitTextureSource ) ;
InSection . AddMenuEntry ( " Texture_ConvertTo8bit " , Label , ToolTip , Icon , UIAction ) ;
}
2023-11-18 17:08:36 -05:00
{
const TAttribute < FText > Label = LOCTEXT ( " Texture2D_JPEGSource " , " Texture Source Compress With JPEG " ) ;
const TAttribute < FText > ToolTip = LOCTEXT ( " Texture2D_JPEGSourceTooltip " , " Reduce texture asset size by compressing source with JPEG. " ) ;
const FSlateIcon Icon = FSlateIcon ( FAppStyle : : GetAppStyleSetName ( ) , " ClassIcon.Texture2D " ) ;
const FToolMenuExecuteAction UIAction = FToolMenuExecuteAction : : CreateStatic ( & ExecuteJPEGTextureSource ) ;
InSection . AddMenuEntry ( " Texture2D_JPEGSource " , Label , ToolTip , Icon , UIAction ) ;
}
2023-11-07 13:52:43 -05:00
}
} ) ) ;
}
{
// VT actions should only be on Texture2D , not all UTexture
UToolMenu * Menu = UE : : ContentBrowser : : ExtendToolMenu_AssetContextMenu ( UTexture2D : : StaticClass ( ) ) ;
FToolMenuSection & Section = Menu - > FindOrAddSection ( " GetAssetActions " ) ;
Section . AddDynamicEntry ( NAME_None , FNewToolMenuSectionDelegate : : CreateLambda ( [ ] ( FToolMenuSection & InSection )
{
if ( const UContentBrowserAssetContextMenuContext * Context = UContentBrowserAssetContextMenuContext : : FindContextWithAssets ( InSection ) )
{
2023-03-30 02:48:05 -04:00
static const auto CVarVirtualTexturesEnabled = IConsoleManager : : Get ( ) . FindTConsoleVariableDataInt ( TEXT ( " r.VirtualTextures " ) ) ;
check ( CVarVirtualTexturesEnabled ) ;
2022-11-17 00:29:54 -05:00
2023-03-30 02:48:05 -04:00
bool bVTEnabled = ! ! CVarVirtualTexturesEnabled - > GetValueOnAnyThread ( ) ;
static const auto CVarVirtualTexturesMenuRestricted = IConsoleManager : : Get ( ) . FindTConsoleVariableDataInt ( TEXT ( " r.VT.MenuRestricted " ) ) ;
check ( CVarVirtualTexturesMenuRestricted ) ;
bool bVTMenuRestricted = ! ! CVarVirtualTexturesMenuRestricted - > GetValueOnAnyThread ( ) ;
2022-11-17 00:29:54 -05:00
2023-03-30 02:48:05 -04:00
if ( bVTEnabled & & ! bVTMenuRestricted )
{
const bool bHasVirtualTextures =
Algo : : AnyOf ( Context - > SelectedAssets , [ ] ( const FAssetData & AssetData ) {
bool VirtualTextured = false ;
AssetData . GetTagValue < bool > ( " VirtualTextureStreaming " , VirtualTextured ) ;
return VirtualTextured ;
} ) ;
2023-11-07 13:52:43 -05:00
const bool bHasNonVirtualTextures =
Algo : : AnyOf ( Context - > SelectedAssets , [ ] ( const FAssetData & AssetData ) {
bool VirtualTextured = false ;
AssetData . GetTagValue < bool > ( " VirtualTextureStreaming " , VirtualTextured ) ;
return ! VirtualTextured ;
} ) ;
2023-03-30 02:48:05 -04:00
if ( bHasVirtualTextures )
{
2023-11-07 13:52:43 -05:00
const TAttribute < FText > Label = LOCTEXT ( " Texture_ConvertToRegular " , " Convert VT to Regular Texture " ) ;
2023-03-30 02:48:05 -04:00
const TAttribute < FText > ToolTip = LOCTEXT ( " Texture_ConvertToRegularTooltip " , " Converts this texture to a regular 2D texture if it is a virtual texture. " ) ;
const FSlateIcon Icon = FSlateIcon ( FAppStyle : : GetAppStyleSetName ( ) , " ClassIcon.Texture2D " ) ;
const FToolMenuExecuteAction UIAction = FToolMenuExecuteAction : : CreateStatic ( & ExecuteConvertToRegularTexture ) ;
2023-11-07 13:52:43 -05:00
InSection . AddMenuEntry ( " Texture_ConvertToRegular " , Label , ToolTip , Icon , UIAction ) ;
2023-03-30 02:48:05 -04:00
}
2023-11-07 13:52:43 -05:00
if ( bHasNonVirtualTextures )
2023-03-30 02:48:05 -04:00
{
const TAttribute < FText > Label = LOCTEXT ( " Texture_ConvertToVT " , " Convert to Virtual Texture " ) ;
2023-11-07 13:52:43 -05:00
const TAttribute < FText > ToolTip = LOCTEXT ( " Texture_ConvertToVTTooltip " , " Converts this texture to a virtual texture if it exceeds the specified size. " ) ;
2023-03-30 02:48:05 -04:00
const FSlateIcon Icon = FSlateIcon ( FAppStyle : : GetAppStyleSetName ( ) , " ClassIcon.Texture2D " ) ;
const FToolMenuExecuteAction UIAction = FToolMenuExecuteAction : : CreateStatic ( & ExecuteConvertToVirtualTexture ) ;
InSection . AddMenuEntry ( " Texture_ConvertToVT " , Label , ToolTip , Icon , UIAction ) ;
}
}
2022-11-14 13:18:14 -05:00
}
} ) ) ;
2023-11-07 13:52:43 -05:00
}
2022-11-14 13:18:14 -05:00
} ) ) ;
} ) ;
}
# undef LOCTEXT_NAMESPACE