0

Attempting to create a custom shader for an application that I'm building for Magic Leap 1. The shaders purpose is to only render the shadows cast onto the material. The shader works as intended in the unity editor but throws errors on mldb logs and the material turns pink which in my experience means issues with the renderpipeline/shader. I assume the issue is with some syntax or Unity Semantic that isn't easily translated to openGLCore/GLES by Unity's built in HLSLcc which compiles shaders into said language. The following is the shader from top to bottom:

    Shader "Custom/SecondaryShadowShader"
    {
        Properties
        {
            [NoScaleOffset] _MainTex("Texture", 2D) = "grey" {}
            _Color("Main Color", Color) = (1,1,1,1)
            _ShadowColor("Shadow Color", Color) = (1,1,1,1)
            _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
        }
            SubShader
            {
        Pass
        {
            Tags {"LightMode" = "ForwardBase"}
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            fixed4 _ShadowColor;
        #include "UnityCG.cginc"
        #include "Lighting.cginc"

    // compile shader into multiple variants, with and without shadows
    // (we don't care about any lightmaps yet, so skip these variants)
    #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
    #pragma multi_compile_instancing
    // shadow helper functions and macros
    #include "AutoLight.cginc"

    struct appdata
    {
        float2 texcoord : TEXCOORD0;
        float3 normal : NORMAL;
        float4 vertex : POSITION;

        UNITY_VERTEX_INPUT_INSTANCE_ID
    };

    struct v2f
    {
        float2 uv : TEXCOORD0;
        SHADOW_COORDS(1) // put shadows data into TEXCOORD1
        fixed3 diff : COLOR0;
        fixed3 ambient : COLOR1;
        float4 pos : SV_POSITION;

        UNITY_VERTEX_OUTPUT_STEREO
    };

    v2f vert(appdata v)
    {
        v2f o;

       UNITY_SETUP_INSTANCE_ID(v);
       UNITY_INITIALIZE_OUTPUT(v2f, o);
       UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

        o.pos = UnityObjectToClipPos(v.vertex);
        o.uv = v.texcoord;
        half3 worldNormal = UnityObjectToWorldNormal(v.normal);
        half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
        o.diff = nl * _LightColor0.rgb;
        o.ambient = ShadeSH9(half4(worldNormal,1));
        // compute shadows data
        TRANSFER_SHADOW(o)
        return o;
    }

    UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex);

    fixed4 frag(v2f i) : SV_Target
    {

        UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);

        fixed4 col = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, i.uv); //Insert
        fixed shadow = SHADOW_ATTENUATION(i);
        clip(-shadow);
        fixed3 lighting = i.diff * shadow + i.ambient;
        lighting += _ShadowColor * lighting;
        col.rgb *= lighting;
        
        
        return col;
    }
ENDCG
}

UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
   }
}
  • Turn on shader compilation: Edit - Project Settings - Log Shader Compilation. Create Development Build. Run it on the target device where the shader doesn't work. The console will write the cause of the error in the shader. – viruseg Jun 03 '22 at 18:38

0 Answers0