I am writing custom a custom shader for Unity's Universal Render Pipeline (Version 7.3.1). The meshes are built using three separate UVMaps for different portions of the mesh. The reason I actually need all three to be separate is because two of them are scaled & offset independently by a C# script, kind of like texture atlas. I need the front and back of a sphere to be independently textured using this.

I followed this tutorial from CyanGameDev to get an understanding of the new syntax for URP as I had previously done this with the Built-In RP. The older version still used the Vertex & Fragment functions to draw it, but it was unlit so I was free to use as many TEXCOORDs as I needed. Now as I followed this tutorial, I found that I needed all 8 available TEXCOORDs immediately just to get it functioning:

struct Varyings // AKA v2f, or OUTPUT from Vertex function to Fragment function
            float4 positionCS                    : SV_POSITION; // Position in ClipSpace AKA screen space
            float4 color                         : COLOR;

            float2 uv                               : TEXCOORD0;
            DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1); // the 3rd argument is the TEXCOORD index

                float3 positionWS               : TEXCOORD2;
                float3 normalWS                 : TEXCOORD3; // Normal in WorldSpace as defined in Vert function
        #ifdef _NORMALMAP
                float4 tangentWS                : TEXCOORD4; // Tangent in WorldSpace as defined in Vert Function
                float3 viewDirWS                : TEXCOORD5;

                // Convert to half3 for just vertex lighting, free up a TEXCOORD?
                half4 fogFactorAndVertexLight   : TEXCOORD6; // x: fogFactor, yzw: vertex light

                float4 shadowCoord              : TEXCOORD7;

Is there anyway I can remove some of these and still get PBR-ish result? For instance, I don't necessarily need the fog on this object. As well, I do not really want to perform this in Shader Graph if I can avoid it.

One thought I am having is to declare the uv as a float4 and just swizzle the texcoord like uv.xy for UV1 and uv.zw for UV2 but my understanding is the texturing and offset is stored within the Z & W vectors which is why you used to define a _MainTex_ST float4.


The specific effect is this:

The background is one texture (not a solid color, or i'd do this differently) enter image description here And the Foreground is another texture (Alpha channel symbols) enter image description here

Through the script, you control which background section or foreground symbol you want by changing the tiling of the texture. The idea is that you can have many different combinations of the two textures. The back of the sphere will always have just the background color. The script is such that the amount of tiles on a texture does not have to be square, and the foreground and background textures do not need the same amount of tiles (its calculated at start). At any given time, only one of the tiles is shown on the sphere, mapped to both sides.

The final look is explained in a previous question of mine The final look is explained in a previous question of mine

  • \$\begingroup\$ You could split the mesh into submeshes, then each can use its own maps without needing to male space for the others. Or you can set your scale/offset values as uniforms and apply multiple shifts to one source UV map in the shader instead of baking each variant into its own channels. But there might be more specific options available if you can show us the particular effect you're creating. \$\endgroup\$
    – DMGregory
    Sep 1 '20 at 4:08
  • \$\begingroup\$ Edited to show the process with images \$\endgroup\$
    – Rug
    Sep 1 '20 at 5:07
  • \$\begingroup\$ And sorry, can you clarify how this is mapped on the sphere? Do we see just colour and one symbol on the sphere at a time? If so, this looks like a job for uniforms, not extra texture coordinate channels. \$\endgroup\$
    – DMGregory
    Sep 1 '20 at 11:46
  • \$\begingroup\$ I use a UV projection of the front hemisphere as one map and scale the other components down/off the map, and a second projection of the back hemisphere with the same layout. Im not sure I understand what you mean by Uniforms in this context, can you elaborate? Finally, yes it is one color, though it is not solid color, and one symbol mapped at any time. \$\endgroup\$
    – Rug
    Sep 1 '20 at 13:15
  • \$\begingroup\$ I'm asking more about the final rendered look. Can you include an image of what the sphere should look like at the end, just to be sure we're understanding you correctly? \$\endgroup\$
    – DMGregory
    Sep 1 '20 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.