Search Unity

Color the shared space between two images Unity (SOLVED)

Discussion in 'Editor & General Support' started by abadazouhir, Dec 13, 2018.

  1. abadazouhir

    abadazouhir

    Joined:
    Oct 21, 2018
    Posts:
    4
    Hello Developer,

    I spent a lot of time looking for a solution but I could not find anything about this idea :( ...Please i need help :)

    How to color the shared space between two images Unity !!


    THANK YOU
     
  2. abadazouhir

    abadazouhir

    Joined:
    Oct 21, 2018
    Posts:
    4
    I FOUND SOLUTION

    Code (CSharp):
    1.     Shader "Unlit/NewUnlitShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.  
    18.             struct appdata_t
    19.             {
    20.                 float4 vertex   : POSITION;
    21.                 float4 color    : COLOR;
    22.                 float2 uv : TEXCOORD0;
    23.             };
    24.  
    25.             struct v2f
    26.             {
    27.                 float4 vertex   : SV_POSITION;
    28.                 fixed4 color    : COLOR;
    29.                 float2 uv  : TEXCOORD0;
    30.             };
    31.  
    32.             sampler2D _MainTex;
    33.  
    34.             v2f vert (appdata_t v)
    35.             {
    36.                 v2f o;
    37.                 o.vertex = UnityObjectToClipPos(v.vertex);
    38.                 o.uv = v.uv;
    39.                 o.color = v.color;
    40.  
    41.                 return o;
    42.             }
    43.  
    44.             fixed4 frag (v2f i) : SV_Target
    45.             {
    46.                 // sample the texture
    47.                 fixed4 col = tex2D(_MainTex, i.uv);
    48.                 col = col * i.color;
    49.  
    50.                 if(col.a < .5f)
    51.                 {
    52.                     discard;
    53.                 }
    54.  
    55.                 return col;
    56.             }
    57.  
    58.             ENDCG
    59.         }
    60.  
    61.         Pass
    62.         {
    63.             Stencil
    64.             {
    65.                 Comp always
    66.                 Pass IncrSat
    67.             }
    68.  
    69.             CGPROGRAM
    70.             #pragma vertex vert
    71.             #pragma fragment frag
    72.  
    73.             struct appdata_t
    74.             {
    75.                 float4 vertex   : POSITION;
    76.                 float4 color    : COLOR;
    77.                 float2 uv : TEXCOORD0;
    78.             };
    79.  
    80.             struct v2f
    81.             {
    82.                 float4 vertex   : SV_POSITION;
    83.                 fixed4 color    : COLOR;
    84.                 float2 uv  : TEXCOORD0;
    85.             };
    86.  
    87.             sampler2D _MainTex;
    88.  
    89.             v2f vert (appdata_t v)
    90.             {
    91.                 v2f o;
    92.                 o.vertex = UnityObjectToClipPos(v.vertex);
    93.                 o.uv = v.uv;
    94.                 o.color = v.color;
    95.  
    96.                 return o;
    97.             }
    98.  
    99.             fixed4 frag (v2f i) : SV_Target
    100.             {
    101.                 // sample the texture
    102.                 fixed4 col = fixed4(0, 0, 0, 1);
    103.                 return col;
    104.             }
    105.             ENDCG
    106.         }
    107.  
    108.         Pass
    109.         {
    110.             Stencil
    111.             {
    112.                 Ref 1
    113.                 Comp Less
    114.             }
    115.  
    116.             CGPROGRAM
    117.             #pragma vertex vert
    118.             #pragma fragment frag
    119.  
    120.             struct appdata_t
    121.             {
    122.                 float4 vertex   : POSITION;
    123.                 float4 color    : COLOR;
    124.                 float2 uv : TEXCOORD0;
    125.             };
    126.  
    127.             struct v2f
    128.             {
    129.                 float4 vertex   : SV_POSITION;
    130.                 fixed4 color    : COLOR;
    131.                 float2 uv  : TEXCOORD0;
    132.             };
    133.  
    134.             sampler2D _MainTex;
    135.  
    136.             v2f vert (appdata_t v)
    137.             {
    138.                 v2f o;
    139.                 o.vertex = UnityObjectToClipPos(v.vertex);
    140.                 o.uv = v.uv;
    141.                 o.color = v.color;
    142.  
    143.                 return o;
    144.             }
    145.  
    146.             fixed4 frag (v2f i) : SV_Target
    147.             {
    148.                 fixed4 col = fixed4(1, 1, 1, 1);
    149.                 return col;
    150.             }
    151.             ENDCG
    152.         }
    153.     }
    154. }
    155.  
     
  3. abadazouhir

    abadazouhir

    Joined:
    Oct 21, 2018
    Posts:
    4
    MORE OPTION ..IF +3 OBJECTS SHARED SAME AREA COLOR BECOME BLACK

    Code (CSharp):
    1. Shader "Custom/InvertSha" {
    2.  
    3. SubShader
    4.     {
    5.  
    6. Pass
    7.     {
    8.         Stencil
    9.         {
    10.             Comp always
    11.             Pass IncrSat
    12.         }
    13.  
    14.         CGPROGRAM
    15.         #pragma vertex vert
    16.         #pragma fragment frag
    17.  
    18.         struct appdata
    19.         {
    20.             float4 vertex : POSITION;
    21.             float2 uv : TEXCOORD0;
    22.         };
    23.  
    24.         struct v2f
    25.         {
    26.             float2 uv : TEXCOORD0;
    27.             float4 vertex : SV_POSITION;
    28.         };
    29.  
    30.         sampler2D _MainTex;
    31.         float4 _MainTex_ST;
    32.  
    33.         v2f vert (appdata v)
    34.         {
    35.             v2f o;
    36.             o.vertex = UnityObjectToClipPos(v.vertex);
    37.             o.uv = v.uv;
    38.  
    39.             return o;
    40.         }
    41.  
    42.         fixed4 frag (v2f i) : SV_Target
    43.         {
    44.             // sample the texture
    45.             fixed4 col = fixed4(0, 0, 0, 1);
    46.             // apply fog
    47.             return col;
    48.         }
    49.         ENDCG
    50.     }
    51.  
    52.     Pass
    53.     {
    54.         Stencil
    55.         {
    56.             Ref 1
    57.             Comp Less
    58.         }
    59.  
    60.         CGPROGRAM
    61.         #pragma vertex vert
    62.         #pragma fragment frag
    63.  
    64.         struct appdata
    65.         {
    66.             float4 vertex : POSITION;
    67.             float2 uv : TEXCOORD0;
    68.         };
    69.  
    70.         struct v2f
    71.         {
    72.             float2 uv : TEXCOORD0;
    73.             float4 vertex : SV_POSITION;
    74.         };
    75.  
    76.         sampler2D _MainTex;
    77.         float4 _MainTex_ST;
    78.  
    79.         v2f vert (appdata v)
    80.         {
    81.             v2f o;
    82.             o.vertex = UnityObjectToClipPos(v.vertex);
    83.             o.uv = v.uv;
    84.  
    85.             return o;
    86.         }
    87.  
    88.         fixed4 frag (v2f i) : SV_Target
    89.         {
    90.             // sample the texture
    91.             fixed4 col = fixed4(1, 1, 1, 1);
    92.             // apply fog
    93.             return col;
    94.         }
    95.         ENDCG
    96.     }
    97.  
    98.      Pass
    99.     {
    100.         Stencil
    101.         {
    102.             Ref 2
    103.             Comp Less
    104.         }
    105.  
    106.         CGPROGRAM
    107.         #pragma vertex vert
    108.         #pragma fragment frag
    109.  
    110.         struct appdata
    111.         {
    112.             float4 vertex : POSITION;
    113.             float2 uv : TEXCOORD0;
    114.         };
    115.  
    116.         struct v2f
    117.         {
    118.             float2 uv : TEXCOORD0;
    119.             float4 vertex : SV_POSITION;
    120.         };
    121.  
    122.         sampler2D _MainTex;
    123.         float4 _MainTex_ST;
    124.  
    125.         v2f vert (appdata v)
    126.         {
    127.             v2f o;
    128.             o.vertex = UnityObjectToClipPos(v.vertex);
    129.             o.uv = v.uv;
    130.  
    131.             return o;
    132.         }
    133.  
    134.         fixed4 frag (v2f i) : SV_Target
    135.         {
    136.             // sample the texture
    137.             fixed4 col = fixed4(0,0,0,1);
    138.             // apply fog
    139.             return col;
    140.         }
    141.         ENDCG
    142.     }
    143.  
    144.     }
    145. }
    146.  
    147.