Search Unity

Different tiling for two textures in one shader

Discussion in 'Shaders' started by FlashMuller, Apr 6, 2017.

  1. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    Hey guys,
    just as about any post here starts: I have no clue about shaders ;-)
    What I am trying to achieve is: I want a texture (transparent <- alpha channel) to be covering a certain object and using the tile modificator to, well, make it tiled.
    I have a second texture, basically a second alpha map, that blends certain areas in or out. This texture however has to be independent of the previous mentioned tiling.
    Same for Offset.
    You can see what I mean in the attached image, red being my (tiled 4x4) texture, green being my additional alpha.
    tiling.png

    What I have got so far is a shader that does blend the two alpha channels nicely, but as soon as I change the tiling of the main texture also the additional alpha will change its tiling.
    Changing the tiling of the alpha doesn't affect the image at all.
    Heres the code:

    Code (CSharp):
    1.  Shader "Transparent/Diffuse + Extra Alpha"
    2. {
    3.      Properties {
    4.      _Color ("Main Color", Color) = (1,1,1,1)
    5.      _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6.      _AlphaMap ("Additional Alpha Map (Greyscale)", 2D) = "white" {}
    7. }
    8. SubShader {
    9.      Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    10.      LOD 200
    11. CGPROGRAM
    12. #pragma surface surf Lambert alpha
    13. sampler2D _MainTex;
    14. sampler2D _AlphaMap;
    15. float4 _Color;
    16. struct Input {
    17.      float2 uv_MainTex;
    18. };
    19. void surf (Input IN, inout SurfaceOutput o) {
    20.      half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    21.      o.Albedo = c.rgb;
    22.      o.Alpha = c.a * tex2D(_AlphaMap, IN.uv_MainTex).r;
    23. }
    24. ENDCG
    25. }
    26. Fallback "Transparent/VertexLit"
    27. }
    28.  
    Thanks
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    Never mind. Heres the solution - in case someone else searches

    Code (CSharp):
    1.  Shader "Transparent/Diffuse + Extra Alpha"
    2. {
    3.      Properties {
    4.      _Color ("Main Color", Color) = (1,1,1,1)
    5.      _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6.      _AlphaMap ("Additional Alpha Map (Greyscale)", 2D) = "white" {}
    7. }
    8. SubShader {
    9.      Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    10.      LOD 200
    11. CGPROGRAM
    12. #pragma surface surf Lambert alpha
    13. sampler2D _MainTex;
    14. sampler2D _AlphaMap;
    15. float4 _Color;
    16. struct Input {
    17.      float2 uv_MainTex;
    18.      float2 uv_AlphaMap;
    19. };
    20. void surf (Input IN, inout SurfaceOutput o) {
    21.      half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    22.      o.Albedo = c.rgb;
    23.      o.Alpha = c.a * tex2D(_AlphaMap, IN.uv_AlphaMap).r;
    24. }
    25. ENDCG
    26. }
    27. Fallback "Transparent/VertexLit"
    28. }
    Note the changes in Line 18 and 23.
    Cheers
     
    joshua_42 likes this.