Genuine help needed wrapping my head around this UV issue.

Hey guys,

I posted another thread here: Mudbox-style multiple UV tiles question! - Unity Engine - Unity Discussions 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.

Here is what I have at the moment, based on a 4-texture shader I found here.

Shader "Custom/4 UV-Tile Shader" {
    Properties {
        _UVTile0 ("U1, V1 (RGB)", 2D) = "white" {}
        _UVTile1 ("U1, V2 (RGB)", 2D) = "white" {}
        _UVTile2 ("U2, V1 (RGB)", 2D) = "white" {}
        _UVTile3 ("U2, V2 (RGB)", 2D) = "white" {}
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Lambert

        sampler2D _UVTile0;
        sampler2D _UVTile1;
        sampler2D _UVTile2;
        sampler2D _UVTile3;

        struct Input {
            float2 uv_UVTile0;
            float2 uv_UVTile1;
            float2 uv_UVTile2;
            float2 uv_UVTile3;
        };

        void surf (Input IN, inout SurfaceOutput o) {
       
            half4 c0 = tex2D (_UVTile0, IN.uv_UVTile0);
            half4 c1 = tex2D (_UVTile1, IN.uv_UVTile1);
            half4 c2 = tex2D (_UVTile2, IN.uv_UVTile2);
            half4 c3 = tex2D (_UVTile3, IN.uv_UVTile3);

            if(IN.uv_UVTile0.x >= 0.5)
            {
                if(IN.uv_UVTile0.y <= 0.5)
                {
                    c0.rgb = c1.rgb = c2.rgb = 0;
                }
                else
                {
                    c0.rgb = c2.rgb = c3.rgb = 0;
                }
            }
            else
            {
                if(IN.uv_UVTile0.y <= 0.5)
                {
                    c0.rgb = c1.rgb = c3.rgb = 0;
                }
                else
                {
                    c1.rgb = c2.rgb = c3.rgb = 0;
                }
            }
            o.Albedo = c0.rgb + c1.rgb + c2.rgb + c3.rgb;
            o.Alpha = c0.a + c1.a + c2.a + c3.a ;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

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.

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.