我是靠谱客的博主 执着大象,最近开发中收集的这篇文章主要介绍Ue4 Shader 使用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

(1)

    MSM_ThinTranslucent            UMETA(DisplayName="Thin Translucent"),
    // TODO:My
    MSM_MyShaderAa    UMETA(DisplayName = "My Shader Aa"),
    MSM_MyShaderAb    UMETA(DisplayName = "My Shader Ab"),

(1)

        case MSM_ThinTranslucent:    ShadingModelName = TEXT("MSM_ThinTranslucent"); break;
        // TODO:My
        case MSM_MyShaderAa:    ShadingModelName = TEXT("MSM_MyShaderAa"); break;
        case MSM_MyShaderAb:    ShadingModelName = TEXT("MSM_MyShaderAb"); break;

(1)

        const bool bSingleLayerWaterUsesSimpleShading = (IsSwitchPlatform(InPlatform) || IsVulkanMobileSM5Platform(InPlatform)) && IsForwardShadingEnabled(InPlatform);
        if (ShadingModels.HasShadingModel(MSM_SingleLayerWater) && bSingleLayerWaterUsesSimpleShading)
        {
            OutEnvironment.SetDefine(TEXT("SINGLE_LAYER_WATER_SIMPLE_FORWARD"), TEXT("1"));
        }
        // todo:My
        if (ShadingModels.HasShadingModel(MSM_MyShaderAa))
        {
            OutEnvironment.SetDefine(TEXT("MATERIAL_SHADINGMODEL_MYSHADERAA"), TEXT("1"));
            NumSetMaterials++;
        }
        if (ShadingModels.HasShadingModel(MSM_MyShaderAb))
        {
            OutEnvironment.SetDefine(TEXT("MATERIAL_SHADINGMODEL_MYSHADERAB"), TEXT("1"));
            NumSetMaterials++;
        }

(1)

(1)

    case MP_Metallic:
        CustomPinNames.Add({MSM_Hair, "Scatter"});
        CustomPinNames.Add({ MSM_MyShaderAa, "MetallicMyShaderAa" });
        CustomPinNames.Add({ MSM_MyShaderAb, "MetallicMyShaderAb" });
        return FText::FromString(GetPinNameFromShadingModelField(Material->GetShadingModels(), CustomPinNames, "Metallic"));

(1)

    case MP_Specular:
        return Material->GetShadingModels() == MSM_MyShaderAa ? LOCTEXT("SpecularMyShaderAa", "SpecularMyShaderAa") : LOCTEXT("Specular", "Specular");
    case MP_Roughness:
        return Material->GetShadingModels() == MSM_MyShaderAa ? LOCTEXT("RoughnessMyShaderAa", "RoughnessMyShaderAa") : LOCTEXT("Roughness", "Roughness");

(1)

    case MP_SubsurfaceColor:
        CustomPinNames.Add({MSM_Cloth, "Fuzz Color"});
        CustomPinNames.Add({ MSM_MyShaderAa, "SubsurfaceColorMyShaderAa" });
        CustomPinNames.Add({ MSM_MyShaderAb, "SubsurfaceColorMyShaderAb" });
        return FText::FromString(GetPinNameFromShadingModelField(Material->GetShadingModels(), CustomPinNames, "Subsurface Color"));
    case MP_CustomData0:    
        CustomPinNames.Add({ MSM_ClearCoat, "Clear Coat" });
        CustomPinNames.Add({MSM_Hair, "Backlit"});
        CustomPinNames.Add({MSM_Cloth, "Cloth"});
        CustomPinNames.Add({MSM_Eye, "Iris Mask"});
        CustomPinNames.Add({ MSM_MyShaderAa, "CustomData0MyShaderAa" });
        CustomPinNames.Add({ MSM_MyShaderAb, "CustomData0MyShaderAa" });
        return FText::FromString(GetPinNameFromShadingModelField(Material->GetShadingModels(), CustomPinNames, "Custom Data 0"));

(1)

修改前

#define WRITES_CUSTOMDATA_TO_GBUFFER        (USES_GBUFFER && (MATERIAL_SHADINGMODEL_SUBSURFACE || MATERIAL_SHADINGMODEL_PREINTEGRATED_SKIN || MATERIAL_SHADINGMODEL_SUBSURFACE_PROFILE || MATERIAL_SHADINGMODEL_CLEAR_COAT || MATERIAL_SHADINGMODEL_TWOSIDED_FOLIAGE || MATERIAL_SHADINGMODEL_HAIR || MATERIAL_SHADINGMODEL_CLOTH || MATERIAL_SHADINGMODEL_EYE))

修改后

#define WRITES_CUSTOMDATA_TO_GBUFFER        (USES_GBUFFER && (MATERIAL_SHADINGMODEL_SUBSURFACE || MATERIAL_SHADINGMODEL_PREINTEGRATED_SKIN || MATERIAL_SHADINGMODEL_SUBSURFACE_PROFILE || MATERIAL_SHADINGMODEL_CLEAR_COAT || MATERIAL_SHADINGMODEL_TWOSIDED_FOLIAGE || MATERIAL_SHADINGMODEL_HAIR || MATERIAL_SHADINGMODEL_CLOTH || MATERIAL_SHADINGMODEL_EYE || MATERIAL_SHADINGMODEL_MYSHADERAA || MATERIAL_SHADINGMODEL_MYSHADERAB))

(1)

修改前

#if MATERIAL_SHADINGMODEL_CLOTH
        else if (ShadingModel == SHADINGMODELID_CLOTH)
        {
            SubsurfaceColor = SubsurfaceData.rgb;
        }
#endif

修改后

#if MATERIAL_SHADINGMODEL_CLOTH || MATERIAL_SHADINGMODEL_MYSHADERAA || MATERIAL_SHADINGMODEL_MYSHADERAB
        else if (ShadingModel == SHADINGMODELID_CLOTH || ShadingModel == SHADINGMODELID_MYSHADERAA || ShadingModel == SHADINGMODELID_MYSHADERAB)
        {
            SubsurfaceColor = SubsurfaceData.rgb;
        }
#endif

 

修正修改

修改前

#if MATERIAL_SHADINGMODEL_SUBSURFACE || MATERIAL_SHADINGMODEL_PREINTEGRATED_SKIN || MATERIAL_SHADINGMODEL_SUBSURFACE_PROFILE || MATERIAL_SHADINGMODEL_TWOSIDED_FOLIAGE || MATERIAL_SHADINGMODEL_CLOTH || MATERIAL_SHADINGMODEL_EYE
    if (ShadingModel == SHADINGMODELID_SUBSURFACE || ShadingModel == SHADINGMODELID_PREINTEGRATED_SKIN || ShadingModel == SHADINGMODELID_SUBSURFACE_PROFILE || ShadingModel == SHADINGMODELID_TWOSIDED_FOLIAGE || ShadingModel == SHADINGMODELID_CLOTH || ShadingModel == SHADINGMODELID_EYE)
    {
        float4 SubsurfaceData = GetMaterialSubsurfaceData(PixelMaterialInputs);

        if (false) // Dummy if to make the ifdef logic play nicely
        {
        }
#if MATERIAL_SHADINGMODEL_SUBSURFACE || MATERIAL_SHADINGMODEL_PREINTEGRATED_SKIN || MATERIAL_SHADINGMODEL_TWOSIDED_FOLIAGE
        else if (ShadingModel == SHADINGMODELID_SUBSURFACE || ShadingModel == SHADINGMODELID_PREINTEGRATED_SKIN || ShadingModel == SHADINGMODELID_TWOSIDED_FOLIAGE)
        {
            SubsurfaceColor = SubsurfaceData.rgb * View.DiffuseOverrideParameter.w + View.DiffuseOverrideParameter.xyz;
        }
#endif
#if MATERIAL_SHADINGMODEL_CLOTH
        else if (ShadingModel == SHADINGMODELID_CLOTH)
        {
            SubsurfaceColor = SubsurfaceData.rgb;
        }
#endif

        SubsurfaceProfile = SubsurfaceData.a;
    }
#endif

修改后

#if MATERIAL_SHADINGMODEL_SUBSURFACE || MATERIAL_SHADINGMODEL_PREINTEGRATED_SKIN || MATERIAL_SHADINGMODEL_SUBSURFACE_PROFILE || MATERIAL_SHADINGMODEL_TWOSIDED_FOLIAGE || MATERIAL_SHADINGMODEL_CLOTH || MATERIAL_SHADINGMODEL_EYE || MATERIAL_SHADINGMODEL_MYSHADERAA || MATERIAL_SHADINGMODEL_MYSHADERAB

    if (ShadingModel == SHADINGMODELID_SUBSURFACE || ShadingModel == SHADINGMODELID_PREINTEGRATED_SKIN || ShadingModel == SHADINGMODELID_SUBSURFACE_PROFILE || ShadingModel == SHADINGMODELID_TWOSIDED_FOLIAGE || ShadingModel == SHADINGMODELID_CLOTH || ShadingModel == SHADINGMODELID_EYE || ShadingModel == SHADINGMODELID_MYSHADERAA || ShadingModel == SHADINGMODELID_MYSHADERAB)

    {

        float4 SubsurfaceData = GetMaterialSubsurfaceData(PixelMaterialInputs);

 

        if (false) // Dummy if to make the ifdef logic play nicely

        {

        }

#if MATERIAL_SHADINGMODEL_SUBSURFACE || MATERIAL_SHADINGMODEL_PREINTEGRATED_SKIN || MATERIAL_SHADINGMODEL_TWOSIDED_FOLIAGE

        else if (ShadingModel == SHADINGMODELID_SUBSURFACE || ShadingModel == SHADINGMODELID_PREINTEGRATED_SKIN || ShadingModel == SHADINGMODELID_TWOSIDED_FOLIAGE)

        {

            SubsurfaceColor = SubsurfaceData.rgb * View.DiffuseOverrideParameter.w + View.DiffuseOverrideParameter.xyz;

        }

#endif

#if MATERIAL_SHADINGMODEL_CLOTH || MATERIAL_SHADINGMODEL_MYSHADERAA || MATERIAL_SHADINGMODEL_MYSHADERAB

        else if (ShadingModel == SHADINGMODELID_CLOTH || ShadingModel == SHADINGMODELID_MYSHADERAA || ShadingModel == SHADINGMODELID_MYSHADERAB)

        {

            SubsurfaceColor = SubsurfaceData.rgb;

        }

#endif

 

        SubsurfaceProfile = SubsurfaceData.a;

    }

#endif

 

(1)

修改前

    // So that the following code can still use DiffuseColor and SpecularColor.
    GBuffer.SpecularColor = ComputeF0(Specular, BaseColor, Metallic);

修改后

    // So that the following code can still use DiffuseColor and SpecularColor.
    GBuffer.SpecularColor = ComputeF0(Specular, BaseColor, Metallic);

#if MATERIAL_SHADINGMODEL_MYSHADERAA || MATERIAL_SHADINGMODEL_MYSHADERAB
    if (GBuffer.ShadingModelID == SHADINGMODELID_MYSHADERAA || GBuffer.ShadingModelID == SHADINGMODELID_MYSHADERAB)
    {
        GBuffer.SpecularColor = float3(0.0f, 0.0f, 0.0f);
    }    
#endif

(1)

修改前

GBuffer.DiffuseColor = BaseColor - BaseColor * Metallic;

修改后

GBuffer.DiffuseColor = BaseColor - BaseColor * Metallic;

#if MATERIAL_SHADINGMODEL_MYSHADERAA || MATERIAL_SHADINGMODEL_MYSHADERAB
    if (GBuffer.ShadingModelID == SHADINGMODELID_MYSHADERAA || GBuffer.ShadingModelID == SHADINGMODELID_MYSHADERAB)
    {
        GBuffer.DiffuseColor = float3(0.0f, 0.0f, 0.0f);
    }    
#endif

(1)

修改前

LightAccumulator.EstimatedCost += 0.3f;        // add the cost of getting the shadow terms

        BRANCH
        if( Shadow.SurfaceShadow + Shadow.TransmissionShadow > 0 )
        {
            const bool bNeedsSeparateSubsurfaceLightAccumulation = UseSubsurfaceProfile(GBuffer.ShadingModelID);
            float3 LightColor = LightData.Color;

        #if NON_DIRECTIONAL_DIRECT_LIGHTING
            float Lighting;

            if( LightData.bRectLight )
            {
                FRect Rect = GetRect( ToLight, LightData );

                Lighting = IntegrateLight( Rect, SourceTexture);
            }
            else
            {
                FCapsuleLight Capsule = GetCapsule( ToLight, LightData );

                Lighting = IntegrateLight( Capsule, LightData.bInverseSquared );
            }

            float3 LightingDiffuse = Diffuse_Lambert( GBuffer.DiffuseColor ) * Lighting;
            LightAccumulator_AddSplit(LightAccumulator, LightingDiffuse, 0.0f, 0, LightColor * LightMask * Shadow.SurfaceShadow, bNeedsSeparateSubsurfaceLightAccumulation);
        #else
            FDirectLighting Lighting;

            if (LightData.bRectLight)
            {
                FRect Rect = GetRect( ToLight, LightData );

                #if REFERENCE_QUALITY
                    Lighting = IntegrateBxDF( GBuffer, N, V, Rect, Shadow, SourceTexture, SVPos );
                #else
                    Lighting = IntegrateBxDF( GBuffer, N, V, Rect, Shadow, SourceTexture);
                #endif
            }
            else
            {
                FCapsuleLight Capsule = GetCapsule( ToLight, LightData );

                #if REFERENCE_QUALITY
                    Lighting = IntegrateBxDF( GBuffer, N, V, Capsule, Shadow, SVPos );
                #else
                    Lighting = IntegrateBxDF( GBuffer, N, V, Capsule, Shadow, LightData.bInverseSquared );
                #endif
            }

            Lighting.Specular *= LightData.SpecularScale;
                
            LightAccumulator_AddSplit( LightAccumulator, Lighting.Diffuse, Lighting.Specular, Lighting.Diffuse, LightColor * LightMask * Shadow.SurfaceShadow, bNeedsSeparateSubsurfaceLightAccumulation );
            LightAccumulator_AddSplit( LightAccumulator, Lighting.Transmission, 0.0f, Lighting.Transmission, LightColor * LightMask * Shadow.TransmissionShadow, bNeedsSeparateSubsurfaceLightAccumulation );

            LightAccumulator.EstimatedCost += 0.4f;        // add the cost of the lighting computations (should sum up to 1 form one light)
        #endif
        }
    }

修改后

        LightAccumulator.EstimatedCost += 0.3f;        // add the cost of getting the shadow terms

        BRANCH
        #if MATERIAL_SHADINGMODEL_MYSHADERAA || MATERIAL_SHADINGMODEL_MYSHADERAB
        if (GBuffer.ShadingModelID == SHADINGMODELID_MYSHADERAA || GBuffer.ShadingModelID == SHADINGMODELID_MYSHADERAB)
        {
            float3 LightColor = LightData.Color;

            float3 SSS = float3(GBuffer.CustomData.r, GBuffer.CustomData.g, GBuffer.CustomData.b);
            float SpecStrength = GBuffer.Metallic;
            float ShadowThreshold = GBuffer.Specular;
            float SpecularSize = GBuffer.Roughness;
            float InnerLine = GBuffer.CustomData.a;

            // 纹理数据处理
            float3 BrightColor = GBuffer.BaseColor;
            float3 ShadowColor = GBuffer.BaseColor * SSS;

            // 这里我觉得内部描边的颜色不够明显,于是乘上了0.5
            if (InnerLine < 0.8f)
            {
                InnerLine *= 0.5f;
            }
            float3 InnerLineColor = float3(InnerLine, InnerLine, InnerLine);

            float NdotL = dot(L, N) * Shadow.SurfaceShadow;
            float NdotH = dot(normalize(L + V), N);

            ShadowThreshold = step(ShadowThreshold, NdotL);

            float3 Diffuse = InnerLineColor * LightColor * lerp(ShadowColor, BrightColor, ShadowThreshold);
            float3 Specular = LightColor * BrightColor * ShadowThreshold * InnerLineColor * step(0.2f, SpecularSize * pow(NdotH, SpecStrength));

            LightAccumulator.TotalLight = Diffuse + Specular;
        }
        else
        {
        #endif
        if( Shadow.SurfaceShadow + Shadow.TransmissionShadow > 0 )
        {
            const bool bNeedsSeparateSubsurfaceLightAccumulation = UseSubsurfaceProfile(GBuffer.ShadingModelID);
            float3 LightColor = LightData.Color;

        #if NON_DIRECTIONAL_DIRECT_LIGHTING
            float Lighting;

            if( LightData.bRectLight )
            {
                FRect Rect = GetRect( ToLight, LightData );

                Lighting = IntegrateLight( Rect, SourceTexture);
            }
            else
            {
                FCapsuleLight Capsule = GetCapsule( ToLight, LightData );

                Lighting = IntegrateLight( Capsule, LightData.bInverseSquared );
            }

            float3 LightingDiffuse = Diffuse_Lambert( GBuffer.DiffuseColor ) * Lighting;
            LightAccumulator_AddSplit(LightAccumulator, LightingDiffuse, 0.0f, 0, LightColor * LightMask * Shadow.SurfaceShadow, bNeedsSeparateSubsurfaceLightAccumulation);
        #else
            FDirectLighting Lighting;

            if (LightData.bRectLight)
            {
                FRect Rect = GetRect( ToLight, LightData );

                #if REFERENCE_QUALITY
                    Lighting = IntegrateBxDF( GBuffer, N, V, Rect, Shadow, SourceTexture, SVPos );
                #else
                    Lighting = IntegrateBxDF( GBuffer, N, V, Rect, Shadow, SourceTexture);
                #endif
            }
            else
            {
                FCapsuleLight Capsule = GetCapsule( ToLight, LightData );

                #if REFERENCE_QUALITY
                    Lighting = IntegrateBxDF( GBuffer, N, V, Capsule, Shadow, SVPos );
                #else
                    Lighting = IntegrateBxDF( GBuffer, N, V, Capsule, Shadow, LightData.bInverseSquared );
                #endif
            }

            Lighting.Specular *= LightData.SpecularScale;
                
            LightAccumulator_AddSplit( LightAccumulator, Lighting.Diffuse, Lighting.Specular, Lighting.Diffuse, LightColor * LightMask * Shadow.SurfaceShadow, bNeedsSeparateSubsurfaceLightAccumulation );
            LightAccumulator_AddSplit( LightAccumulator, Lighting.Transmission, 0.0f, Lighting.Transmission, LightColor * LightMask * Shadow.TransmissionShadow, bNeedsSeparateSubsurfaceLightAccumulation );

            LightAccumulator.EstimatedCost += 0.4f;        // add the cost of the lighting computations (should sum up to 1 form one light)
        #endif
        #if MATERIAL_SHADINGMODEL_MYSHADERAA || MATERIAL_SHADINGMODEL_MYSHADERAB
        }
        #endif
        }
    }

(1)

修改前

else
    {
        Color.rgb *= EnvBRDF( SpecularColor, GBuffer.Roughness, NoV );
    }

修改后

    else if(GBuffer.ShadingModelID == SHADINGMODELID_MYSHADERAA || GBuffer.ShadingModelID == SHADINGMODELID_MYSHADERAA)
    {
        return float3(0.0f, 0.0f, 0.0f);
    }
    else
    {
        Color.rgb *= EnvBRDF( SpecularColor, GBuffer.Roughness, NoV );
    }

(1)

修改前

#define SHADINGMODELID_THIN_TRANSLUCENT        11
#define SHADINGMODELID_NUM                    12

修改后

#define SHADINGMODELID_MYSHADERAA        12
#define SHADINGMODELID_MYSHADERAB        13
#define SHADINGMODELID_NUM                    14

(1)

修改前

最后添加

修改后

#if MATERIAL_SHADINGMODEL_MYSHADERAA || MATERIAL_SHADINGMODEL_MYSHADERAB
    else if (ShadingModel == SHADINGMODELID_MYSHADERAA || ShadingModel == SHADINGMODELID_MYSHADERAB)
    {
        GBuffer.CustomData.rgb = float4(SubsurfaceColor, GetMaterialCustomData0(MaterialParameters));
    }
#endif

(1)

 Shader Model C++ 和 hlsl 对应的地方,

注意, C++ 中的值和 hlsl 中的值一定要相等

C++

hlsl

(1)

C++ 中添加 Shader 中的宏定义

(1)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

最后

以上就是执着大象为你收集整理的Ue4 Shader 使用的全部内容,希望文章能够帮你解决Ue4 Shader 使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部