Search Unity

RGB alpha mask shader with 3 uv-sets?

Discussion in 'Shaders' started by Enzign, Jul 29, 2015.

  1. Enzign

    Enzign

    Joined:
    Aug 20, 2010
    Posts:
    169
    Is it possible to change this shader so that it uses 3 uv-sets? So basically we have a mesh with 3 different materials and we need it to just have one material and guessing that will be possible if the mesh has 3 uv-sets instead?


    // Unlit alpha-blended shader.
    // - no lighting
    // - no lightmap support
    // - no per-material color

    Shader "Unlit/AlphaMask" {
    Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _AlphaTex ("Alpha mask (R)", 2D) = "white" {}
    }

    SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 100

    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha

    Pass {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"

    struct appdata_t {
    float4 vertex : POSITION;
    float2 texcoord : TEXCOORD0;
    };

    struct v2f {
    float4 vertex : SV_POSITION;
    half2 texcoord : TEXCOORD0;
    };

    sampler2D _MainTex;
    sampler2D _AlphaTex;

    float4 _MainTex_ST;

    v2f vert (appdata_t v)
    {
    v2f o;
    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    return o;
    }

    fixed4 frag (v2f i) : SV_Target
    {
    fixed4 col = tex2D(_MainTex, i.texcoord);
    fixed4 col2 = tex2D(_AlphaTex, i.texcoord);

    return fixed4(col.r, col.g, col.b, col2.r);
    }
    ENDCG
    }
    }

    }
     
  2. Enzign

    Enzign

    Joined:
    Aug 20, 2010
    Posts:
    169
    I don't really know much about writing shaders, but i have been able to get pretty close to where i want to be. The problem now seems to be that col1, col2 and col3 takes a color value from all the textures, no matter if the fbx has any uv assigned to some area for a specific uv-channel. I would want col2 for example be 0 if there is no uv for it. Is that doable? Here is the shader so far.

    Shader "Unlit/AlphaMask_CombinedMesh" {
    Properties {
    _MainTex1 ("Uv1 (RGB)", 2D) = "" {}
    _AlphaTex1 ("Uv1 Alpha mask (R)", 2D) = "" {}
    _MainTex2 ("Uv2 (RGB)", 2D) = "" {}
    _AlphaTex2 ("Uv2 Alpha mask (R)", 2D) = "" {}
    _MainTex3 ("Uv3 (RGB)", 2D) = "" {}
    _AlphaTex3 ("Uv3 Alpha mask (R)", 2D) = "" {}
    }

    SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 100

    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha

    Pass {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"

    struct appdata_t {
    float4 vertex : POSITION;
    float2 texcoord1 : TEXCOORD0;
    float2 texcoord2 : TEXCOORD1;
    float2 texcoord3 : TEXCOORD2;
    };

    struct v2f {
    float4 pos : SV_POSITION;
    half2 uv1 : TEXCOORD0;
    half2 uv2 : TEXCOORD1;
    half2 uv3 : TEXCOORD2;
    };

    sampler2D _MainTex1;
    sampler2D _AlphaTex1;
    sampler2D _MainTex2;
    sampler2D _AlphaTex2;
    sampler2D _MainTex3;
    sampler2D _AlphaTex3;

    float4 _MainTex1_ST;
    float4 _MainTex2_ST;
    float4 _MainTex3_ST;

    v2f vert (appdata_t v)
    {
    v2f o;
    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    o.uv1 = TRANSFORM_TEX(v.texcoord1, _MainTex1);
    o.uv2 = TRANSFORM_TEX(v.texcoord2, _MainTex2);
    o.uv3 = TRANSFORM_TEX(v.texcoord3, _MainTex3);
    return o;
    }

    fixed4 frag (v2f i) : SV_Target
    {
    fixed4 col1 = tex2D(_MainTex1, i.uv1);
    fixed4 colalpha1 = tex2D(_AlphaTex1, i.uv1);

    fixed4 col2 = tex2D(_MainTex2, i.uv2);
    fixed4 colalpha2 = tex2D(_AlphaTex2, i.uv2);

    fixed4 col3 = tex2D(_MainTex3, i.uv3);
    fixed4 colalpha3 = tex2D(_AlphaTex3, i.uv3);

    return fixed4(col1.r + col2.r + col3.r, col1.g + col2.g + col3.g, col1.b + col2.b + col3.b, colalpha1.r + colalpha2.r + colalpha3.r);
    }
    ENDCG
    }
    }

    }