#include "../VertexLighting_VS.h"

// main֐
VS_OUTPUT main( VS_INPUT VSInput )
{
	VS_OUTPUT VSOutput ;
	int4 lBoneFloatIndex ;
	float4 lLocalWorldMatrix[ 3 ] ;
	float4 lWorldPosition ;
	float4 lViewPosition ;
	float3 lWorldNrm ;
	float3 lWorldTan ;
	float3 lWorldBin ;
	float3 lViewNrm ;
	float3 lViewTan ;
	float3 lViewBin ;
	float3 lBumpTemp ;
	float4 lTotalDiffuse ;
	float4 lTotalSpecular ;
	float3 lLightDir ;
	float lTotalGen ;
	float4 lLightTemp ;
	float lLightDistancePow2 ;
	float lLightGen ;
	float lLightDirectionCosA ;
	float3 lLightHalfVec ;
	float4 lLightLitParam ;
	float4 lLightLitDest ;
	float4 lDifColor;
#if USE_SPE
	float4 lSpeColor;
#endif
#if SHADOWMAP
	float4 lLViewPosition ;
#endif

#if ( ( BUMPMAP || PHONG ) && USE_SPE ) || SHADOWMAP
	VSOutput.V_to_Eye_ShadowMap3PosZ.xyzw = cfZeroOne.x;
#endif
	
	lDifColor = cfDifSpeSource.x > 0.5f ? VSInput.Diffuse : cfMaterial.Diffuse;
	
#if USE_SPE
	lSpeColor = cfDifSpeSource.y > 0.5f ? VSInput.Specular : cfMaterial.Specular;
#endif

#if ( BUMPMAP == 0 && PHONG ) || SM_3
	VSOutput.Normal_Fog = float4( 0.0f, 0.0f, 0.0f, 1.0f ) ;
#endif

	// _Wϊ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( Jn )

	#ifdef SKINMESH

		// XLbV

		// uhs̍쐬
		lBoneFloatIndex = VSInput.BlendIndices0 ;
		lLocalWorldMatrix[ 0 ]  = cfLocalWorldMatrix[ lBoneFloatIndex.x + 0 ] * VSInput.BlendWeight0.xxxx;
		lLocalWorldMatrix[ 1 ]  = cfLocalWorldMatrix[ lBoneFloatIndex.x + 1 ] * VSInput.BlendWeight0.xxxx;
		lLocalWorldMatrix[ 2 ]  = cfLocalWorldMatrix[ lBoneFloatIndex.x + 2 ] * VSInput.BlendWeight0.xxxx;

		lLocalWorldMatrix[ 0 ] += cfLocalWorldMatrix[ lBoneFloatIndex.y + 0 ] * VSInput.BlendWeight0.yyyy;
		lLocalWorldMatrix[ 1 ] += cfLocalWorldMatrix[ lBoneFloatIndex.y + 1 ] * VSInput.BlendWeight0.yyyy;
		lLocalWorldMatrix[ 2 ] += cfLocalWorldMatrix[ lBoneFloatIndex.y + 2 ] * VSInput.BlendWeight0.yyyy;

		lLocalWorldMatrix[ 0 ] += cfLocalWorldMatrix[ lBoneFloatIndex.z + 0 ] * VSInput.BlendWeight0.zzzz;
		lLocalWorldMatrix[ 1 ] += cfLocalWorldMatrix[ lBoneFloatIndex.z + 1 ] * VSInput.BlendWeight0.zzzz;
		lLocalWorldMatrix[ 2 ] += cfLocalWorldMatrix[ lBoneFloatIndex.z + 2 ] * VSInput.BlendWeight0.zzzz;

		lLocalWorldMatrix[ 0 ] += cfLocalWorldMatrix[ lBoneFloatIndex.w + 0 ] * VSInput.BlendWeight0.wwww;
		lLocalWorldMatrix[ 1 ] += cfLocalWorldMatrix[ lBoneFloatIndex.w + 1 ] * VSInput.BlendWeight0.wwww;
		lLocalWorldMatrix[ 2 ] += cfLocalWorldMatrix[ lBoneFloatIndex.w + 2 ] * VSInput.BlendWeight0.wwww;

		#ifdef BONE8

			lBoneFloatIndex = VSInput.BlendIndices1 ;
			lLocalWorldMatrix[ 0 ] += cfLocalWorldMatrix[ lBoneFloatIndex.x + 0 ] * VSInput.BlendWeight1.xxxx;
			lLocalWorldMatrix[ 1 ] += cfLocalWorldMatrix[ lBoneFloatIndex.x + 1 ] * VSInput.BlendWeight1.xxxx;
			lLocalWorldMatrix[ 2 ] += cfLocalWorldMatrix[ lBoneFloatIndex.x + 2 ] * VSInput.BlendWeight1.xxxx;

			lLocalWorldMatrix[ 0 ] += cfLocalWorldMatrix[ lBoneFloatIndex.y + 0 ] * VSInput.BlendWeight1.yyyy;
			lLocalWorldMatrix[ 1 ] += cfLocalWorldMatrix[ lBoneFloatIndex.y + 1 ] * VSInput.BlendWeight1.yyyy;
			lLocalWorldMatrix[ 2 ] += cfLocalWorldMatrix[ lBoneFloatIndex.y + 2 ] * VSInput.BlendWeight1.yyyy;

			lLocalWorldMatrix[ 0 ] += cfLocalWorldMatrix[ lBoneFloatIndex.z + 0 ] * VSInput.BlendWeight1.zzzz;
			lLocalWorldMatrix[ 1 ] += cfLocalWorldMatrix[ lBoneFloatIndex.z + 1 ] * VSInput.BlendWeight1.zzzz;
			lLocalWorldMatrix[ 2 ] += cfLocalWorldMatrix[ lBoneFloatIndex.z + 2 ] * VSInput.BlendWeight1.zzzz;

			lLocalWorldMatrix[ 0 ] += cfLocalWorldMatrix[ lBoneFloatIndex.w + 0 ] * VSInput.BlendWeight1.wwww;
			lLocalWorldMatrix[ 1 ] += cfLocalWorldMatrix[ lBoneFloatIndex.w + 1 ] * VSInput.BlendWeight1.wwww;
			lLocalWorldMatrix[ 2 ] += cfLocalWorldMatrix[ lBoneFloatIndex.w + 2 ] * VSInput.BlendWeight1.wwww;

		#endif // BONE8

	#endif	// SKINMESH

	#ifdef TOONOUTLINE

		// WvZ( [J[h )
		lWorldPosition.x = dot( VSInput.Position, LOCAL_WORLD_MAT[ 0 ] ) ;
		lWorldPosition.y = dot( VSInput.Position, LOCAL_WORLD_MAT[ 1 ] ) ;
		lWorldPosition.z = dot( VSInput.Position, LOCAL_WORLD_MAT[ 2 ] ) ;
		lWorldPosition.w = 1.0f ;

		lWorldNrm.x = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 0 ].xyz ) ;
		lWorldNrm.y = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 1 ].xyz ) ;
		lWorldNrm.z = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 2 ].xyz ) ;

		// ֊s̑@Ɉړ
		lWorldPosition.xyz += lWorldNrm * cfToonOutLineSize.x ;

		// [hr[
		lViewPosition.x = dot( lWorldPosition, cfViewMatrix[ 0 ] ) ;
		lViewPosition.y = dot( lWorldPosition, cfViewMatrix[ 1 ] ) ;
		lViewPosition.z = dot( lWorldPosition, cfViewMatrix[ 2 ] ) ;
		lViewPosition.w = 1.0f ;

		// r[vWFNV
		VSOutput.Position.x = dot( lViewPosition, cfProjectionMatrix[ 0 ] ) ;
		VSOutput.Position.y = dot( lViewPosition, cfProjectionMatrix[ 1 ] ) ;
		VSOutput.Position.z = dot( lViewPosition, cfProjectionMatrix[ 2 ] ) ;
		VSOutput.Position.w = dot( lViewPosition, cfProjectionMatrix[ 3 ] ) ;

	#else  // TOONOUTLINE

		// WvZ( [Jr[vWFNV )
		lWorldPosition.x = dot( VSInput.Position, LOCAL_WORLD_MAT[ 0 ] ) ;
		lWorldPosition.y = dot( VSInput.Position, LOCAL_WORLD_MAT[ 1 ] ) ;
		lWorldPosition.z = dot( VSInput.Position, LOCAL_WORLD_MAT[ 2 ] ) ;
		lWorldPosition.w = 1.0f ;

		lViewPosition.x = dot( lWorldPosition, cfViewMatrix[ 0 ] ) ;
		lViewPosition.y = dot( lWorldPosition, cfViewMatrix[ 1 ] ) ;
		lViewPosition.z = dot( lWorldPosition, cfViewMatrix[ 2 ] ) ;
		lViewPosition.w = 1.0f ;

		VSOutput.Position.x = dot( lViewPosition, cfProjectionMatrix[ 0 ] ) ;
		VSOutput.Position.y = dot( lViewPosition, cfProjectionMatrix[ 1 ] ) ;
		VSOutput.Position.z = dot( lViewPosition, cfProjectionMatrix[ 2 ] ) ;
		VSOutput.Position.w = dot( lViewPosition, cfProjectionMatrix[ 3 ] ) ;

	#endif // TOONOUTLINE

	#ifdef BUMPMAP

		// ]@AڐA@r[Ԃɓe
		lWorldTan.x = dot( VSInput.Tan, LOCAL_WORLD_MAT[ 0 ].xyz ) ;
		lWorldTan.y = dot( VSInput.Tan, LOCAL_WORLD_MAT[ 1 ].xyz ) ;
		lWorldTan.z = dot( VSInput.Tan, LOCAL_WORLD_MAT[ 2 ].xyz ) ;

		lWorldBin.x = dot( VSInput.Bin, LOCAL_WORLD_MAT[ 0 ].xyz ) ;
		lWorldBin.y = dot( VSInput.Bin, LOCAL_WORLD_MAT[ 1 ].xyz ) ;
		lWorldBin.z = dot( VSInput.Bin, LOCAL_WORLD_MAT[ 2 ].xyz ) ;

		lWorldNrm.x = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 0 ].xyz ) ;
		lWorldNrm.y = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 1 ].xyz ) ;
		lWorldNrm.z = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 2 ].xyz ) ;

		lViewTan.x = dot( lWorldTan, cfViewMatrix[ 0 ].xyz ) ;
		lViewTan.y = dot( lWorldTan, cfViewMatrix[ 1 ].xyz ) ;
		lViewTan.z = dot( lWorldTan, cfViewMatrix[ 2 ].xyz ) ;

		lViewBin.x = dot( lWorldBin, cfViewMatrix[ 0 ].xyz ) ;
		lViewBin.y = dot( lWorldBin, cfViewMatrix[ 1 ].xyz ) ;
		lViewBin.z = dot( lWorldBin, cfViewMatrix[ 2 ].xyz ) ;

		lViewNrm.x = dot( lWorldNrm, cfViewMatrix[ 0 ].xyz ) ;
		lViewNrm.y = dot( lWorldNrm, cfViewMatrix[ 1 ].xyz ) ;
		lViewNrm.z = dot( lWorldNrm, cfViewMatrix[ 2 ].xyz ) ;

	#endif	// BUMPMAP


	// _Wϊ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( I )

	#if LG_USE == 0 || BUMPMAP == 1 || PHONG == 1

		// CggpȂAov}bv̓tHVF[fBȌꍇ̓}eA( or _ )J[̂܂܏o
		VSOutput.Diffuse  = lDifColor ;

		#if USE_SPE
			VSOutput.Specular = lSpeColor ;
		#endif
		
		#if BUMPMAP == 0 && PHONG
		
			// @r[Ԃ̊pxɕϊ( LOCAL_WORLD_MAT ̓XLjObV ̏ꍇ r0 ĝ mov r0, ZERO ɎsKv )
			lWorldNrm.x = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 0 ].xyz ) ;
			lWorldNrm.y = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 1 ].xyz ) ;
			lWorldNrm.z = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 2 ].xyz ) ;

			lViewNrm.x = dot( lWorldNrm, cfViewMatrix[ 0 ].xyz ) ;
			lViewNrm.y = dot( lWorldNrm, cfViewMatrix[ 1 ].xyz ) ;
			lViewNrm.z = dot( lWorldNrm, cfViewMatrix[ 2 ].xyz ) ;

			// @ۑ
			VSOutput.Normal_Fog.xyz = lViewNrm ;

		#endif // PHONG

	#endif // LG_USE == 0 || BUMPMAP == 1 || PHONG == 1





	// Cg̏ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( Jn )
	#if LG_USE

		#ifdef BUMPMAP

			#if USE_SPE
				// _W王_ւ̃xNgڒԂɓe㐳Kĕۑ
				lBumpTemp.x = dot( lViewTan, -lViewPosition.xyz ) ;
				lBumpTemp.y = dot( lViewBin, -lViewPosition.xyz ) ;
				lBumpTemp.z = dot( lViewNrm, -lViewPosition.xyz ) ;
				VSOutput.V_to_Eye_ShadowMap3PosZ.xyz = normalize( lBumpTemp ) ;
			#endif

		#else // BUMPMAP 

			// @r[Ԃ̊pxɕϊ( LOCAL_WORLD_MAT ̓XLjObV ̏ꍇ r0 ĝ mov r0, ZERO ɎsKv )
			lWorldNrm.x = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 0 ].xyz ) ;
			lWorldNrm.y = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 1 ].xyz ) ;
			lWorldNrm.z = dot( VSInput.Normal, LOCAL_WORLD_MAT[ 2 ].xyz ) ;

			lViewNrm.x = dot( lWorldNrm, cfViewMatrix[ 0 ].xyz ) ;
			lViewNrm.y = dot( lWorldNrm, cfViewMatrix[ 1 ].xyz ) ;
			lViewNrm.z = dot( lWorldNrm, cfViewMatrix[ 2 ].xyz ) ;

			// fBt[YJ[ƃXyLJ[̒~ϒl̏
			lTotalDiffuse = float4( 0, 0, 0, 0 ) ;
			lTotalSpecular = float4( 0, 0, 0, 0 ) ;

			#ifdef PHONG

				#if USE_SPE
					// _W王_ւ̃xNg𐳋Kĕۑ
					VSOutput.V_to_Eye_ShadowMap3PosZ.xyz = normalize( -lViewPosition.xyz ) ;
				#endif

				// @ۑ
				VSOutput.Normal_Fog.xyz = lViewNrm ;

			#endif // PHONG

		#endif // BUMPMAP







		%MV1Default_VertexLighting_VS_Light.txt%$0$
		%MV1Default_VertexLighting_VS_Light.txt%$1$
		%MV1Default_VertexLighting_VS_Light.txt%$2$







		// SẴfBt[Yvf킹ďo̓WX^ɃZbg =====( Jn )

		#if BUMPMAP == 0 && PHONG == 0

			// o̓fBt[YJ[ = CgfBt[YJ[~ϒl + ( }eÃArGgJ[ƃO[oArGgJ[Ẑƃ}eAG~bVuJ[Z )
			VSOutput.Diffuse = lTotalDiffuse + cfAmbient_Emissive ;

			// At@̓fBt[YJ[̃At@̂܂܎g
			VSOutput.Diffuse.w = lDifColor.w ;

		#endif // BUMPMAP == 0 && PHONG == 0

		// SẴfBt[Yvf킹ďo̓WX^ɃZbg =====( I )





		// SẴXyLvf킹ďo̓WX^ɃZbg =====( Jn )

		#if USE_SPE && BUMPMAP == 0 && PHONG == 0

			// o̓XyLJ[ = CgXyLJ[~ϒl * XyLJ[
			VSOutput.Specular = lTotalSpecular * lSpeColor ;

		#endif // USE_SPE && BUMPMAP == 0 && PHONG == 0

		// SẴXyLvf킹ďo̓WX^ɃZbg =====( I )






	#endif // LG_USE
	// Cg̏ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( I )















	// tHOvZ =============================================( Jn )

	#if FOG_LINEAR || FOG_EXP || FOG_EXP2

		#ifdef FOG_LINEAR

			// `tHOvZ
			FOG_OUT = lViewPosition.z * FOG_LINEAR_DIV + FOG_LINEAR_ADD ;

		#endif

		#ifdef FOG_EXP

			// wtHOvZ 1.0f / pow( e,  * density )
			FOG_OUT = 1.0f / pow( abs( FOG_EXP_E ), lViewPosition.z * FOG_EXP_DENSITY ) ;

		#endif

		#ifdef FOG_EXP2

			// wtHOQvZ 1.0f / pow( e, (  * density ) * (  * density ) )
			FOG_OUT = 1.0f / pow( abs( FOG_EXP_E ), ( lViewPosition.z * FOG_EXP_DENSITY ) * ( lViewPosition.z * FOG_EXP_DENSITY ) ) ;

		#endif

	#endif // FOG_LINEAR || FOG_EXP || FOG_EXP2

	// tHOvZ =============================================( I )

#if SHADOWMAP_DRAW
	// ˉeWeNX`WƂďo͂ =================( Jn )

	VSOutput.ShadowMap1Pos_ShadowMap3PosX.xyz = VSOutput.Position.xyz ;
	VSOutput.ShadowMap1Pos_ShadowMap3PosX.w   = cfZeroOne.y;

	// ˉeWeNX`WƂďo͂ =================( I )
#endif


#if SHADOWMAP
	// [xep̃Cg猩ˉeWZo =================( Jn )

	// [hWCgP̎ˉeWɕϊ
	VSOutput.ShadowMap1Pos_ShadowMap3PosX.x = dot( lWorldPosition, cfShadowMap1LightViewProjectionMatrix[ 0 ] ) ;
	VSOutput.ShadowMap1Pos_ShadowMap3PosX.y = dot( lWorldPosition, cfShadowMap1LightViewProjectionMatrix[ 1 ] ) ;
	VSOutput.ShadowMap1Pos_ShadowMap3PosX.z = dot( lWorldPosition, cfShadowMap1LightViewProjectionMatrix[ 2 ] ) ;

	// [hWCgQ̎ˉeWɕϊ
	VSOutput.ShadowMap2Pos_ShadowMap3PosY.x = dot( lWorldPosition, cfShadowMap2LightViewProjectionMatrix[ 0 ] ) ;
	VSOutput.ShadowMap2Pos_ShadowMap3PosY.y = dot( lWorldPosition, cfShadowMap2LightViewProjectionMatrix[ 1 ] ) ;
	VSOutput.ShadowMap2Pos_ShadowMap3PosY.z = dot( lWorldPosition, cfShadowMap2LightViewProjectionMatrix[ 2 ] ) ;

	// [hWCgR̎ˉeWɕϊ
	VSOutput.ShadowMap1Pos_ShadowMap3PosX.w = dot( lWorldPosition, cfShadowMap3LightViewProjectionMatrix[ 0 ] ) ;
	VSOutput.ShadowMap2Pos_ShadowMap3PosY.w = dot( lWorldPosition, cfShadowMap3LightViewProjectionMatrix[ 1 ] ) ;
	VSOutput.V_to_Eye_ShadowMap3PosZ.w      = dot( lWorldPosition, cfShadowMap3LightViewProjectionMatrix[ 2 ] ) ;

	// [xep̃Cg猩ˉeWZo =================( I )
#endif

	// eNX`W̃Zbg
	VSOutput.TexCoords0_1.x = dot( VSInput.TexCoords0, cfTextureMatrix[ 0 ][ 0 ] ) ;
	VSOutput.TexCoords0_1.y = dot( VSInput.TexCoords0, cfTextureMatrix[ 0 ][ 1 ] ) ;

//#ifdef SUBTEXTURE
	VSOutput.TexCoords0_1.z = dot( VSInput.TexCoords1, cfTextureMatrix[ 0 ][ 0 ] ) ;
	VSOutput.TexCoords0_1.w = dot( VSInput.TexCoords1, cfTextureMatrix[ 0 ][ 1 ] ) ;
//#endif // SUBTEXTURE


	return VSOutput ;
}

