Search Unity

Genuine help needed wrapping my head around this UV issue.

Discussion in 'Shaders' started by Fabelwesen, Sep 9, 2014.

  1. Fabelwesen

    Fabelwesen

    Joined:
    Aug 14, 2014
    Posts:
    9
    Hey guys,

    I posted another thread here: http://forum.unity3d.com/threads/mudbox-style-multiple-uv-tiles-question.267089/ and have since spent the entire day trying to get my head around CG but can't seem to get the thing to work. I thought I would post it here since this is the shader section, which I missed before. While I seem to have figured out how to do a whole lot of other things, and some more after buying and playing around with ShaderForge I still cannot seem to figure out how to solve my problem.

    Could someone please give me a hand with this? I am not sure how to apply the sampled textures from my 4 images to their respective UV quadrants. Loading the texture or even loading all four is easy, but I can't get them to follow the UVs unless the UVs are on the same quadrant (which, of course, causes terrible blur effects on the building). I am really sorry to ask for this, but with all the searching and reading I've done today I feel no closer to the solution.

    To avoid having to click the link above, the problem is that I have 4 UV tiles from a large building model I have painted in Mudbox. Having the texture on one 4096 is not an option due to the detail needed, so instead mudbox spits out 4 4096 tiles as seen on the UV space below.

     
  2. Fabelwesen

    Fabelwesen

    Joined:
    Aug 14, 2014
    Posts:
    9
    Here is what I have at the moment, based on a 4-texture shader I found here.

    Code (CSharp):
    1. Shader "Custom/4 UV-Tile Shader" {
    2.     Properties {
    3.         _UVTile0 ("U1, V1 (RGB)", 2D) = "white" {}
    4.         _UVTile1 ("U1, V2 (RGB)", 2D) = "white" {}
    5.         _UVTile2 ("U2, V1 (RGB)", 2D) = "white" {}
    6.         _UVTile3 ("U2, V2 (RGB)", 2D) = "white" {}
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.  
    12.         CGPROGRAM
    13.         #pragma surface surf Lambert
    14.  
    15.         sampler2D _UVTile0;
    16.         sampler2D _UVTile1;
    17.         sampler2D _UVTile2;
    18.         sampler2D _UVTile3;
    19.  
    20.         struct Input {
    21.             float2 uv_UVTile0;
    22.             float2 uv_UVTile1;
    23.             float2 uv_UVTile2;
    24.             float2 uv_UVTile3;
    25.         };
    26.  
    27.         void surf (Input IN, inout SurfaceOutput o) {
    28.        
    29.             half4 c0 = tex2D (_UVTile0, IN.uv_UVTile0);
    30.             half4 c1 = tex2D (_UVTile1, IN.uv_UVTile1);
    31.             half4 c2 = tex2D (_UVTile2, IN.uv_UVTile2);
    32.             half4 c3 = tex2D (_UVTile3, IN.uv_UVTile3);
    33.  
    34.             if(IN.uv_UVTile0.x >= 0.5)
    35.             {
    36.                 if(IN.uv_UVTile0.y <= 0.5)
    37.                 {
    38.                     c0.rgb = c1.rgb = c2.rgb = 0;
    39.                 }
    40.                 else
    41.                 {
    42.                     c0.rgb = c2.rgb = c3.rgb = 0;
    43.                 }
    44.             }
    45.             else
    46.             {
    47.                 if(IN.uv_UVTile0.y <= 0.5)
    48.                 {
    49.                     c0.rgb = c1.rgb = c3.rgb = 0;
    50.                 }
    51.                 else
    52.                 {
    53.                     c1.rgb = c2.rgb = c3.rgb = 0;
    54.                 }
    55.             }
    56.             o.Albedo = c0.rgb + c1.rgb + c2.rgb + c3.rgb;
    57.             o.Alpha = c0.a + c1.a + c2.a + c3.a ;
    58.         }
    59.         ENDCG
    60.     }
    61.     FallBack "Diffuse"
    62. }
    amusingly if I turn off clamp, and let it sit on Repeat, the 2nd texture (tile1) lines up correctly (with artifacts). This is probably just a fluke but ...

    How would I apply a UV offset like in 3dsmax' Coordinates rollout? The Offset in the rollout in Unity doesn't seem to do much in regards to this?

    Here's a screen from the slate editor in max to show what I mean. Ignore the black maps and the yellow lines. I painted parts of the house in happy colors to see how the maps lined up and max shows black when the map is offset but renders OK. The offset of 1,1 here would put the tile in the UV space from U1V1 to U2V2.

     
  3. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Scale up your UVs by 200%, set up 4 materials, give each material one of the textures, sky to the appropriate plugins.

    Doing this in the shader is unnecessary and will give you artefacts, you can easily fix this at the mesh level.