从资源商店

时间:2018-04-04 10:39:25

标签: unity3d shader lighting fading

我是学生动画,学习自己编写c#脚本。我试图将Unity中的两个着色器脚本组合成一个:一个脚本是一种着色效果,可以让对象因一系列Gizmo而褪色。

您可以在资产商店中找到它以获取详细信息: 世界空间褪色效应 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
        } 
    }
}

}

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

有人知道为什么以及如何修复它?非常感谢!

1 个答案:

答案 0 :(得分: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