Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

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

Discussion in 'Universal Render Pipeline' started by shwhjw, Mar 25, 2023.

  1. shwhjw


    Mar 15, 2016
    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:
    Code (CSharp):
    1. Shader "Custom/WebGLTestShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Transform("Transform", Vector) = (1,1,0,0)
    7.     }
    9.         SubShader
    10.     {
    11.         Pass
    12.         {
    13.             CGPROGRAM
    14.             #pragma vertex vert_img
    15.             #pragma fragment frag
    16.             #include "UnityCG.cginc"
    17.             uniform sampler2D _MainTex;
    18.             uniform float4 _Transform;
    20.             float4 frag(v2f_img i) : COLOR
    21.             {
    22.                 float u = _Transform.z + (i.uv[0] * _Transform.x);
    23.                 float v = _Transform.w + (i.uv[1] * _Transform.y);
    24.                 return tex2D(_MainTex, half2(u,v));
    25.             }
    26.             ENDCG
    27.         }
    28.     }
    29. }
    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:

    (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).


    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.
    Last edited: Mar 25, 2023
  2. shwhjw


    Mar 15, 2016
    Using Unity macros UNITY_DECLARE_TEX2D and UNITY_SAMPLE_TEX2D doesn't help.