
//================================================================
//ݒl
//Texture
sampler sampler0_ : register(s0);

//--------------------------------
//䂪ݐpp[^
static const float RENDER_WIDTH = 1024; //_OeNX`̕
static const float RENDER_HEIGHT = 1024; //_OeNX`̍
static const float RENDER_LEFT = 32.9f/1024.0f;		//W+1
static const float RENDER_TOP = 16.9f/512.0f;		//W+1
static const float RENDER_RIGHT = 415.1f/1024.0f;	//EW-1
static const float RENDER_BOTTOM = 463.1f/512.0f;	//W-1


float frame_;		//t[
float waveRadius_;	//GtFNg̔a
float2 enemyPos_;	//G̈ʒu
float2 enemyPos2_;	//G̈ʒuQ̖
//float4 waveColor_;	//GtFNg̃J[


//================================================================
//--------------------------------
//sNZVF[_͒l
struct PS_INPUT
{
	float4 diffuse : COLOR0;  //fBt[YF
	float2 texCoord : TEXCOORD0; //eNX`W
	float2 vPos : VPOS; //`W
};

//--------------------------------
//sNZVF[_o͒l
struct PS_OUTPUT
{
    float4 color : COLOR0; //o͐F
};


//================================================================
// VF[_
//--------------------------------
//sNZVF[_
PS_OUTPUT PsWave( PS_INPUT In ) : COLOR0
{
	PS_OUTPUT Out;

	//--------------------------------
	//ʔ͈͊O͌vZȂ
	if (RENDER_LEFT > In.texCoord.x || RENDER_RIGHT < In.texCoord.x || RENDER_TOP > In.texCoord.y || RENDER_BOTTOM < In.texCoord.y)
	{
		//eNX`̐F
		float4 colorTexture = tex2D(sampler0_, In.diffuse);

		//_fBt[YF
		float4 colorDiffuse = In.diffuse;

		//
		float4 color = colorTexture * colorDiffuse;

		Out.color = color;
	}
	//--------------------------------
	else
	{
		// Q̑ΉAGORIOSHI!!!
		float biasX = 0.0f;
		float biasY = 0.0f;
		float powerRatio = 0.0f;
		float powerRatio2 = 0.0f;

		if (all(enemyPos_))
		{
			//炬vZ
			float dist2 = pow(In.vPos.x-enemyPos_.x ,2) + pow(In.vPos.y-enemyPos_.y ,2);
			float dist = sqrt(dist2);
			float sinTheta = (In.vPos.y - enemyPos_.y) / dist;
			float cosTheta = (In.vPos.x - enemyPos_.x) / dist;

			//cݍ쐬psinɎgppxp[^
			float angle = In.vPos.y - enemyPos_.y + In.vPos.x - enemyPos_.x + frame_;
			angle = radians(angle);

			//YsNZ̘c݂̔avZ
			//GtFNga1/16ő̘cݕƂ
			float waveRadius = waveRadius_ + waveRadius_/16 * (-1 + sin(angle));

			//S狗قǉe
			powerRatio = (waveRadius - dist) / waveRadius / 2;
			if (powerRatio < 0) {powerRatio = 0;}

			//F擾ʒũoCAXl
			float biasRadius = waveRadius * powerRatio;
			biasX += biasRadius * cosTheta;
			biasY += biasRadius * sinTheta;
		}
		if (all(enemyPos2_))
		{
			//炬vZ
			float dist2 = pow(In.vPos.x-enemyPos2_.x ,2) + pow(In.vPos.y-enemyPos2_.y ,2);
			float dist = sqrt(dist2);
			float sinTheta = (In.vPos.y - enemyPos2_.y) / dist;
			float cosTheta = (In.vPos.x - enemyPos2_.x) / dist;

			//cݍ쐬psinɎgppxp[^
			float angle = In.vPos.y - enemyPos2_.y + In.vPos.x - enemyPos2_.x + frame_;
			angle = radians(angle);

			//YsNZ̘c݂̔avZ
			//GtFNga1/16ő̘cݕƂ
			float waveRadius = waveRadius_ + waveRadius_/16 * (-1 + sin(angle));

			//S狗قǉe
			powerRatio2 = (waveRadius - dist) / waveRadius;
			if (powerRatio2 < 0) {powerRatio2 = 0;}

			//F擾ʒũoCAXl
			float biasRadius = waveRadius * powerRatio2;
			biasX += biasRadius * cosTheta;
			biasY += biasRadius * sinTheta;
		}

		//eNX`̐F擾ʒu
		//ʔ͈͊O擾ȏꍇ͉ʓɕ␳
		float2 texUV;
		texUV.x = min(RENDER_RIGHT,  max(RENDER_LEFT,
				-biasX / RENDER_WIDTH  + In.texCoord.x));
		texUV.y = min(RENDER_BOTTOM, max(RENDER_TOP,
				 -biasY / RENDER_HEIGHT + In.texCoord.y));

		//--------------------------------
		//eNX`̐F
		float4 colorTexture = tex2D(sampler0_, texUV);

		//_fBt[YF
		float4 colorDiffuse = In.diffuse;

		//
		float4 color = colorTexture * colorDiffuse;

		//Fω遨p~
//		powerRatio = max(powerRatio, powerRatio2);
//		powerRatio = min(1.0f, powerRatio*2.0f);
//		if (powerRatio > 0)
//		{
//			color.rgb *= (1.0f - (1.0f - waveColor_.rgb) * powerRatio);
//			color.rgb += waveColor_.rgb * powerRatio * 0.125f;
//		}

		Out.color = color;
	}

	return Out;
}


//================================================================
//--------------------------------
//technique
technique TecWave
{
	pass P0
	{
		PixelShader = compile ps_3_0 PsWave();
	}
}

