【问题标题】:Combining 2 shader scripts in Unity from the Asset Store在 Unity 中组合 Asset Store 中的 2 个着色器脚本
【发布时间】:2018-04-04 10:39:25
【问题描述】:

我是一名动画学生,正在学习自己编写 c# 脚本。我试图将 Unity 中的 2 个着色器脚本合并为一个:一个脚本是一种着色效果,可让对象因一系列小玩意而褪色。

您可以在此处的资产商店中找到它以了解详细信息: 世界空间衰落效果 https://assetstore.unity.com/packages/vfx/shaders/world-space-fading-transitions-98207

另一个脚本在 maintex 中添加了 alpha 的剪切,并忽略了对象的照明设置。

褪色脚本:

Shader "Fading/Surface/DissolveGlowEdited" {
Properties {
    _Color ("Color", Color) = (1,1,1,1)
    _MainTex ("Albedo (RGB)", 2D) = "white" {}
    //_Noise ("noise", 2D) = "white" {}
    _Glossiness ("Smoothness", Range(0,1)) = 0.5
    _Metallic ("Metallic", Range(0,1)) = 0.0
    //_spread ("dissolveSpread", Range(0,1)) = 1.0

    _GlowIntensity("Glow Intensity", Range(0.0, 5.0)) = 1
    _GlowScale("Glow Size", Range(0.0, 5.0)) = 1.0
    _Glow("Glow Color", Color) = (1, 0, 0, 1)
    _GlowEnd("Glow End Color", Color) = (1, 1, 0, 1)
    _GlowColFac("Glow Colorshift", Range(0.01, 2.0)) = 0.5

    _SectionColor ("Section Color", Color) = (1,0,0,1)
    [Toggle] _inverse("inverse", Float) = 0
    [Toggle] _doubleSided ("doubleSided", Float) = 1
    [Toggle(RETRACT_BACKFACES)] _retractBackfaces("retractBackfaces", Float) = 0
    [Toggle(DISSOLVE_GLOW)] _glowdissolve("glowdissolve", Float) = 1
}
SubShader {
    Tags { "RenderType"="Clipping" }
    LOD 200

    // ------------------------------------------------------------------

    Cull off

    CGPROGRAM
    // Physically based Standard lighting model, and enable shadows on all light types
    #pragma surface surf Standard addshadow vertex:vert
    #pragma multi_compile __ FADE_PLANE FADE_SPHERE
    #pragma shader_feature RETRACT_BACKFACES
    #pragma shader_feature DISSOLVE_GLOW
    #include "CGIncludes/section_clipping_CS.cginc"

    // Use shader model 3.0 target, to get nicer looking lighting
    #pragma target 3.5

    sampler2D _MainTex;

    struct Input {
        float2 uv_MainTex;
        float3 worldPos;
        float myface : VFACE;
    };

    half _BackfaceExtrusion;

    void vert (inout appdata_full v) {
        #if RETRACT_BACKFACES
        float3 viewDir = ObjSpaceViewDir(v.vertex);
        float dotProduct = dot(v.normal, viewDir);
        if(dotProduct<0) {
            float3 worldPos = mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1)).xyz;
            float3 worldNorm = UnityObjectToWorldNormal(v.normal);
            worldPos -= worldNorm * _BackfaceExtrusion;
            v.vertex.xyz = mul(unity_WorldToObject, float4(worldPos, 1)).xyz;
        }
        #endif
    }

    half _Glossiness;
    half _Metallic;
    fixed4 _Color;
    fixed4 _SectionColor;
    fixed _doubleSided;

    fixed4 _Glow;
    fixed4 _GlowEnd;
    half _GlowScale;
    half _GlowColFac;
    half _GlowIntensity;


    void surf (Input IN, inout SurfaceOutputStandard o) {
        fixed4 glowCol = fixed4(0,0,0,0);
        //if(IN.myface<0&&_doubleSided==0) discard; 
        #if (FADE_PLANE || FADE_SPHERE)&& DISSOLVE_GLOW
        if(IN.myface<0&&_doubleSided==0) discard; else PLANE_CLIP(IN.worldPos);

        float4 fade = PLANE_FADE(IN.worldPos);

        //Combine texture factor with geometry coefficient from vertex routine.
        half dFinal = fade.a;

        //Shift the computed raw alpha value based on the scale factor of the glow.
        //Scale the shifted value based on effect intensity.
        half dPredict = (_GlowScale - dFinal) * _GlowIntensity;

        //Change colour interpolation by adding in another factor controlling the gradient.
        half dPredictCol = (_GlowScale * _GlowColFac - dFinal) * _GlowIntensity;                      

        //Calculate and clamp glow colour.
        glowCol = dPredict * lerp(_Glow, _GlowEnd, clamp(dPredictCol, 0.0f, 1.0f));
        glowCol = clamp(glowCol, 0.0f, 1.0f);

        #endif
        fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
        o.Albedo = c.rgb;
        // Metallic and smoothness come from slider variables
        if(IN.myface>0) 
        {
            #if (FADE_PLANE || FADE_SPHERE)&& DISSOLVE_GLOW
            glowCol = clamp(glowCol, 0.0f, 1.0f);

            o.Emission = glowCol;
            #endif
            o.Albedo = c.rgb;
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        else
        {       
            #if (FADE_PLANE || FADE_SPHERE)&& DISSOLVE_GLOW
            glowCol.rgb = lerp(glowCol.rgb,  0.5*o.Albedo, dFinal);
            glowCol = clamp(glowCol, 0.0f, 1.0f);
            if(dFinal>1) glowCol.rgb = 0.5*o.Albedo;
            o.Emission = glowCol;
            #else
            o.Emission =  0.5*o.Albedo;
            #endif
            o.Albedo = float3(0,0,0);
        }
    }
    ENDCG
}
FallBack "Diffuse"

}

平面阴影/剪切脚本:

Shader "Unlit/Transparent Color CutoutTrans" {
Properties {
    _Color ("Color Tint", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}

SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutoff"}
    Pass {
        Alphatest Greater [_Cutoff]
        Lighting Off
        SetTexture [_MainTex] {
           constantColor [_Color]
           combine texture * constant DOUBLE
        } 
    }
}

}

我遇到的问题是,如果我将它结合起来,它当时仍然会读取其中一个脚本,而不是其中两个。

有人知道为什么以及如何解决它吗?非常感谢!

【问题讨论】:

    标签: unity3d shader lighting fading


    【解决方案1】:

    如果您只想使用第一个着色器但使用 alpha 截止,那么根据 optional parameters section in the documentation 您可以执行以下操作:

    1. 在第一个着色器的属性部分添加:

      _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
      
    2. 然后,编辑该行:

      #pragma surface surf Standard addshadow vertex:vert
      

      成为:

      #pragma surface surf Standard addshadow vertex:vert alphatest:_Cutoff 
      

    【讨论】:

    • 太棒了,谢谢!稍加检查,cutof 功能立即生效。现在我只需要检查如何添加平面照明选项 :)
    • 第二个着色器没有光照,如果这是您的目标,您可以尝试移除:o.Albedo = c.rgb; o.Metallic = _Metallic; o.Smoothness = 光泽度;并将它们替换为:o.Albedo = float3(0,0,0); o.Emission += c.rgb;
    • 啊当然!我在考虑很难合并,但这要好得多。再次非常感谢您的回复!现在我可以继续设计了 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多