Blitting with custom shader doesn't work in URP+WebGL build

Hi all, I’m having trouble getting my custom shader working with WebGL when using URP.

The shader works correctly in the Editor and when building for Windows, but not in the WebGL build.

My shader is included in “always included shaders”, and is in a Resources folder, and has a reference in a material in the scene (so I’ve covered all bases it is definitely included).

It’s a simple shader really to apply a transform to a texture:

Shader "Custom/WebGLTestShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Transform("Transform", Vector) = (1,1,0,0)
    }

        SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert_img
            #pragma fragment frag
            #include "UnityCG.cginc"
            uniform sampler2D _MainTex;
            uniform float4 _Transform;

            float4 frag(v2f_img i) : COLOR
            {
                float u = _Transform.z + (i.uv[0] * _Transform.x);
                float v = _Transform.w + (i.uv[1] * _Transform.y);
                return tex2D(_MainTex, half2(u,v));
            }
            ENDCG
        }
    }
}

As it works with Windows and in the editor I guess it’s a problem with WebGL itself? It also works in the built-in render pipeline with WebGL though so must be something unique to URP.

I have a try/catch block to catch any exceptions but seems the error happens without throwing any.

Editor/Windows build:

WebGL:


(bottom images are after setting the resulting texture as a spotlight’s cookie. “shader ref in scene” is a material with the shader applied to a quad.)

Any ideas? Or is it a bug? Using Unity version 2021.3.12f1 (haven’t run this specific test on other versions but 2022.1.23f1 does have issues running my main code which is similar).

Thanks,
Si.

Edit: Shader.find does return a shader, but the Blit has no effect for some reason. I removed the transform to simplify the shader but same result.

Using Unity macros UNITY_DECLARE_TEX2D and UNITY_SAMPLE_TEX2D doesn’t help.

I was having similar issues when trying to blit using a custom shader inside a scriptable render pass/feature (works fine in editor/windows but black screen in webgl) This thread and specifically this solution solved my issues Graphics.Blit produces transparent texture when passing a Material (bug?)

1 Like

Thanks so much, that fixes my debug project! Hope it carries over into my main one!

Edit: it does!

1 Like