Search Unity

Combining Two Shaders

Discussion in 'Shaders' started by ysftulek, Aug 3, 2019.

  1. ysftulek

    ysftulek

    Joined:
    Aug 14, 2015
    Posts:
    46
    I need to combine two different shaders guys and I'm failing everytime because I dont know a lot about shaders.


    Shader 1:
    Code (CSharp):
    1. Unlit/Texture HSBC" {
    2.    Properties
    3.    {
    4.        _MainTex("Base (RGB), Alpha (A)", 2D) = "black" {}
    5.        _Hue("Hue", Range(0, 1.0)) = 0
    6.        _Saturation("Saturation", Range(0, 1.0)) = 0.5
    7.        _Brightness("Brightness", Range(0, 1.0)) = 0.5
    8.        _Contrast("Contrast", Range(0, 1.0)) = 0.5
    9.    }
    10.  
    11.    SubShader
    12.    {
    13.        LOD 100
    14.        Tags
    15.        {
    16.            "Queue" = "Transparent"
    17.            "IgnoreProjector" = "True"
    18.            "RenderType" = "Transparent"
    19.        }
    20.  
    21.        Cull Off
    22.        Lighting Off
    23.        ZWrite On
    24.        Fog{ Mode Off }
    25.        Offset -1, -1
    26.        Blend SrcAlpha OneMinusSrcAlpha
    27.  
    28.        Pass
    29.        {
    30.            CGPROGRAM
    31.            #pragma vertex vert
    32.            #pragma fragment frag
    33.  
    34.            #include "UnityCG.cginc"
    35.  
    36.            // Function
    37.            inline float3 applyHue(float3 aColor, float aHue)
    38.            {
    39.                float angle = radians(aHue);
    40.                float3 k = float3(0.57735, 0.57735, 0.57735);
    41.                float cosAngle = cos(angle);
    42.                //Rodrigues' rotation formula
    43.                return aColor * cosAngle + cross(k, aColor) * sin(angle) + k * dot(k, aColor) * (1 - cosAngle);
    44.            }
    45.  
    46.  
    47.            inline float4 applyHSBEffect(float4 startColor, fixed4 hsbc)
    48.            {
    49.                float hue = 360 * hsbc.r;
    50.                float saturation = hsbc.g * 2;
    51.                float brightness = hsbc.b * 2 - 1;
    52.                float contrast = hsbc.a * 2;
    53.  
    54.                float4 outputColor = startColor;
    55.                outputColor.rgb = applyHue(outputColor.rgb, hue);
    56.                outputColor.rgb = (outputColor.rgb - 0.5f) * contrast + 0.5f + brightness;
    57.                outputColor.rgb = lerp(Luminance(outputColor.rgb), outputColor.rgb, saturation);
    58.              
    59.                return outputColor;
    60.            }
    61.  
    62.  
    63.            struct appdata_t
    64.            {
    65.                float4 vertex : POSITION;
    66.                float2 texcoord : TEXCOORD0;
    67.                fixed4 color : COLOR;
    68.            };
    69.  
    70.            struct v2f
    71.            {
    72.                float4 vertex : SV_POSITION;
    73.                half2 texcoord : TEXCOORD0;
    74.                fixed4 hsbc : COLOR;
    75.            };
    76.  
    77.            sampler2D _MainTex;
    78.            fixed _Hue, _Saturation, _Brightness, _Contrast;
    79.  
    80.            v2f vert(appdata_t v)
    81.            {
    82.                v2f o;
    83.                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    84.                o.texcoord = v.texcoord;
    85.                o.hsbc = fixed4(_Hue, _Saturation, _Brightness, _Contrast);
    86.                return o;
    87.            }
    88.  
    89.            fixed4 frag(v2f i) : COLOR
    90.            {
    91.                float4 startColor = tex2D(_MainTex, i.texcoord);
    92.                float4 hsbColor = applyHSBEffect(startColor, i.hsbc);
    93.                return hsbColor;
    94.            }
    95.            ENDCG
    96.        } // Pass
    97.    } // SubShader
    98.  
    99.    SubShader
    100.    {
    101.        LOD 100
    102.        Tags
    103.        {
    104.            "Queue" = "Transparent"
    105.            "IgnoreProjector" = "True"
    106.            "RenderType" = "Transparent"
    107.        }
    108.  
    109.        Pass
    110.        {
    111.            Cull Off
    112.            Lighting Off
    113.            ZWrite Off
    114.            Fog{ Mode Off }
    115.            Offset -1, -1
    116.            ColorMask RGB
    117.            //AlphaTest Greater .01
    118.            Blend SrcAlpha OneMinusSrcAlpha
    119.            ColorMaterial AmbientAndDiffuse
    120.  
    121.            SetTexture[_MainTex] { Combine Texture * Primary }
    122.        } // Pass
    123.    } // SubShader
    124. }
    125.  
    Shader 2:
    Code (CSharp):
    1. Shader "Custom/curved"
    2. {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _thresh ("Threshold", Range (0, 16)) = 0.8
    6.         _slope ("Slope", Range (0, 1)) = 0.2
    7.         _keyingColor ("Key Colour", Color) = (1,1,1,1)
    8.  
    9.     }
    10.    
    11.     SubShader {
    12.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    13.         LOD 100
    14.        
    15.         Lighting Off
    16.         ZWrite Off
    17.         //AlphaTest Off
    18.         Blend SrcAlpha OneMinusSrcAlpha
    19.         Cull Off
    20.  
    21.         Pass {
    22.             CGPROGRAM
    23.                 #pragma vertex vert_img
    24.                 #pragma fragment frag
    25.                 #pragma fragmentoption ARB_precision_hint_fastest
    26.  
    27.                 sampler2D _MainTex;
    28.                 float3 _keyingColor;
    29.                 float _thresh; // 0.8
    30.                 float _slope; // 0.2
    31.  
    32.                 #include "UnityCG.cginc"
    33.  
    34.                 float4 frag(v2f_img i) : COLOR {
    35.                     float3 input_color = tex2D(_MainTex, i.uv).rgb;
    36.                     float d = abs(length(abs(_keyingColor.rgb - input_color.rgb)));
    37.                     float edge0 = _thresh * (1.0 - _slope);
    38.                     float alpha = smoothstep(edge0, _thresh, d);
    39.                     return float4(input_color, alpha);
    40.                 }
    41.  
    42.  
    43.             ENDCG
    44.         }
    45.  
    46.  
    47.     }
    48.  
    49.  
    50.  
    51.  
    52.         FallBack "Unlit"
    53. }
    54.  
    It should be easy I think but I'm so newbie in shaders, I even couldnt understand what these codes means and does...