/*
    Scanlines Sine Absolute Value
    An ultra light scanline shader
    by RiskyJumps
	license: public domain
*/

#pragma parameter amp          "Amplitude"      1.2500  0.000 2.000 0.05
#pragma parameter phase        "Phase"          0.5000  0.000 2.000 0.05
#pragma parameter lines_black  "Lines Blacks"   0.0000  0.000 1.000 0.05
#pragma parameter lines_white  "Lines Whites"   1.0000  0.000 2.000 0.05
 
#ifdef PARAMETER_UNIFORM
uniform float amp;
uniform float phase;
uniform float lines_black;
uniform float lines_white;
#else
#define amp              1.250000
#define phase            0.500000
#define lines_black      0.000000
#define lines_white      1.000000
#endif
 
#define freq             0.500000
#define offset           0.000000
#define pi               3.141592654
 
/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
   - FX11 compilers
*/

#include "../compat_includes.inc"
uniform COMPAT_Texture2D(decal) : TEXUNIT0;
uniform float4x4 modelViewProj;

struct out_vertex
{
	float4 position : COMPAT_POS;
	float2 texCoord : TEXCOORD0;
	float  angle    : TEXCOORD1;
#ifndef HLSL_4
	float4 Color    : COLOR;
#endif
};

out_vertex main_vertex(COMPAT_IN_VERTEX)
{
	out_vertex OUT;
#ifdef HLSL_4
	float4 position = VIN.position;
	float2 texCoord = VIN.texCoord;
#else
	OUT.Color = color;
#endif
	OUT.position = mul(modelViewProj, position);
	OUT.texCoord = texCoord;
	float omega = 2.0 * pi * freq;  // Angular frequency
 
    // NOTE: I can't calculate lines on vertex, because of sin()
    /*
    lines = texCoord.y * omega * COMPAT_texture_size.y + phase;
    lines = sin(lines);                     // The use of sin() in here
                                            // yields a black screen...
    lines *= amp;
    lines += offset;
    lines = abs(lines);
    */
 
    OUT.angle = texCoord.y * omega * COMPAT_texture_size.y + phase;
	
	return OUT;
} 
 
float4 scanline_sine_abs(float angle, float2 texCoord, COMPAT_Texture2D(decal))
{
    float3 color = COMPAT_Sample(decal, texCoord).xyz;
    float grid;
 
    float lines;
 
    lines = sin(angle);
    lines *= amp;
    lines += offset;
    lines = abs(lines) * (lines_white - lines_black) + lines_black;
    color *= lines;
 
    return color.xyzz;
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return scanline_sine_abs(VOUT.angle, VOUT.texCoord, decal);
}
COMPAT_END