Shader "Custom/Custom Lit"
{
    Properties
    {

    }

    SubShader
    {
        Tags{"RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline"}

        HLSLINCLUDE
        #include "Packages\com.unity.render-pipelines.universal\ShaderLibrary\Core.hlsl"
        #include "Packages\com.unity.render-pipelines.universal\ShaderLibrary\Lighting.hlsl"

        CBUFFER_START(UnityPerMaterial)
        CBUFFER_END

        struct VertexInput
        {
            float4 pos : POSITION;
            float3 normal : NORMAL;
            float2 uv : TEXCOORD0;
        };

        struct VertexOutput
        {
            float4 pos : SV_POSITION;
            float3 normal : TEXCOORD1;
            float2 uv : TEXCOORD0;
        };
        ENDHLSL

        Pass
        {
            Name "Forward Lit"
            Tags{"LightMode" = "UniversalForward"}

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            VertexOutput vert(VertexInput i)
            {
                VertexOutput o;

                o.pos = TransformObjectToHClip(i.pos.xyz);
                o.normal = i.normal;
                o.uv = i.uv;

                return o;
            }

            float4 frag(VertexOutput i) : SV_Target
            {
                float3 lightColor;
                float3 lightDir;

                float lightFalloff;
                float3 diffuseLight;
                float3 ambientLight = float3(0, 0, 0);

                uint lightCount = GetAdditionalLightsCount();
                for(uint lightIndex = 0; lightIndex < lightCount; lightIndex++)
                {
                    Light light = GetAdditionalLight(lightIndex, i.pos);

                    lightColor = light.color;
                    lightDir = light.direction;

                    lightFalloff = saturate(dot(i.normal, lightDir));
                }

                diffuseLight = lightColor * lightFalloff;
                return float4(ambientLight + diffuseLight, 0);
            }
            ENDHLSL
        }
    }
}