Check if outside of clamping in shader chunk

I have a setup with detailMaps to project a barcode onto a box. This currently looks like this:

This is the result I desire, but currently I need 2 detailMaps for this. One for the barcode and one for the white background. Both of this textures are set to clamp and have transparent edges. Barcode as well as the background need to be tinted.

Here is my current shader chunk override for this:

uniform vec3 material_diffuseDetail;
uniform vec3 material_diffuseDetail2;

#ifdef MAPTEXTURE
uniform sampler2D texture_diffuseDetailMap;
uniform sampler2D texture_diffuseDetailMap2;
#endif
vec3 addAlbedoDetail(vec3 albedo) {
#ifdef MAPTEXTURE
    vec4 albedoDetail = gammaCorrectInput(texture2DBias(texture_diffuseDetailMap, \$UV, textureBias).rgba);
    if(albedoDetail.a == 0.0){
        vec4 albedoDetail2 = gammaCorrectInput(texture2DBias(texture_diffuseDetailMap2, \$UV, textureBias).rgba);
        if(albedoDetail2.a == 0.0){
            return albedo;
        }
        return albedoDetail2.rgb * material_diffuseDetail2.rgb;
    }
    return albedoDetail.rgb * material_diffuseDetail.rgb;
#else
    return albedo;
#endif
}

And my editor setup:
capture

Now to eliminate the second texture for the background I would like to know if it is possible in the shader to know wether I´m in the area of the red square or not (see image below). Which depends on the offset and tiling of the detailMap.

In the shader I could then check:
Outside Area → Diffuse albedo (do nothing/transparent)
Inside Area, but no barcode → Background with tinting
Inside Area and barcode → Barcode with tinting

@Gustav_Sterbrant - any thoughts on this one?

Any idea for this?
It would also be cool to have a second detail map, so that I can have a second bar code on the box.

If there is a better way to do something like this than detail maps, I’m also open to it :smiley:

Could you generate the barcode with white background as single texture at runtime and that way you can use just one detail map

That would work if I have one barcode, but if I have a second one I do need a second detailMap. Generating the texture at runtime would probably lead to a new set of problems^^

Is it possible to get the offset and tiling of the detailMap in the shader chunk? That would eliminate the need for the background texture, as I could then check if im in the barcode area or outside of it.

I guess I could just add a uniform vec4 and set the offset and tiling to it.

@LucaHeft

If you know the UVs map between 0-1, then you can skip applying the rectangle if the UV coordinate for your pixel is outside the area. If you define your clipping area as a uniform vec4 containing left, right, top, bottom, then all pixels between 0 < left, right > 1, 0 < top, 1 > bottom can be discarded.

Obviously, to handle tiling and offsets, you’d need to do something more clever, since you’d get a repeating pattern. Something like this should work:


const vec2 tiling = vec2(4.0, 4.0);
const vec4 clip = vec4(0.25, 0.75, 0.25, 0.75);

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = mod(fragCoord/iResolution.xy * tiling, vec2(1.0));
    if (   uv.x < clip.x || uv.x > clip.y
        || uv.y < clip.z || uv.y > clip.w)
        fragColor = vec4(0,0,0,0);
    else
        fragColor = vec4(1,1,1,1);
}

Which produces this image:

3 Likes