Search Unity

"UI" Particle Shader - iOS problems

Discussion in 'Universal Render Pipeline' started by Moritz5thPlanet, Feb 18, 2020.

  1. Moritz5thPlanet

    Moritz5thPlanet

    Joined:
    Feb 5, 2019
    Posts:
    73
    So from an old unity UI extensions package, we used a few shaders to have particles in UI space. They are used in a Material on a procedural mesh which has a Canvas Renderer.

    We use Universal RP, so I'm looking for a way to rewrite them in a modern way, anyhow.

    The problem I have is they don't render on iOS (across all our available devices and OS versions). No error, just nothing. They are fine on all other platforms, including Metal in the Editor and Device Simulators, on very Android phone we got, on PC, etc.

    Any ideas how to change this shader? Any glaring omissions for iOS?

    Can't I get rid of all the Stencil and Alpha Clip baggage? (I'm fairly unfamiliar with how uGUI actually does its rendering now that the renderer is the SRP, and documentation is sparse and ambiguous...)

    My idea would be to somehow make this Shader mimic the same behaviors as Sprites/Default, but isn't that, too, a legacy from the Builtin renderer?

    Code (Boo):
    1. Shader "5pg/Particles/UI/Additive" {
    2. Properties {
    3.     _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
    4.     _MainTex ("Particle Texture", 2D) = "white" {}
    5.  
    6.     _StencilComp ("Stencil Comparison", Float) = 8
    7.     _Stencil ("Stencil ID", Float) = 0
    8.     _StencilOp ("Stencil Operation", Float) = 0
    9.     _StencilWriteMask ("Stencil Write Mask", Float) = 255
    10.     _StencilReadMask ("Stencil Read Mask", Float) = 255
    11.  
    12.     [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
    13. }
    14.  
    15. Category {
    16.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" "CanUseSpriteAtlas"="True" }
    17.     Blend SrcAlpha One
    18.     ColorMask RGB
    19.     Cull Off Lighting Off ZWrite Off
    20.  
    21.     SubShader {
    22.  
    23.         Stencil
    24.         {
    25.             Ref [_Stencil]
    26.             Comp [_StencilComp]
    27.             Pass [_StencilOp]
    28.             ReadMask [_StencilReadMask]
    29.             WriteMask [_StencilWriteMask]
    30.         }
    31.  
    32.         Pass {
    33.  
    34.             CGPROGRAM
    35.             #pragma vertex vert
    36.             #pragma fragment frag
    37.  
    38.             #include "UnityCG.cginc"
    39.             #include "UnityUI.cginc"
    40.  
    41.             #pragma multi_compile __ UNITY_UI_ALPHACLIP
    42.  
    43.             sampler2D _MainTex;
    44.             fixed4 _TintColor;
    45.  
    46.             struct appdata_t {
    47.                 float4 vertex : POSITION;
    48.                 fixed4 color : COLOR;
    49.                 float2 texcoord : TEXCOORD0;
    50.             };
    51.  
    52.             struct v2f {
    53.                 float4 vertex : SV_POSITION;
    54.                 fixed4 color : COLOR;
    55.                 float2 texcoord : TEXCOORD0;
    56.             };
    57.  
    58.             float4 _MainTex_ST;
    59.  
    60.             v2f vert (appdata_t IN)
    61.             {
    62.                 v2f v;
    63.                 v.vertex = UnityObjectToClipPos(IN.vertex);
    64.                 v.color = IN.color;
    65.                 v.texcoord = TRANSFORM_TEX(IN.texcoord,_MainTex);
    66.                 return v;
    67.             }
    68.  
    69.             fixed4 frag (v2f IN) : SV_Target
    70.             {
    71.                 fixed4 col = 2.0f * IN.color * _TintColor * tex2D(_MainTex, IN.texcoord);
    72.                 return col;
    73.             }
    74.             ENDCG
    75.         }
    76.     }
    77. }
    78. }
    79.  
     
    Last edited: Feb 18, 2020
  2. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    I have attempted to adapt the builtin default sprite shader to the URP format.
    Code (CSharp):
    1. Shader "Universal Render Pipeline/2D/CustomURPSpriteShader"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    8.         [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
    9.         [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
    10.         [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
    11.         [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
    12.         [Enum(UnityEngine.Rendering.BlendMode)]_SrcBlend ("Src Blend", Float) = 5
    13.         [Enum(UnityEngine.Rendering.BlendMode)]_DstBlend ("Dst Blend", Float) = 10
    14.     }
    15.  
    16.     SubShader
    17.     {
    18.         Tags
    19.         {
    20.             "Queue"="Transparent"
    21.             "IgnoreProjector"="True"
    22.             "RenderType"="Transparent"
    23.             "PreviewType"="Plane"
    24.             "CanUseSpriteAtlas"="True"
    25.         }
    26.  
    27.         Cull Off
    28.         Lighting Off
    29.         ZWrite Off
    30.         Blend [_SrcBlend] [_DstBlend]
    31.  
    32.         Pass
    33.         {
    34.             HLSLPROGRAM
    35.             // Required to compile gles 2.0 with standard srp library
    36.             #pragma prefer_hlslcc gles
    37.             #pragma exclude_renderers d3d11_9x
    38.  
    39.             #pragma vertex vert
    40.             #pragma fragment frag
    41.             #pragma shader_feature _ALPHATEST_ON
    42.             #pragma shader_feature _ALPHAPREMULTIPLY_ON
    43.  
    44.             // -------------------------------------
    45.             // Unity defined keywords
    46.             #pragma multi_compile_fog
    47.             #pragma multi_compile_instancing
    48.  
    49.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    50.             #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl"
    51.             #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
    52.  
    53.             struct Attributes
    54.             {
    55.                 float4 positionOS       : POSITION;
    56.                 float2 uv               : TEXCOORD0;
    57.                 float4 color : COLOR;
    58.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    59.             };
    60.  
    61.             struct Varyings
    62.             {
    63.                 float2 uv        : TEXCOORD0;
    64.                 float fogCoord  : TEXCOORD1;
    65.                 float4 vertex : SV_POSITION;
    66.                 float4 color : COLOR;
    67.  
    68.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    69.                 UNITY_VERTEX_OUTPUT_STEREO
    70.             };
    71.  
    72.             TEXTURE2D(_MainTex);
    73.             SAMPLER(sampler_MainTex);
    74.             TEXTURE2D(_AlphaTex);
    75.             SAMPLER(sampler_AlphaTex);
    76.  
    77.             CBUFFER_START(UnityPerMaterial)
    78.             float4 _MainTex_ST;
    79.             float4 _AlphaTex_ST;
    80.             half4 _RendererColor;
    81.             half4 _Color;
    82.             half _Cutoff;
    83.             float2 _Flip;
    84.             half _EnableExternalAlpha;
    85.             CBUFFER_END
    86.  
    87.             inline float3 UnityFlipSprite(in float3 pos, in float2 flip)
    88.             {
    89.                 return float3(pos.xy * flip, pos.z);
    90.             }
    91.  
    92.             Varyings vert(Attributes input)
    93.             {
    94.                 Varyings output = (Varyings)0;
    95.  
    96.                 UNITY_SETUP_INSTANCE_ID(input);
    97.                 UNITY_TRANSFER_INSTANCE_ID(input, output);
    98.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    99.  
    100.                 float3 vert = UnityFlipSprite(input.positionOS.xyz, _Flip);
    101.                
    102.                 VertexPositionInputs vertexInput = GetVertexPositionInputs(vert);
    103.                 output.vertex = vertexInput.positionCS;
    104.                 output.uv = TRANSFORM_TEX(input.uv, _MainTex);
    105.                 output.fogCoord = ComputeFogFactor(vertexInput.positionCS.z);
    106.                 output.color = input.color * _Color * _RendererColor;
    107.  
    108.                 return output;
    109.             }
    110.  
    111.             half4 frag(Varyings input) : SV_Target
    112.             {
    113.                 UNITY_SETUP_INSTANCE_ID(input);
    114.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    115.  
    116.                 half2 uv = input.uv;
    117.                 half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
    118.                 half3 color = texColor.rgb * input.color.rgb;
    119.                 half alpha = texColor.a * input.color.a;
    120.                
    121. #ifdef _ALPHATEST_ON
    122.                     clip(alpha - _Cutoff);
    123. #endif
    124.  
    125. #ifdef _ALPHAPREMULTIPLY_ON
    126.                 color *= alpha;
    127. #endif
    128.  
    129.                 color = MixFog(color, input.fogCoord);
    130.  
    131.                 return half4(color, alpha);
    132.             }
    133.             ENDHLSL
    134.         }
    135.     }
    136. }
    137.  
     
    Moritz5thPlanet likes this.
  3. Moritz5thPlanet

    Moritz5thPlanet

    Joined:
    Feb 5, 2019
    Posts:
    73
    Interesting, I can work off of that. I still don't understand what's special about rendering on UI / in UI.

    Also, isn't PerRendererData just a glorified HideInInspector in SRP? I read that somewhere on these forums. (I really liked how it worked before, but maybe it was all smoke and mirrors)
     
  4. Moritz5thPlanet

    Moritz5thPlanet

    Joined:
    Feb 5, 2019
    Posts:
    73
    The suggested shader has the same behaviour, doesn't show on iOS device, shows fine on all other platforms, Editors, and mobiles.
     
  5. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    192
    It has worked for me on Unity 2019.3.0f6 when building out to iOS. I suspect that the use of custom vertex streams in SpriteRenderers and ParticleRenderers is messing up the rendering for URP. I have not fully investigated every single rendering feature/exploit that the builtin renderers are using, might have missed out on some other needed Material Property or shader variables that is needed for iOS
     
    Moritz5thPlanet likes this.
  6. Moritz5thPlanet

    Moritz5thPlanet

    Joined:
    Feb 5, 2019
    Posts:
    73
    I solved it using Particles and sorting layers, and getting away from the terrible Overlay Canvases.

    Here is a demo project how you can do particles in UI (using sorting layers - so you can't sort within UI hierarchies, sadly).

    Still need to test it on iPhone, but standard particles always worked there.
     

    Attached Files: