剖析虚幻渲染体系(09)- 材质体系

本文深入剖析虚幻引擎的材质基础,重点介绍UMaterial,它是材质编辑器中创建的uasset资源,影响网格在场景中的视觉效果。UMaterial继承自UMaterialInterface,包含SubsurfaceProfile、AssetUserData、LightmassSettings等关键数据。此外,文章还提及UMaterialInstance等相关概念。
摘要由CSDN通过智能技术生成

9.2 材质基础

本章将分析材质涉及的基础概念和类型,阐述它们之间的基本关系和使用方法。

9.2.1 UMaterial

UMaterial是属于引擎层的概念,对应着我们在材质编辑器编辑的uasset资源文件,可以被应用到网格上,以便控制它在场景中的视觉效果。它继承自UMaterialInterface,它们的定义如下:

// Engine\Source\Runtime\Engine\Classes\Materials\MaterialInterface.h

// 材质的基础接口类, 定义了大量材质相关的数据和接口, 部分接口是空实现或未实现的接口.
class UMaterialInterface : public UObject, public IBlendableInterface, public IInterface_AssetUserData
{
    // 次表面散射轮廓(配置)
    class USubsurfaceProfile* SubsurfaceProfile;
    // 当图元不再被用作父元素时进行跟踪的栅栏.
    FRenderCommandFence ParentRefFence;

protected:
    // Lightmass离线的GI设置.
    struct FLightmassMaterialInterfaceSettings LightmassSettings;
    // 纹理流数据.
    TArray<FMaterialTextureInfo> TextureStreamingData;
    // 存于此材质资源内的用户数据列表.
    TArray<UAssetUserData*> AssetUserData;

private:
    // 强制编译的目标Feature Level.
    uint32 FeatureLevelsToForceCompile;

public:
    //---- IInterface_AssetUserData接口 ----
    virtual void AddAssetUserData(UAssetUserData* InUserData) override;
    virtual void RemoveUserDataOfClass(TSubclassOf<UAssetUserData> InUserDataClass) override;
    virtual UAssetUserData* GetAssetUserDataOfClass(TSubclassOf<UAssetUserData> InUserDataClass) override;
    //---- IInterface_AssetUserData接口 ----

    // 为所有材质编译的Featurelevel位域.
    static uint32 FeatureLevelsForAllMaterials;
    void SetFeatureLevelToCompile(ERHIFeatureLevel::Type FeatureLevel, bool bShouldCompile);
    static void SetGlobalRequiredFeatureLevel(ERHIFeatureLevel::Type FeatureLevel, bool bShouldCompile);

    //---- UObject接口 ----
    virtual void BeginDestroy() override;
    virtual bool IsReadyForFinishDestroy() override;
    virtual void PostLoad() override;
    virtual void PostDuplicate(bool bDuplicateForPIE) override;
    virtual void PostCDOContruct() override;
    //---- UObject接口 ----

    //---- IBlendableInterface接口 ----
    virtual void OverrideBlendableSettings(class FSceneView& View, float Weight) const override;
    //---- IBlendableInterface接口 ----

    // 沿着父链查找这个实例所在的基础材质.
    UMaterial* GetBaseMaterial();
    // 获取正在实例化的材质
    virtual class UMaterial* GetMaterial() PURE_VIRTUAL(UMaterialInterface::GetMaterial,return NULL;);
    virtual const class UMaterial* GetMaterial() const PURE_VIRTUAL(UMaterialInterface::GetMaterial,return NULL;);
    // 获取正在实例化的材质(并发模式)
    virtual const class UMaterial* GetMaterial_Concurrent(TMicRecursionGuard RecursionGuard = TMicRecursionGuard()) const PURE_VIRTUAL(UMaterialInterface::GetMaterial_Concurrent,return NULL;);

    // 测试该材质是否依赖指定的材料.
    virtual bool IsDependent(UMaterialInterface* TestDependency);
    virtual bool IsDependent_Concurrent(UMaterialInterface* TestDependency, TMicRecursionGuard RecursionGuard = TMicRecursionGuard());

    // 获取此材质对应的用于渲染的FMaterialRenderProxy实例.
    virtual class FMaterialRenderProxy* GetRenderProxy() const PURE_VIRTUAL(UMaterialInterface::GetRenderProxy,return NULL;);
    
    (......)

    // 获取用于渲染此材质的纹理列表.
    virtual void GetUsedTextures(TArray<UTexture*>& OutTextures, EMaterialQualityLevel::Type QualityLevel, bool bAllQualityLevels, ERHIFeatureLevel::Type FeatureLevel, bool bAllFeatureLevels) const
        PURE_VIRTUAL(UMaterialInterface::GetUsedTextures,);
    // 获取用于渲染此材质的纹理列表和索引.
    virtual void GetUsedTexturesAndIndices(TArray<UTexture*>& OutTextures, TArray< TArray<int32> >& OutIndices, EMaterialQualityLevel::Type QualityLevel, ERHIFeatureLevel::Type FeatureLevel) const;
    // 覆盖指定的纹理.
    virtual void OverrideTexture(const UTexture* InTextureToOverride, UTexture* OverrideTexture, ERHIFeatureLevel::Type InFeatureLevel) PURE_VIRTUAL(UMaterialInterface::OverrideTexture, return;);

    // 覆盖给定参数的默认值(短暂的)  
    virtual void OverrideVectorParameterDefault(const FHashedMaterialParameterInfo& ParameterInfo, const FLinearColor& Value, bool bOverride, ERHIFeatureLevel::Type FeatureLevel) PURE_VIRTUAL(UMaterialInterface::OverrideTexture, return;);

    // 检测指定的材质标记, 如果存在就返回true.
    virtual bool CheckMaterialUsage(const EMaterialUsage Usage) PURE_VIRTUAL(UMaterialInterface::CheckMaterialUsage,return false;);
    virtual bool CheckMaterialUsage_Concurrent(const EMaterialUsage Usage) const PURE_VIRTUAL(UMaterialInterface::CheckMaterialUsage,return false;);

    // 获取材质的渲染纹理, 需要指定FeatureLevel/QualityLevel.
    virtual FMaterialResource* GetMaterialResource(ERHIFeatureLevel::Type InFeatureLevel, EMaterialQualityLevel::Type QualityLevel = EMaterialQualityLevel::Num);

    // 获取组排序优先级.
    virtual bool GetGroupSortPriority(const FString& InGroupName, int32& OutSortPriority) const
        PURE_VIRTUAL(UMaterialInterface::GetGroupSortPriority, return false;);

    // 获取材质的各种类型的所有数据.
    virtual void GetAllScalarParameterInfo(TArray<FMaterialParameterInfo>& OutParameterInfo, TArray<FGuid>& OutParameterIds) const
        PURE_VIRTUAL(UMaterialInterface::GetAllScalarParameterInfo,return;);
    virtual void GetAllVectorParameterInfo(TArray<FMaterialParameterInfo>& OutParameterInfo, TArray<FGuid>& OutParameterIds) const
        PURE_VIRTUAL(UMaterialInterface::GetAllVectorParameterInfo,return;);
    virtual void GetAllTextureParameterInfo(TArray<FMaterialParameterInfo>& OutParameterInfo, TArray<FGuid>& OutParameterIds) const
        PURE_VIRTUAL(UMaterialInterface::GetAllTextureParameterInfo,return;);
    virtual void GetAllRuntimeVirtualTextureParameterInfo(TArray<FMaterialParameterInfo>& OutParameterInfo, TArray<FGuid>& OutParameterIds) const
        PURE_VIRTUAL(UMaterialInterface::GetAllRuntimeVirtualTextureParameterInfo, return;);
    virtual void GetAllFontParameterInfo(TArray<FMaterialParameterInfo>& OutParameterInfo, TArray<FGuid>& OutParameterIds) const
        PURE_VIRTUAL(UMaterialInterface::GetAllFontParameterInfo,return;);
    
    // 获取材质的各种类型的数据.
    virtual bool GetScalarParameterDefaultValue(const FHashedMaterialParameterInfo& ParameterInfo, float& OutValue, bool bOveriddenOnly = false, bool bCheckOwnedGlobalOverrides = false) const
        PURE_VIRTUAL(UMaterialInterface::GetScalarParameterDefaultValue,return false;);
    virtual bool GetVectorParameterDefaultValue(const FHashedMaterialParameterInfo& ParameterInfo, FLinearColor& OutValue, bool bOveriddenOnly = false, bool bCheckOwnedGlobalOverrides = false) const
        PURE_VIRTUAL(UMaterialInterface::GetVectorParameterDefaultValue,return false;);
    virtual bool GetTextureParameterDefaultValue(const FHashedMaterialParameterInfo& ParameterInfo, class UTexture*& OutValue, bool bCheckOwnedGlobalOverrides = false) const
        PURE_VIRTUAL(UMaterialInterface::GetTextureParameterDefaultValue,return false;);
    virtual bool GetRuntimeVirtualTextureParameterDefaultValue(const FHashedMaterialParameterInfo& ParameterInfo, class URuntimeVirtualTexture*& OutValue, bool bCheckOwnedGlobalOverrides = false) const
        PURE_VIRTUAL(UMaterialInterface::GetRuntimeVirtualTextureParameterDefaultValue, return false;);
    virtual bool GetFontParameterDefaultValue(const FHashedMaterialParameterInfo& ParameterInfo, class UFont*& OutFontValue, int32& OutFontPage, bool bCheckOwnedGlobalOverrides = false) const
        PURE_VIRTUAL(UMaterialInterface::GetFontParameterDefaultValue,return false;);

    // 获取分层数据.
    virtual int32 GetLayerParameterIndex(EMaterialParameterAssociation Association, UMaterialFunctionInterface * LayerFunction) const
        PURE_VIRTUAL(UMaterialInterface::GetLayerParameterIndex, return INDEX_NONE;);

    // 获取由表达式引用的纹理,包括嵌套函数.
    virtual TArrayView<UObject* const> GetReferencedTextures() const
        PURE_VIRTUAL(UMaterialInterface::GetReferencedTextures,return TArrayView<UObject* const>(););

    // 保存shader稳定的键值.
    virtual void SaveShaderStableKeysInner(const class ITargetPlatform* TP, const struct FStableShaderKeyAndValue& SaveKeyVal)
        PURE_VIRTUAL(UMaterialInterface::SaveShaderStableKeysInner, );

    // 获取材质参数信息.
    FMaterialParameterInfo GetParameterInfo(EMaterialParameterAssociation Association, FName ParameterName, UMaterialFunctionInterface* LayerFunction) const;
    // 获取材质关联标记.
    ENGINE_API FMaterialRelevance GetRelevance(ERHIFeatureLevel::Type InFeatureLevel) const;
    ENGINE_API FMaterialRelevance GetRelevance_Concurrent(ERHIFeatureLevel::Type InFeatureLevel) const;

#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
    // 记录材质和纹理.
    ENGINE_API virtual void LogMaterialsAndTextures(FOutputDevice& Ar, int32 Indent) const {}
#endif

private:
    int32 GetWidth() const;
    int32 GetHeight() const;

    // Lightmass接口.
    const FGuid& GetLightingGuid() const;
    void SetLightingGuid();
    virtual void GetLightingGuidChain(bool bIncludeTextures, TArray<FGuid>& OutGuids) const;
    virtual bool UpdateLightmassTextureTracking();
    inline bool GetOverrideCastShadowAsMasked() const;
    inline bool GetOverrideEmissiveBoost() const;
    (......)

    // 数据获取接口.
    virtual bool GetScalarParameterValue(const FHashedMaterialParameterInfo& ParameterInfo, float& OutValue, bool bOveriddenOnly = false) const;
    virtual bool GetScalarCurveParameterValue(const FHashedMaterialParameterInfo& ParameterInfo, FInterpCurveFloat& OutValue) const;
    virtual bool GetVectorParameterValue(const FHashedMaterialParameterInfo& ParameterInfo, FLinearColor& OutValue, bool bOveriddenOnly = false) const;
    virtual bool GetVectorCurveParameterValue(const FHashedMaterialParameterInfo& ParameterInfo, FInterpCurveVector& OutValue) const;
    virtual bool GetLinearColorParameterValue(const FHashedMaterialParameterInfo& ParameterInfo, FLinearColor& OutValue) const;
    virtual bool GetLinearColorCurveParameterValue(const FHashedMaterialParameterInfo& ParameterInfo, FInterpCurveLinearColor& OutValue) const;
    virtual bool GetTextureParameterValue(const FHashedMaterialParameterInfo& ParameterInfo, class UTexture*& OutValue, bool bOveriddenOnly = false) const;
    virtual bool GetRuntimeVirtualTextureParameterValue(const FHashedMaterialParameterInfo& ParameterInfo, class URuntimeVirtualTexture*& OutValue, bool bOveriddenOnly = false) const;
    virtual bool GetFontParameterValue(const FHashedMaterialParameterInfo& ParameterInfo,class UFont*& OutFontValue, int32& OutFontPage, bool bOveriddenOnly = false) const;
    virtual bool GetRefractionSettings(float& OutBiasValue) const;

    // 访问基础材质的覆盖属性.
    virtual float GetOpacityMaskClipValue() const;
    virtual bool GetCastDynamicShadowAsMasked() const;
    virtual EBlendMode GetBlendMode() const;
    virtual FMaterialShadingModelField GetShadingModels() const;
    virtual bool IsShadingModelFromMaterialExpression() const;
    virtual bool IsTwoSided() const;
    virtual bool IsDitheredLODTransition() const;
    virtual bool IsTranslucencyWritingCustomDepth() const;
    virtual bool IsTranslucencyWritingVelocity() const;
    virtual bool IsMasked() const;
    virtual bool IsDeferredDecal() const;
    virtual USubsurfaceProfile* GetSubsurfaceProfile_Internal() const;
    virtual bool CastsRayTracedShadows() const;

    // 强制流系统忽略指定持续时间的正常逻辑,而总是加载此材质使用的所有纹理的所有mip级别.
    virtual void SetForceMipLevelsToBeResident( bool OverrideForceMiplevelsToBeResident, bool bForceMiplevelsToBeResidentValue, float ForceDuration, int32 CinematicTextureGroups = 0, bool bFastResponse = false );

    // 重新缓存所有材材质接口的统一表达式.
    static void RecacheAllMaterialUniformExpressions(bool bRecreateUniformBuffer);
    virtual void RecacheUniformExpressions(bool bRecreateUniformBuffer) const;
    // 初始化所有的系统默认材质.
    static void InitDefaultMaterials();
    virtual bool IsPropertyActive(EMaterialProperty InProperty) const;
    static uint32 GetFeatureLevelsToCompileForAllMaterials()

    // 返回使用纹理坐标的数量,以及顶点数据是否在着色器图中使用.
    void AnalyzeMaterialProperty(EMaterialProperty InProperty, int32& OutNumTextureCoordinates, bool& bOutRequiresVertexData);

    // 遍历所有的FeatureLevel, 可以指定回调.
    template <typename FunctionType>
    static void IterateOverActiveFeatureLevels(FunctionType InHandler);

    // 访问材质采样器类型的缓存的UEnum类型信息.
    static UEnum* GetSamplerTypeEnum();

    bool UseAnyStreamingTexture() const;
    bool HasTextureStreamingData() const;
    const TArray<FMaterialTextureInfo>& GetTextureStreamingData() const;
    FORCEINLINE TArray<FMaterialTextureInfo>& GetTextureStreamingData();
    // 纹理流接口.
    bool FindTextureStreamingDataIndexRange(FName TextureName, int32& LowerIndex, int32& HigherIndex) const;
    void SetTextureStreamingData(const TArray<FMaterialTextureInfo>& InTextureStreamingData);
    // 返回纹理的比例(LocalSpace Unit / Texture), 用于纹理流矩阵.
    virtual float GetTextureDensity(FName TextureName, const struct FMeshUVChannelInfo& UVChannelData) const;
    // 预保存.
    virtual void PreSave(const class ITargetPlatform* TargetPlatform) override;
    // 按名称对纹理流数据进行排序,以加速搜索. 只在需要时排序.
    void SortTextureStreamingData(bool bForceSort, bool bFinalSort);

protected:
    uint32 GetFeatureLevelsToCompileForRendering() const;
    void UpdateMaterialRenderProxy(FMaterialRenderProxy& Proxy);

private:
    static void PostLoadDefaultMaterials();
    // 材质采样器类型的缓存的UEnum类型信息.
    static UEnum* SamplerTypeEnum;
};


// Engine\Source\Runtime\Engine\Classes\Materials\Material.h

// 材质类, 对应着一个材质资源文件.
class UMaterial : public UMaterialInterface
{
    // 物理材质.
    class UPhysicalMaterial* PhysMaterial;
    class UPhysicalMaterialMask* PhysMaterialMask;
    class UPhysicalMaterial* PhysicalMaterialMap[EPhysicalMaterialMaskColor::MAX];

    // 材质属性.
    FScalarMaterialInput Metallic;
    FScalarMaterialInput Specular;
    FScalarMaterialInput Anisotropy;
    FVectorMaterialInput Normal;
    FVectorMaterialInput Tangent;
    FColorMaterialInput EmissiveColor;

#if WITH_EDITORONLY_DATA
    // 透射.
    FScalarMaterialInput Opacity;
    FScalarMaterialInput OpacityMask;
#endif
    
    TEnumAsByte<enum EMaterialDomain> MaterialDomain;
    TEnumAsByte<enum EBlendMode> BlendMode;
    TEnumAsByte<enum EDecalBlendMode> DecalBlendMode;
    TEnumAsByte<enum EMaterialDecalResponse> MaterialDecalResponse;
    TEnumAsByte<enum EMaterialShadingModel> ShadingModel; 
    UPROPERTY(AssetRegistrySearchable)
    FMaterialShadingModelField ShadingModels;

public:
    // 材质属性.
    float OpacityMaskClipValue;
    FVectorMaterialInput WorldPositionOffset;
    FScalarMaterialInput Refraction;
    FMaterialAttributesInput MaterialAttributes;
    FScalarMaterialInput PixelDepthOffset;
    FShadingModelMaterialInput ShadingModelFromMaterialExpression;
    
#if WITH_EDITORONLY_DATA
    FVectorMaterialInput WorldDisplacement;
    FScalarMaterialInput TessellationMultiplier;
    FColorMaterialInput SubsurfaceColor;
    FScalarMaterialInput ClearCoat;
    FScalarMaterialInput ClearCoatRoughness;
    FScalarMaterialInput AmbientOcclusion;
    FVector2MaterialInput CustomizedUVs[8];
#endif
    int32 NumCustomizedUVs;

    // 材质标记.
    uint8 bCastDynamicShadowAsMasked : 1;
    uint8 bEnableSeparateTranslucency : 1;
    uint8 bEnableResponsiveAA : 1;
    uint8 bScreenSpaceReflections : 1;
    uint8 bContactShadows : 1;
    uint8 TwoSided : 1;
    uint8 DitheredLODTransition : 1;
    uint8 DitherOpacityMask : 1;
    uint8 bAllowNegativeEmissiveColor : 1;

    // 透明相关.
    TEnumAsByte<enum ETranslucencyLightingMode> TranslucencyLightingMode;
    uint8 bEnableMobileSeparateTranslucency : 1;
    float TranslucencyDirectionalLightingIntensity;
    float TranslucentShadowDensityScale;
    float TranslucentSelfShadowDensityScale;
    float TranslucentSelfShadowSecondDensityScale;
    float TranslucentSelfShadowSecondOpacity;
    float TranslucentBackscatteringExponent;
    FLinearColor TranslucentMultipleScatteringExtinction;
    float TranslucentShadowStartOffset;

    // 使用标记.
    uint8 bDisableDepthTest : 1;
    uint8 bWriteOnlyAlpha : 1;
    uint8 bGenerateSphericalParticleNormals : 1;
    uint8 bTangentSpaceNormal : 1;
    uint8 bUseEmissiveForDynamicAreaLighting : 1;
    uint8 bBlockGI : 1;
    uint8 bUsedAsSpecialEngineMaterial : 1;
    uint8 bUsedWithSkeletalMesh : 1;
    uint8 bUsedWithEditorCompositing : 1;
    uint8 bUsedWithParticleSprites : 1;
    uint8 bUsedWithBeamTrails : 1;
    uint8 bUsedWithMeshParticles : 1;
    uint8 bUsedWithNiagaraSprites : 1;
    uint8 bUsedWithNiagaraRibbons : 1;
    uint8 bUsedWithNiagaraMeshParticles : 1;
    uint8 bUsedWithGeometryCache : 1;
    uint8 bUsedWithStaticLighting : 1;
    uint8 bUsedWithMorphTargets : 1;
    uint8 bUsedWithSplineMeshes : 1;
    uint8 bUsedWithInstancedStaticMeshes : 1;
    uint8 bUsedWithGeometryCollections : 1;
    uint8 bUsesDistortion : 1;
    uint8 bUsedWithClothing : 1;
    uint32 bUsedWithWater : 1;
    uint32 bUsedWithHairStrands : 1;
    uint32 bUsedWithLidarPointCloud : 1;
    uint32 bUsedWithVirtualHeightfieldMesh : 1;
    uint8 bAutomaticallySetUsageInEditor : 1;
    uint8 bFullyRough : 1;
    uint8 bUseFullPrecision : 1;
    uint8 bUseLightmapDirectionality : 1;
    uint8 bUseAlphaToCoverage : 1;
    uint32 bForwardRenderUsePreintegratedGFForSimpleIBL : 1;
    uint8 bUseHQForwardReflections : 1;
    uint8 bUsePlanarForwardReflections : 1;

    // 根据屏幕空间法向变化降低粗糙度.
    uint8 bNormalCurvatureToRoughness : 1;
    uint8 AllowTranslucentCustomDepthWrites : 1;
    uint8 Wireframe : 1;
    // 着色频率.
    TEnumAsByte<EMaterialShadingRate> ShadingRate;
    uint8 bCanMaskedBeAssumedOpaque : 1;
    uint8 bIsPreviewMaterial : 1;
    uint8 bIsFunctionPreviewMaterial : 1;
    uint8 bUseMaterialAttributes : 1;
    uint8 bCastRayTracedShadows : 1;
    uint8 bUseTranslucencyVertexFog : 1;
    uint8 bApplyCloudFogging : 1;
    uint8 bIsSky : 1;
    uint8 bComputeFogPerPixel : 1;
    uint8 bOutputTranslucentVelocity : 1;
    uint8 bAllowDevelopmentShaderCompile : 1;
    uint8 bIsMaterialEditorStatsMaterial : 1;
    TEnumAsByte<enum EBlendableLocation> BlendableLocation;
    uint8 BlendableOutputAlpha : 1;
    uint8 bEnableStencilTest : 1;
    TEnumAsByte<EMaterialStencilCompare> StencilCompare;
    uint8 StencilRefValue = 0;
    TEnumAsByte<enum ERefractionMode> RefractionMode;
    int32 BlendablePriority;
    uint8 bIsBlendable : 1;
    uint32 UsageFlagWarnings;
    float RefractionDepthBias;
    FGuid StateId;
    float MaxDisplacement;

    // 当渲染器需要获取参数值时,代表这个材质到渲染器的FMaterialRenderProxy衍生物.
    class FDefaultMaterialInstance* DefaultMaterialInstance;

#if WITH_EDITORONLY_DATA
    // 编辑器参数.
    TMap<FName, TArray<UMaterialExpression*> > EditorParameters;
    // 材质图. 编辑器模型下的数据.
    class UMaterialGraph*    MaterialGraph;
#endif

private:
    // 从地盘序列化而来的内联材质资源. 由游戏线程的PostLoad处理.
    TArray<FMaterialResource> LoadedMaterialResources;
    // 用于渲染材质的资源列表.
    TArray<FMaterialResource*> MaterialResources;
#if WITH_EDITOR
    // 正在缓存或烘焙的材质资源.
    TMap<const class ITargetPlatform*, TArray<FMaterialResource*>> CachedMaterialResourcesForCooking;
#endif
    
    // 用于保证在清理之前使用此UMaterial中的各种资源完成RT的标志.
    FThreadSafeBool ReleasedByRT;
    FMaterialCachedExpressionData CachedExpressionData;
    
public:
    //~ Begin UMaterialInterface Interface.
    virtual UMaterial* GetMaterial() override;
    virtual const UMaterial* GetMaterial() const override;
    virtual const UMaterial* GetMaterial_Concurrent(TMicRecursionGuard RecursionGuard = TMicRecursionGuard()) const override;
    virtual bool GetScalarParameterValue(...) const override;
    (......)
    void SetShadingModel(EMaterialShadingModel NewModel);
    virtual bool IsPropertyActive(EMaterialProperty InProperty) const override;
    //~ End UMaterialInterface Interface.

    //~ Begin UObject Interface
    virtual void PreSave(const class ITargetPlatform* TargetPlatform) override;
    virtual void PostInitProperties() override;
    virtual void Serialize(FArchive& Ar) override;
    virtual void PostDuplicate(bool bDuplicateForPIE) override;
    virtual void PostLoad() override;
    virtual void BeginDestroy() override;
    virtual bool IsReadyForFinishDestroy() override;
    virtual void FinishDestroy() override;
    virtual void GetResourceSizeEx(FResourceSizeEx& CumulativeResourceSize) override;
    static void AddReferencedObjects(UObject* InThis, FReferenceCollector& Collector);
    virtual bool CanBeClusterRoot() const override;
    virtual void GetAssetRegistryTags(TArray<FAssetRegistryTag>& OutTags) const override;
    //~ End UObject Interface

    // 数据获取接口.
    bool IsDefaultMaterial() const;
    void ReleaseResources();
    bool IsUsageFlagDirty(EMaterialUsage Usage);
    bool IsCompilingOrHadCompileError(ERHIFeatureLevel::Type InFeatureLevel);
    
    (......)

private:
    // 新版的获取材质数据接口.
    bool GetScalarParameterValue_New(...) const;
    bool GetVectorParameterValue_New(...) const;
    bool GetTextureParameterValue_New(...) const;
    bool GetRuntimeVirtualTextureParameterValue_New(...) const;
    bool GetFontParameterValue_New(...) const;
    
    FString GetUsageName(const EMaterialUsage Usage) const;
    bool GetUsageByFlag(const EMaterialUsage Usage) const;
    bool SetMaterialUsage(bool &bNeedsRecompile, const EMaterialUsage Usage);
    
    (......)

private:
    virtual void FlushResourceShaderMaps();
    // 缓冲资源或数据.
    void CacheResourceShadersForRendering(bool bRegenerateId);
    void CacheResourceShadersForCooking(...);
    void CacheShadersForResources(...);

public:
    // 静态工具箱或操作接口.
    static UMaterial* GetDefaultMaterial(EMaterialDomain Domain);
    static void UpdateMaterialShaders(...);
    static void BackupMaterialShadersToMemory(...);
    static void RestoreMaterialShadersFromMemory(...);
    static void CompileMaterialsForRemoteRecompile(...);
    static bool GetExpressionParameterName(const UMaterialExpression* Expression, FName& OutName);
    static bool CopyExpressionParameters(UMaterialExpression* Source, UMaterialExpression* Destination);
    static bool IsParameter(const UMaterialExpression* Expression);
    static bool IsDynamicParameter(const UMaterialExpression* Expression);
    static const TCHAR* GetMaterialShadingModelString(EMaterialShadingModel InMaterialShadingModel);
    static EMaterialShadingModel GetMaterialShadingModelFromString(const TCHAR* InMaterialShadingModelStr);
    static const TCHAR* GetBlendModeString(EBlendMode InBlendMode);
    static EBlendMode GetBlendModeFromString(const TCHAR* InBlendModeStr);
    virtual TArrayView<UObject* const> GetReferencedTextures() const override final;
    
    (......)
};

UMaterialInterface是个基础的抽象类,定义了一组通用的材质属性和接口。UMaterialInterface声明的主要数据有:

  • USubsurfaceProfile* SubsurfaceProfile:次表面散射轮廓,常用于皮肤、玉石等材质。
  • TArray<UAssetUserData*> AssetUserData:用户数据,可以存多个数据。
  • FLightmassMaterialInterfaceSettings LightmassSettings:离线GI数据。
  • TArray TextureStreamingData:纹理流数据。

UMaterialInterface的子类不止UMaterial,还有UMaterialInstance(后面又解析)。

https://www.cnblogs.com/scrm/p/15079994.html

https://www.cnblogs.com/scrm/p/15094896.html
https://weibo.com/ttarticle/p/show?id=2309404666137043665024
https://tieba.baidu.com/p/7476873168
https://www.meipian.cn/3qilsj32?share_depth=1
https://xueqiu.com/4824316523/193026747
https://blog.csdn.net/yunkeyi/article/details/119351829
https://www.im286.net/thread-24272505-1.html
https://blog.51cto.com/weixincrm/3257925
https://itbbs.pconline.com.cn/soft/54653054.html
https://www.acfun.cn/a/ac30503510

https://www.cnblogs.com/scrm/p/15107953.html
https://www.cnblogs.com/scrm/p/15108531.html
 

UMaterial定义了材质所需的所有数据和操作接口,并负责打通其它关联类型的链接。它的核心数据有:

  • TArray<FMaterialResource*> MaterialResources:材质渲染资源,一个材质可以拥有多个渲染资源实例。
  • FDefaultMaterialInstance* DefaultMaterialInstance:默认的材质渲染代理,继承自FMaterialRenderProxy。
  • UPhysicalMaterial* PhysMaterial:物理材质。
  • TArray LoadedMaterialResources:已经加载的材质资源,通常由游戏线程从磁盘加载并序列化而来。
  • 材质的各种属性和标记。

其中FMaterialResource是渲染材质的资源,属于渲染层的类型和数据,后面会有章节解析。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值