Search Unity

Question Help! Shader Not Working on macOS

Discussion in 'Shaders' started by KinanGH, Mar 15, 2024.

  1. KinanGH

    KinanGH

    Joined:
    Dec 3, 2019
    Posts:
    37
    Hi, I'm having trouble with a shader in Unity. It works fine on Windows and Linux, but on macOS the object using the shader is completely invisible.

    The shader creates a dashed line with a movement effect (like in many game maps). It also allows controlling the visible portion using start and end values.

    Technically, the shader shifts object coordinates for the movement effect.

    Heres how the line looks like: Capture.JPG

    Here's the shader code:
    Code (CSharp):
    1. Shader "UnityLibrary/LineRenderer/ClipStartEnd"
    2. {
    3.     Properties
    4.     {
    5.         _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,1)
    6.         _MainTex ("Particle Texture", 2D) = "white" {}
    7.         _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
    8.         _ScrollingSpeed("Scrolling Speed",float) = 2
    9.         _Start ("Start", Range (0.0,1.0)) = 0.25
    10.         _End ("End", Range (0.0,1.0)) = 0.75
    11.     }
    12.  
    13.     Category
    14.     {
    15.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
    16.     Blend SrcAlpha OneMinusSrcAlpha
    17.     ColorMask RGB
    18.     Cull Off Lighting Off ZWrite Off
    19. SubShader{
    20.         Pass
    21.         {
    22.         CGPROGRAM
    23.         #pragma vertex vert
    24.         #pragma fragment frag
    25.         #pragma target 2.0
    26.         #pragma multi_compile_particles
    27.         #pragma multi_compile_fog
    28.  
    29.         #include "UnityCG.cginc"
    30.  
    31.         struct appdata_t
    32.         {
    33.             float4 vertex : POSITION;
    34.             float2 uv : TEXCOORD0;
    35.             float4 color : COLOR;
    36.             UNITY_VERTEX_INPUT_INSTANCE_ID
    37.         };
    38.  
    39.         struct v2f
    40.         {
    41.             float2 uv : TEXCOORD0;
    42.             float4 vertex : SV_POSITION;
    43.             float4 color : COLOR;
    44.             float2 texcoord : TEXCOORD0;
    45.             UNITY_FOG_COORDS(1)
    46.             #ifdef SOFTPARTICLES_ON
    47.             float4 projPos : TEXCOORD2;
    48.             #endif
    49.             UNITY_VERTEX_OUTPUT_STEREO
    50.         };
    51.  
    52.         sampler2D _MainTex;
    53.         float4 _MainTex_ST;
    54.         float _Start;
    55.         float _End;
    56.         float4 _TintColor;
    57.         float _ScrollingSpeed;
    58.  
    59.         v2f vert (appdata_t v)
    60.         {
    61.             v2f o;
    62.             o.vertex = UnityObjectToClipPos(v.vertex);
    63.             o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    64.             o.color = v.color;
    65.             return o;
    66.         }
    67.  
    68.         UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
    69.         float _InvFade;
    70.  
    71.         float4 frag (v2f i) : SV_Target
    72.         {
    73.             #ifdef SOFTPARTICLES_ON
    74.             float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
    75.             float partZ = i.projPos.z;
    76.             float fade = saturate (_InvFade * (sceneZ-partZ));
    77.             i.color.a *= fade;
    78.             #endif
    79.  
    80.             float4 col = 2.0f * i.color * _TintColor * tex2D(_MainTex, float2(i.texcoord.x+_Time.x*_ScrollingSpeed,i.texcoord.y));
    81.             UNITY_APPLY_FOG_COLOR(i.fogCoord, col, float4(0,0,0,0)); // fog towards black due to our blend mode
    82.            
    83.             clip(- (i.uv.x <(_Start * 60) || i.uv.x > (_End * 60)));
    84.  
    85.             return col;
    86.         }
    87.         ENDCG
    88.         }
    89.         }
    90.     }
    91. }
    I'm not a shader expert myself, any ideas why it wouldn't work on macOS? Thanks!
     
  2. KinanGH

    KinanGH

    Joined:
    Dec 3, 2019
    Posts:
    37
    Please any small help that could give me a lead?
     
  3. l3fty

    l3fty

    Joined:
    Mar 23, 2013
    Posts:
    87
    I know this is from a couple of weeks ago, but might help someone else;

    Looks like your shader doesn't have a fallback defined (like Fallback "VertexLit"), so when it fails to render it appears invisible.

    As to why it's failing to render - you could try a different rendering path (In project settings > graphics > tier settings), or override on your camera. Some render paths have limited functions supported, and the shader might just be bailing out due to that (assuming it's not throwing any actual warnings/errors).
     
  4. KinanGH

    KinanGH

    Joined:
    Dec 3, 2019
    Posts:
    37
    Thank you for your response! I hadn't considered the possibility of a missing fallback causing the shader to appear invisible initially.

    Regarding your suggestion to try a different rendering path, I'll give that a shot. Unfortunately, I'm currently running Unity on Windows, issues like this can be tricky to debug without access to a machine running macOS. Nonetheless, I'll explore the options you've mentioned and see if they help resolve the problem. Thanks.