Files
UnrealEngineUWP/Engine/Source/Developer/MaterialBaking/Private/MaterialBakingModule.h
sebastien lussier 3b200ed189 PR #7916: Lucasfilm: MaterialBaking: Fixes and improvements for material baking (Contributed by lflecunneen)
- Fixes for baking decal materials
- Option to return full HDR emissive color value
- Option to make all values linear by default, except for colors
#jira UE-115192
#rb sebastien.lussier

#ushell-cherrypick of 16289332 by ben.ingram
#ushell-cherrypick of 16373665 by sebastien.lussier
#ushell-cherrypick of 16413094 by sebastien.lussier

#ROBOMERGE-SOURCE: CL 16480669 in //UE5/Private-Frosty/...
#ROBOMERGE-BOT: STARSHIP (Private-Frosty -> Main) (v823-16466674)

[CL 16480676 by sebastien lussier in ue5-main branch]
2021-05-27 00:01:31 -04:00

91 lines
3.7 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Modules/ModuleInterface.h"
#include "PixelFormat.h"
#include "SceneTypes.h"
#include "IMaterialBakingModule.h"
#include "MaterialPropertyEx.h"
class UTextureRenderTarget2D;
class UMaterialOptions;
class UMaterialInterface;
class FExportMaterialProxy;
class FTextureRenderTargetResource;
struct FMaterialData;
struct FMeshData;
struct FBakeOutput;
class MATERIALBAKING_API FMaterialBakingModule : public IMaterialBakingModule
{
public:
/** IModuleInterface overrides begin */
virtual void StartupModule();
virtual void ShutdownModule();
/** IModuleInterface overrides end */
/** Bakes out material properties according to MaterialSettings using MeshSettings and stores the output in Output */
virtual void BakeMaterials(const TArray<FMaterialData*>& MaterialSettings, const TArray<FMeshData*>& MeshSettings, TArray<FBakeOutput>& Output) override;
/** Bakes out material properties according to extended MaterialSettings using MeshSettings and stores the output in Output */
virtual void BakeMaterials(const TArray<FMaterialDataEx*>& MaterialSettings, const TArray<FMeshData*>& MeshSettings, TArray<FBakeOutputEx>& Output) override;
/** Promps a slate window to allow the user to populate specific material baking settings used while baking out materials */
virtual bool SetupMaterialBakeSettings(TArray<TWeakObjectPtr<UObject>>& OptionObjects, int32 NumLODs) override;
/** Outputs true HDR version of emissive color */
virtual void SetEmissiveHDR(bool bHDR) override;
/** Bakes all material properties to linear textures, except for colors */
virtual void SetLinearBake(bool bCorrectLinear) override;
protected:
/* Creates and adds or reuses a RenderTarget from the pool */
UTextureRenderTarget2D* CreateRenderTarget(bool bInForceLinearGamma, EPixelFormat InPixelFormat, const FIntPoint& InTargetSize);
/* Creates and adds (or reuses a ExportMaterialProxy from the pool if MaterialBaking.UseMaterialProxyCaching is set to 1) */
FExportMaterialProxy* CreateMaterialProxy(UMaterialInterface* Material, const FMaterialPropertyEx& Property);
/** Helper for emissive color conversion to Output */
static void ProcessEmissiveOutput(const FFloat16Color* Color16, int32 Color16Pitch, const FIntPoint& OutputSize, TArray<FColor>& Output, float& EmissiveScale);
/** Cleans up all cached material proxies in MaterialProxyPool */
void CleanupMaterialProxies();
/** Callback for modified objects which should be removed from MaterialProxyPool in that case */
void OnObjectModified(UObject* Object);
/** Callback used to clear material proxy cache on garbage collection */
void OnPreGarbageCollect();
private:
enum EPropertyColorSpace
{
Linear,
sRGB,
};
/** Pool of available render targets, cached for re-using on consecutive property rendering */
TArray<UTextureRenderTarget2D*> RenderTargetPool;
/** Pool of cached material proxies to optimize material baking workflow, stays resident when MaterialBaking.UseMaterialProxyCaching is set to 1 */
typedef TWeakObjectPtr<UMaterialInterface> FMaterialPoolKey;
typedef TPair<FMaterialPropertyEx, FExportMaterialProxy*> FMaterialPoolValue;
typedef TMultiMap<FMaterialPoolKey, FMaterialPoolValue, FDefaultSetAllocator, TWeakObjectPtrMapKeyFuncs<FMaterialPoolKey, FMaterialPoolValue, true /*bInAllowDuplicateKeys*/>> FMaterialPoolMap;
FMaterialPoolMap MaterialProxyPool;
/** Pixel formats to use for baking out specific material properties */
TMap<FMaterialPropertyEx, EPixelFormat> PerPropertyFormat;
/** Whether or not to enforce gamma correction while baking out specific material properties */
TMap<FMaterialPropertyEx, EPropertyColorSpace> PerPropertyColorSpace;
EPropertyColorSpace DefaultColorSpace;
bool bEmissiveHDR;
};