Search Unity

RGBA texture channels and drive world space UV coordinates

Discussion in 'Shaders' started by marcatore, Feb 4, 2018.

  1. marcatore

    marcatore

    Joined:
    May 22, 2015
    Posts:
    160
    I'd like to use the 4 channels of a RGBA textures and mapping every channel in world space distribuiting the channel one after another one like the image below



    Is it something possible to do?

    Thanks in advance
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Yep.











    You're probably asking about how to do it too.

    There are several threads on world space UVs which you can look up on your own, which is the first part of this. The "hard" part is how to handle switching between channels. Really all you need is to calculate something like a checkerboard in the UVs, offset for each channel, and multiply each texture channel by the offset checkerboard mask.

    Here's a very basic example:
    Code (CSharp):
    1. Shader "Unlit/ColorChannelCheckers"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _ColorR ("Color R", Color) = (1,0,0,1)
    7.         _ColorG ("Color G", Color) = (0,1,0,1)
    8.         _ColorB ("Color B", Color) = (0,0,1,1)
    9.         _ColorA ("Color A", Color) = (0.5,0.5,0.5,1)
    10.     }
    11.     SubShader
    12.     {
    13.         Tags { "RenderType"="Opaque" }
    14.         LOD 100
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             // make fog work
    22.             #pragma multi_compile_fog
    23.            
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float2 uv : TEXCOORD0;
    35.                 UNITY_FOG_COORDS(1)
    36.                 float4 vertex : SV_POSITION;
    37.             };
    38.  
    39.             sampler2D _MainTex;
    40.             float4 _MainTex_ST;
    41.            
    42.             v2f vert (appdata v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = UnityObjectToClipPos(v.vertex);
    46.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    47.                 UNITY_TRANSFER_FOG(o,o.vertex);
    48.                 return o;
    49.             }
    50.  
    51.             fixed4 _ColorR, _ColorG, _ColorB, _ColorA;
    52.            
    53.             fixed4 frag (v2f i) : SV_Target
    54.             {
    55.                 float2 uvScaled = i.uv.xy * 0.5;
    56.                 fixed2 lines = fixed2(step(0.5, frac(uvScaled.x)), step(0.5, frac(uvScaled.y)));
    57.  
    58.                 fixed maskA = lines.x * lines.y;
    59.                 fixed maskB = lines.x * (1-lines.y);
    60.                 fixed maskG = (1-lines.x) * lines.y;
    61.                 fixed maskR = (1-lines.x) * (1-lines.y);
    62.  
    63.                 fixed4 mask = fixed4(maskR, maskG, maskB, maskA);
    64.  
    65.                 fixed4 tex = tex2D(_MainTex, i.uv) * mask;
    66.  
    67.                 fixed4 col = tex.r * _ColorR +
    68.                              tex.g * _ColorG +
    69.                              tex.b * _ColorB +
    70.                              tex.a * _ColorA;
    71.  
    72.                 UNITY_APPLY_FOG(i.fogCoord, col);
    73.                 return col;
    74.             }
    75.             ENDCG
    76.         }
    77.     }
    78. }
     
    marcatore and neoshaman like this.
  3. marcatore

    marcatore

    Joined:
    May 22, 2015
    Posts:
    160
    Thank you @bgolus

    I'll study what you proposed. Meanwhile thank you very much.
     
  4. marcatore

    marcatore

    Joined:
    May 22, 2015
    Posts:
    160
    I've tried your shader and it's seems a really good start to make what I'd like.
    Thank you again.

    Now I hope to complete it as I want. This is up to me.
     
  5. marcatore

    marcatore

    Joined:
    May 22, 2015
    Posts:
    160
    I did it!! :)
    Thank you bgolus for your suggestions.

    I share what I've achieved to say "thank you" to you and to the community that help me to increase my knowledge.
     

    Attached Files: