#version 300 es

// Shader below was causing invalid matrix assignment (different types, i.e. half3x3 vs float3x3)
// in the resulting Metal code. Caused by some combination of matrix transposes and other operations
// on them.

mat3 xll_transpose_mf3x3(mat3 m) {
  return mat3( m[0][0], m[1][0], m[2][0],
               m[0][1], m[1][1], m[2][1],
               m[0][2], m[1][2], m[2][2]);
}
vec3 xll_matrixindex_mf3x3_i (mat3 m, int i) { vec3 v; v.x=m[0][i]; v.y=m[1][i]; v.z=m[2][i]; return v; }
struct SurfaceOutput {
    lowp vec3 Albedo;
    lowp vec3 Normal;
    lowp vec3 Emission;
    mediump float Specular;
    lowp float Gloss;
    lowp float Alpha;
};
struct Input {
    highp vec2 uv_PanelTex;
    highp vec2 uv2_DecalTex;
};
struct v2f_surf {
    highp vec4 pos;
    highp vec4 pack0;
    highp vec4 tSpace0;
    highp vec4 tSpace1;
    highp vec4 tSpace2;
};
uniform highp vec4 _WorldSpaceLightPos0;
uniform sampler2D _PanelNorm;
uniform sampler2D _DecalNorm;
highp vec3 UnityWorldSpaceLightDir( in highp vec3 worldPos ) {
    return (_WorldSpaceLightPos0.xyz - (worldPos * _WorldSpaceLightPos0.w));
}
void surf( in Input IN, inout SurfaceOutput o ) {
   
    lowp vec3 baseNormal = ((texture( _DecalNorm, IN.uv2_DecalTex).xyz * 2.0) - 1.0);
    lowp vec3 detailNormal = ((texture( _PanelNorm, IN.uv_PanelTex).xyz * 2.0) - 1.0);
    highp mat3 nBasis = xll_transpose_mf3x3(mat3( vec3( baseNormal.z, baseNormal.y, (-baseNormal.x)), vec3( baseNormal.x, baseNormal.z, (-baseNormal.y)), vec3( baseNormal.x, baseNormal.y, baseNormal.z)));
   
    lowp vec3 combinedNormal = normalize((((detailNormal.x * xll_matrixindex_mf3x3_i (nBasis, 0)) + (detailNormal.y * xll_matrixindex_mf3x3_i (nBasis, 1))) + (detailNormal.z * xll_matrixindex_mf3x3_i (nBasis, 2))));
    combinedNormal *= 1.0;
    o.Normal = combinedNormal;
}
lowp vec4 frag_surf( in v2f_surf IN ) {
    Input surfIN;
   
    surfIN.uv_PanelTex = IN.pack0.xy;
    surfIN.uv2_DecalTex = IN.pack0.zw;
    highp vec3 worldPos = vec3( IN.tSpace0.w, IN.tSpace1.w, IN.tSpace2.w);
   
    lowp vec3 lightDir = normalize(UnityWorldSpaceLightDir( worldPos));
   
    SurfaceOutput o;
    o.Albedo = vec3( 0.0);
    o.Emission = vec3( 0.0);
   
    o.Specular = 0.0;
    o.Alpha = 0.0;
    o.Gloss = 0.0;
    lowp vec3 normalWorldVertex = vec3( 0.0, 0.0, 1.0);
   
    surf( surfIN, o);
    lowp vec3 worldN;
    worldN.x = dot( IN.tSpace0.xyz, o.Normal);
    worldN.y = dot( IN.tSpace1.xyz, o.Normal);
   
    worldN.z = dot( IN.tSpace2.xyz, o.Normal);
    o.Normal = worldN;
   
    lowp vec4 res;
    res.xyz = ((o.Normal * 0.5) + 0.5);
    res.w = o.Specular;
    return res;
}
in highp vec4 xlv_TEXCOORD0;
in highp vec4 xlv_TEXCOORD1;
in highp vec4 xlv_TEXCOORD2;
in highp vec4 xlv_TEXCOORD3;
out lowp vec4 _fragData;
void main() {
    lowp vec4 xl_retval;
    v2f_surf xlt_IN;
    xlt_IN.pos = vec4(0.0);
    xlt_IN.pack0 = vec4(xlv_TEXCOORD0);
    xlt_IN.tSpace0 = vec4(xlv_TEXCOORD1);
    xlt_IN.tSpace1 = vec4(xlv_TEXCOORD2);
    xlt_IN.tSpace2 = vec4(xlv_TEXCOORD3);
    xl_retval = frag_surf( xlt_IN);
    _fragData = vec4(xl_retval);
}
