Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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

    shwhjw

    Joined:
    Mar 15, 2016
    Posts:
    86
    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.     }
    8.  
    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;
    19.  
    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. }
    30.  
    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:
    upload_2023-3-25_11-44-51.png

    WebGL:
    upload_2023-3-25_11-45-13.png
    (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.
     
    Last edited: Mar 25, 2023
  2. shwhjw

    shwhjw

    Joined:
    Mar 15, 2016
    Posts:
    86
    Using Unity macros UNITY_DECLARE_TEX2D and UNITY_SAMPLE_TEX2D doesn't help.
     
  3. SpikeDevelops

    SpikeDevelops

    Joined:
    Mar 12, 2018
    Posts:
    8
    shwhjw likes this.
  4. shwhjw

    shwhjw

    Joined:
    Mar 15, 2016
    Posts:
    86
    Thanks so much, that fixes my debug project! Hope it carries over into my main one!

    Edit: it does!
     
    Last edited: Aug 6, 2023
    SpikeDevelops likes this.