// Свет на поверхности и объём луча.
#ifndef LIGHT_COUNT
#define LIGHT_COUNT 2
#endif
#if NC_BEAM_VOLUME == 0
float4x4 gWorld : WORLD;
float4x4 gWorldViewProjection : WORLDVIEWPROJECTION;
float3 gCameraPosition : CAMERAPOSITION;
int gDeclNormal < string vertexDeclState = "Normal"; >;
texture gTexture0 < string textureState = "0,Texture"; >;
sampler Road = sampler_state { Texture = <gTexture0>; };
float4 lightPosition[LIGHT_COUNT]; 
float4 lightForward[LIGHT_COUNT]; 
float4 lightRight[LIGHT_COUNT]; 
float4 lightUp[LIGHT_COUNT];
float4 lightColor[LIGHT_COUNT]; 
float lightHighBeam[LIGHT_COUNT]; 
float viewDistance = 65;
bool debugBeam = false;
struct Input { float3 position : POSITION0; float3 normal : NORMAL0; float2 uv : TEXCOORD0; };
struct Output { float4 position : POSITION0; float2 uv : TEXCOORD0; float3 world : TEXCOORD1; float3 normal : TEXCOORD2; };
Output VS(Input v) {
    Output o;
    o.position = mul(float4(v.position, 1), gWorldViewProjection);
    o.world = mul(float4(v.position, 1), gWorld).xyz;
    
    
    o.normal = gDeclNormal ? mul(v.normal, (float3x3)gWorld) : float3(0, 0, 0);
    o.uv = v.uv;
    return o;
}
float4 PS(Output i) : COLOR0 {
    float4 surface = tex2D(Road, i.uv);
    
    clip(surface.a-0.98);
    float3 illumination = 0;
    float3 geometric = cross(ddx(i.world), ddy(i.world));
    
    geometric *= dot(geometric, gCameraPosition-i.world) < 0 ? -1 : 1;
    float3 rawNormal = dot(i.normal, i.normal) > 0.00001 ? i.normal : geometric;
    float3 normal = rawNormal * rsqrt(max(dot(rawNormal, rawNormal), 0.00000001));
    float coverage = 0;
    [unroll] for (int n = 0; n < LIGHT_COUNT; n++) {
        float3 delta = i.world - lightPosition[n].xyz;
        float forward = dot(delta, lightForward[n].xyz);
        float distance = length(delta);
        float horizontal = dot(delta, lightRight[n].xyz) / max(forward, 0.1);
        float vertical = dot(delta, lightUp[n].xyz) / max(forward, 0.1);
        float spread = lightForward[n].w;
        float edge = 1 - smoothstep(spread * 0.55, spread, abs(horizontal));
        
        float cutoff = -0.018 + saturate(horizontal * 5) * 0.018;
        float beam = 1 - smoothstep(cutoff - lightRight[n].w, cutoff + lightRight[n].w, vertical);
        float highShape = 1-smoothstep(0.08,0.22,abs(vertical));
        beam = lerp(beam,highShape,lightHighBeam[n]);
        float nearFill = smoothstep(-0.85, -0.2, vertical);
        float falloff = pow(saturate(1 - distance / max(lightPosition[n].w, 0.1)), 2);
        falloff = lerp(falloff,pow(saturate(1-distance/max(lightPosition[n].w,0.1)),1.2),lightHighBeam[n]);
        float incidence = saturate(dot(normal, -delta / max(distance, 0.1)) * 2.5);
        float unblocked = 1-smoothstep(lightUp[n].w,lightUp[n].w+0.2,distance);
        falloff *= unblocked;
        float hotspot = exp(-horizontal*horizontal*18 - pow(vertical+lerp(0.06,0,lightHighBeam[n]),2)*75);
        illumination += lightColor[n].rgb * lightColor[n].w * (1 + hotspot) * edge * beam * nearFill * falloff * incidence * smoothstep(0.2, 1.5, forward);
        coverage += edge * beam * nearFill * falloff * smoothstep(0.2, 1.5, forward) * saturate(lightColor[n].w);
    }
    float fade = 1 - smoothstep(viewDistance * 0.75, viewDistance, length(i.world - gCameraPosition));
    
    if (debugBeam) return float4(float3(1, 0.1, 0.7) * saturate(coverage) * fade, 1);
    return float4(surface.rgb * min(illumination, 1.2) * fade, surface.a);
}
technique Headlights {
    pass P0 {
        VertexShader = compile vs_3_0 VS();
        PixelShader = compile ps_3_0 PS();
        AlphaBlendEnable = TRUE;
        SrcBlend = SRCALPHA;
        DestBlend = ONE;
        ZWriteEnable = FALSE;
        DepthBias = -0.0001;
    }
}
#elif NC_BEAM_VOLUME == 1
#ifndef VOLUME_STEPS
#define VOLUME_STEPS 6
#endif
float4x4 gWorldViewProjection : WORLDVIEWPROJECTION;
float3 gCameraPosition : CAMERAPOSITION;
float3 origin;
float3 axis;
float3 rightAxis;
float3 upAxis;
float beamLength = 16;
float3 beamColor = 1;
float intensity = 0.07;
bool highBeam = false;
float beamWidth = 0.55;
struct Input { float3 position : POSITION0; };
struct Output { float4 position : POSITION0; float3 world : TEXCOORD0; };
Output VS(Input i) {
    Output o; o.position = mul(float4(i.position, 1), gWorldViewProjection);
    o.world = i.position; return o;
}
float4 PS(Output i) : COLOR0 {
    float3 ray = normalize(i.world - gCameraPosition);
    float3 center = origin + axis * beamLength * 0.5;
    float radius = beamLength * 0.72;
    float3 oc = gCameraPosition - center;
    float b = dot(oc, ray);
    float h = b*b - dot(oc, oc) + radius*radius;
    clip(h);
    float nearT = max(0, -b - sqrt(h));
    float farT = max(nearT, -b + sqrt(h));
    float stepSize = (farT-nearT)/VOLUME_STEPS;
    float density = 0;
    [unroll] for (int n=0; n<VOLUME_STEPS; n++) {
        float3 delta = gCameraPosition + ray*(nearT+(n+0.5)*stepSize) - origin;
        float forward = dot(delta, axis);
        float horizontal = dot(delta, rightAxis)/max(forward, 0.05);
        float vertical = dot(delta, upAxis)/max(forward, 0.05);
        float width = exp(-horizontal*horizontal*18);
        float cutoff = 1-smoothstep(-0.012, 0.009, vertical);
        if (highBeam) cutoff = 1-smoothstep(0.08,0.22,abs(vertical));
        width *= 1-smoothstep(beamWidth*0.55,beamWidth,abs(horizontal));
        float below = smoothstep(-0.35, -0.08, vertical);
        density += width*cutoff*below*smoothstep(0.1, 0.7, forward)
            *pow(saturate(1-forward/beamLength),2)*stepSize;
    }
    float phase = 0.3 + 0.7*pow(saturate(dot(-ray,axis)),4);
    return float4(beamColor, saturate(density*intensity*phase));
}
technique Air {
    pass P0 {
        VertexShader = compile vs_3_0 VS(); PixelShader = compile ps_3_0 PS();
        AlphaBlendEnable = TRUE; SrcBlend = SRCALPHA; DestBlend = ONE;
        ZEnable = TRUE; ZWriteEnable = FALSE; CullMode = NONE; AlphaTestEnable = FALSE;
    }
}
#endif
