Search Unity

How to do simple shader

Discussion in 'Shaders' started by dccoo, May 21, 2018.

  1. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    161
    Suppose I have a NxN texture.

    For each pixel (i,j), if i+j is even, I want that pixel to be black, otherwise it must be white.

    How can I do such shader?
     
  2. yanjingzhaisun

    yanjingzhaisun

    Joined:
    Jun 30, 2013
    Posts:
    11
    Code (CSharp):
    1. Shader "Debug/UV 1" {
    2. SubShader {
    3.     Pass {
    4.         CGPROGRAM
    5.         #pragma vertex vert
    6.         #pragma fragment frag
    7.         #include "UnityCG.cginc"
    8.  
    9.         // vertex input: position, UV
    10.         struct appdata {
    11.             float4 vertex : POSITION;
    12.             float4 texcoord : TEXCOORD0;
    13.         };
    14.  
    15.         struct v2f {
    16.             float4 pos : SV_POSITION;
    17.             float4 uv : TEXCOORD0;
    18.         };
    19.        
    20.         v2f vert (appdata v) {
    21.             v2f o;
    22.             o.pos = UnityObjectToClipPos(v.vertex );
    23.             o.uv = float4( v.texcoord.xy, 0, 0 );
    24.             return o;
    25.         }
    26.        
    27.         half4 frag( v2f i ) : SV_Target {
    28.             if ((i.x == i.y))
    29.                 return 0;
    30.             else return 1;
    31.  
    32.         }
    33.         ENDCG
    34.     }
    35. }
    36. }
    Like this?

    Haven't tested yet.
     
  3. dccoo

    dccoo

    Joined:
    Oct 3, 2015
    Posts:
    161
    who is i.x and i.y? there isn't definition for them in struct v2f

    anyways I think this is wrong, you are using (x == y) but it should be something like (x + y == 2n)
     
    Last edited: May 21, 2018
  4. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    620
    Code (CSharp):
    1.  
    2. v2f vert (appdata v) {
    3.             v2f o;
    4.             o.pos = UnityObjectToClipPos(v.vertex );
    5.             o.uv = float4( v.texcoord.xy, 0, 0 );
    6.             return o;
    7.         }
    8.      
    9.         half4 frag( v2f i ) : SV_Target {
    10.             float current = tex2D(_Texture, i.uv).r; // red channel holds relevant data
    11.            float next = tex2D(_Texture, i.uv + float2(1/_TextureSize, 0)).r;
    12.           float remainder = fmod(current + next, 2);
    13.  
    14.          return remainder == 0 ? 1 : 0;
    15.         }
    16.  
    The above is code pointing in the general direction.
    You need to define a sampler for your texture and probably a property, too (named _Texture in the above code).
    1/_TextureSize for example is something that you can get from internal Unity variables which I do not know the name of at the moment - I made it up. Also: 1/_TextureSize might not end up in the middle of the texel, so you may need to adjust it with a halftexel offset.
    The returned term at the end of the fragment shader needs to be of float4 - in my case it is just a scalar value.
    Hope this helps to get you started.
     
    dccoo likes this.