1

Im a student animation and learning to write c# scripts on my own. Im trying to combine 2 shader scripts from Unity into one: one scripts is a shading effect that lets objects fade due to a range of a gizmo.

You can find it in the Asset store here for details: World Space Fading Effect https://assetstore.unity.com/packages/vfx/shaders/world-space-fading-transitions-98207

And the other script adds a cutout from the alpha in the maintex and ignore the lighting settings of the object.

Fading script:

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"

}

Flat shade / cutout script:

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
        } 
    }
}

}

The problem I have is if I combine it, it still read one of the scripts at the time, and not the two of them.

Does someone knows why and how it can be fixed? Many thanks!

Daxtron2
  • 1,239
  • 1
  • 11
  • 19

1 Answers1

1

If all what you want is to use the first shader but with alpha cutoff then according to the optional parameters section in the documentation you can do the following:

  1. In the properties section in the 1st shader add:

    _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    
  2. Then, edit the line:

    #pragma surface surf Standard addshadow vertex:vert
    

    to be:

    #pragma surface surf Standard addshadow vertex:vert alphatest:_Cutoff 
    
Yahia Zakaria
  • 1,136
  • 9
  • 14
  • Ah cool, thanks! With a little check the cutof function works immediately. Now I only have to check how I can add the flat lighting option :) – Mauritz Seerden Apr 04 '18 at 16:14
  • The second shader has no lighting, If this is what you aim to do, you can try removing: o.Albedo = c.rgb; o.Metallic = _Metallic; o.Smoothness = Glossiness; and replace them with: o.Albedo = float3(0,0,0); o.Emission += c.rgb; – Yahia Zakaria Apr 04 '18 at 16:48
  • Ah of course! I was thinking to hard to merge, but this is way better. Again many thanks for your reply! Now I can go on designing :) – Mauritz Seerden Apr 04 '18 at 17:15