texture gTexture0 < string textureState="0,Texture"; >;
float4x4 gWorld : WORLD;
float4x4 gWorldViewProjection : WORLDVIEWPROJECTION;
float4 gGlobalAmbient < string renderState="AMBIENT"; >;
int gDiffuseMaterialSource < string renderState="DIFFUSEMATERIALSOURCE"; >;
int gAmbientMaterialSource < string renderState="AMBIENTMATERIALSOURCE"; >;
int gEmissiveMaterialSource < string renderState="EMISSIVEMATERIALSOURCE"; >;
float4 gMaterialAmbient < string materialState="Ambient"; >;
float4 gMaterialDiffuse < string materialState="Diffuse"; >;
float4 gMaterialEmissive < string materialState="Emissive"; >;
float3 gLightDirection : LIGHTDIRECTION;
float4 gLightDiffuse : LIGHTDIFFUSE;
float4 gLightAmbient : LIGHTAMBIENT;
bool state = false;
float4 color = float4(1, 0.9, 0.72, 1);
float brightness = 0.85;
float colorMul = 1;
float emissionScale = 1.8;

sampler gTex = sampler_state
{
    Texture = <gTexture0>;
    AddressU = Clamp;
    AddressV = Clamp;
};

struct LampVertex
{
    float3 position : POSITION0;
    float3 normal : NORMAL0;
    float4 diffuse : COLOR0;
    float2 uv : TEXCOORD0;
};

struct LampPixel
{
    float4 position : POSITION0;
    float4 diffuse : COLOR0;
    float2 uv : TEXCOORD0;
};

LampPixel HalogenVertex(LampVertex input)
{
    LampPixel output;
    output.position = mul(float4(input.position, 1), gWorldViewProjection);
    output.uv = input.uv;
    float3 normal = normalize(mul(input.normal, (float3x3)gWorld));
    float4 ambient = gAmbientMaterialSource == 0 ? gMaterialAmbient : input.diffuse;
    float4 diffuse = gDiffuseMaterialSource == 0 ? gMaterialDiffuse : input.diffuse;
    float4 emissive = gEmissiveMaterialSource == 0 ? gMaterialEmissive : input.diffuse;
    float direction = max(0, dot(normal, -gLightDirection));
    output.diffuse = saturate(ambient * (gGlobalAmbient + gLightAmbient) + diffuse * gLightDiffuse * direction + emissive);
    output.diffuse.a *= diffuse.a;
    return output;
}

float4 Halogen(LampPixel input) : COLOR0
{
    float2 uv = input.uv;
    float4 surface = tex2D(gTex, uv) * input.diffuse;
    if (state)
    {
        float2 p = (uv - float2(0.5, 0.5)) * float2(1.8, 1.7);
        float radius = dot(p, p);
        float filament = exp2(-radius * 10);
        float reflector = exp2(-radius * 3.5);
        float flutes = 0.76 + 0.14 * cos(uv.x * 125.66) + 0.10 * cos(uv.x * 61.8 + uv.y * 15.7);
        float emission = (0.95 * filament + 0.32 * reflector * flutes) * brightness * emissionScale;
        surface.rgb += color.rgb * emission * colorMul;
    }
    return surface;
}

technique HalogenLight
{
    pass P0
    {
        VertexShader = compile vs_2_0 HalogenVertex();
        PixelShader = compile ps_2_0 Halogen();
    }
}
