// 頂点シェーダーの入力 struct VS_INPUT { float4 Position : POSITION ; // 座標( ローカル空間 ) int4 BlendIndices0 : BLENDINDICES0 ; // スキニング処理用 Float型定数配列インデックス float4 BlendWeight0 : BLENDWEIGHT0 ; // スキニング処理用ウエイト値 float3 Normal : NORMAL0 ; // 法線( ローカル空間 ) float4 Diffuse : COLOR0 ; // ディフューズカラー float4 Specular : COLOR1 ; // スペキュラカラー float4 TexCoords0 : TEXCOORD0 ; // テクスチャ座標 } ; // 頂点シェーダーの出力 struct VS_OUTPUT { float4 Position : POSITION ; // 座標( 射影空間 ) float4 Diffuse : COLOR0 ; // ディフューズカラー float4 Specular : COLOR1 ; // スペキュラカラー float2 TexCoords0 : TEXCOORD0 ; // テクスチャ座標 float4 LPPosition : TEXCOORD1 ; // ライトからみた座標( ライトの射影空間 ) } ; // マテリアルパラメータ struct VS_CONST_MATERIAL { float4 Diffuse ; // マテリアルディフューズカラー float4 Specular ; // マテリアルスペキュラカラー float4 Power ; // マテリアルスペキュラハイライトの強さ } ; // ライトパラメータ struct VS_CONST_LIGHT { float4 Position ; // 座標( ビュー空間 ) float3 Direction ; // 方向( ビュー空間 ) float4 Diffuse ; // ディフューズカラー float4 Specular ; // スペキュラカラー float4 Ambient ; // アンビエントカラーとマテリアルのアンビエントカラーを乗算したもの float4 Range_FallOff_AT0_AT1 ; // x:有効距離 y:スポットライト用FallOff z:距離による減衰処理用パラメータ0 w:距離による減衰処理用パラメータ1 float4 AT2_SpotP0_SpotP1 ; // x:距離による減衰処理用パラメータ2 y:スポットライト用パラメータ0( cos( Phi / 2.0f ) ) z:スポットライト用パラメータ1( 1.0f / ( cos( Theta / 2.0f ) - cos( Phi / 2.0f ) ) ) } ; // C++ 側で設定する定数の定義 float4 cfAmbient_Emissive : register( c1 ) ; // マテリアルエミッシブカラー + マテリアルアンビエントカラー * グローバルアンビエントカラー float4 cfProjectionMatrix[ 4 ] : register( c2 ) ; // ビュー  → 射影行列 float4 cfViewMatrix[ 3 ] : register( c6 ) ; // ワールド → ビュー行列 float4 cfLocalWorldMatrix[ 162 ] : register( c94 ) ; // ローカル → ワールド行列 VS_CONST_MATERIAL cfMaterial : register( c11 ) ; // マテリアルパラメータ VS_CONST_LIGHT cfLight : register( c14 ) ; // 有効ライト0番のパラメータ matrix cfLightViewMatrix : register( c43 ) ; // ライトのワールド → ビュー行列 matrix cfLightProjectionMatrix : register( c47 ) ; // ライトのビュー  → 射影行列 // main関数 VS_OUTPUT main( VS_INPUT VSInput ) { VS_OUTPUT VSOutput ; float4 lWorldPosition ; float4 lViewPosition ; float4 lLViewPosition ; float3 lWorldNrm ; float3 lViewNrm ; float3 lLightHalfVec ; float4 lLightLitParam ; float4 lLightLitDest ; float4 lLocalWorldMatrix[ 3 ] ; // 複数のフレームのブレンド行列の作成 lLocalWorldMatrix[ 0 ] = cfLocalWorldMatrix[ VSInput.BlendIndices0.x + 0 ] * VSInput.BlendWeight0.x; lLocalWorldMatrix[ 1 ] = cfLocalWorldMatrix[ VSInput.BlendIndices0.x + 1 ] * VSInput.BlendWeight0.x; lLocalWorldMatrix[ 2 ] = cfLocalWorldMatrix[ VSInput.BlendIndices0.x + 2 ] * VSInput.BlendWeight0.x; lLocalWorldMatrix[ 0 ] += cfLocalWorldMatrix[ VSInput.BlendIndices0.y + 0 ] * VSInput.BlendWeight0.y; lLocalWorldMatrix[ 1 ] += cfLocalWorldMatrix[ VSInput.BlendIndices0.y + 1 ] * VSInput.BlendWeight0.y; lLocalWorldMatrix[ 2 ] += cfLocalWorldMatrix[ VSInput.BlendIndices0.y + 2 ] * VSInput.BlendWeight0.y; lLocalWorldMatrix[ 0 ] += cfLocalWorldMatrix[ VSInput.BlendIndices0.z + 0 ] * VSInput.BlendWeight0.z; lLocalWorldMatrix[ 1 ] += cfLocalWorldMatrix[ VSInput.BlendIndices0.z + 1 ] * VSInput.BlendWeight0.z; lLocalWorldMatrix[ 2 ] += cfLocalWorldMatrix[ VSInput.BlendIndices0.z + 2 ] * VSInput.BlendWeight0.z; lLocalWorldMatrix[ 0 ] += cfLocalWorldMatrix[ VSInput.BlendIndices0.w + 0 ] * VSInput.BlendWeight0.w; lLocalWorldMatrix[ 1 ] += cfLocalWorldMatrix[ VSInput.BlendIndices0.w + 1 ] * VSInput.BlendWeight0.w; lLocalWorldMatrix[ 2 ] += cfLocalWorldMatrix[ VSInput.BlendIndices0.w + 2 ] * VSInput.BlendWeight0.w; // 頂点座標変換 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( 開始 ) // ローカル座標をワールド座標に変換 lWorldPosition.x = dot( VSInput.Position, lLocalWorldMatrix[ 0 ] ) ; lWorldPosition.y = dot( VSInput.Position, lLocalWorldMatrix[ 1 ] ) ; lWorldPosition.z = dot( VSInput.Position, lLocalWorldMatrix[ 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 ] ) ; // 頂点座標変換 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( 終了 ) // ライトの処理 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( 開始 ) // 法線をビュー空間の角度に変換 =========================================( 開始 ) // ローカルベクトルをワールドベクトルに変換 lWorldNrm.x = dot( VSInput.Normal, lLocalWorldMatrix[ 0 ].xyz ) ; lWorldNrm.y = dot( VSInput.Normal, lLocalWorldMatrix[ 1 ].xyz ) ; lWorldNrm.z = dot( VSInput.Normal, lLocalWorldMatrix[ 2 ].xyz ) ; // ワールドベクトルをビューベクトルに変換 lViewNrm.x = dot( lWorldNrm, cfViewMatrix[ 0 ].xyz ) ; lViewNrm.y = dot( lWorldNrm, cfViewMatrix[ 1 ].xyz ) ; lViewNrm.z = dot( lWorldNrm, cfViewMatrix[ 2 ].xyz ) ; // 法線をビュー空間の角度に変換 =========================================( 終了 ) // ライトディフューズカラーとライトスペキュラカラーの角度減衰計算 =======( 開始 ) // 法線とライトの逆方向ベクトルとの内積を lLightLitParam.x にセット lLightLitParam.x = dot( lViewNrm, -cfLight.Direction ) ; // ハーフベクトルの計算 norm( ( norm( 頂点位置から視点へのベクトル ) + ライトの方向 ) ) lLightHalfVec = normalize( normalize( -lViewPosition.xyz ) - cfLight.Direction ) ; // 法線とハーフベクトルの内積を lLightLitParam.y にセット lLightLitParam.y = dot( lLightHalfVec, lViewNrm ) ; // スペキュラ反射率を lLightLitParam.w にセット lLightLitParam.w = cfMaterial.Power.x ; // ライトパラメータ計算 lLightLitDest = lit( lLightLitParam.x, lLightLitParam.y, lLightLitParam.w ) ; // ライトディフューズカラーとライトスペキュラカラーの角度減衰計算 =======( 終了 ) // ライトの処理 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( 終了 ) // 出力パラメータセット ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( 開始 ) // ディフューズカラー = // ディフューズ角度減衰計算結果 * // ライトのディフューズカラー * // マテリアルのディフューズカラー + // ライトのアンビエントカラーとマテリアルのアンビエントカラーを乗算したもの + // マテリアルのアンビエントカラーとグローバルアンビエントカラーを乗算したものとマテリアルエミッシブカラーを加算したもの VSOutput.Diffuse = lLightLitDest.y * cfLight.Diffuse * cfMaterial.Diffuse + cfLight.Ambient + cfAmbient_Emissive ; // ディフューズアルファはマテリアルのディフューズカラーのアルファをそのまま使う VSOutput.Diffuse.w = cfMaterial.Diffuse.w ; // スペキュラカラー = スペキュラ角度減衰計算結果 * ライトのスペキュラカラー * マテリアルのスペキュラカラー VSOutput.Specular = lLightLitDest.z * cfLight.Specular * cfMaterial.Specular ; // テクスチャ座標のセット VSOutput.TexCoords0 = VSInput.TexCoords0; // 出力パラメータセット ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( 終了 ) // 深度影用のライトから見た射影座標を算出 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( 開始 ) // ワールド座標をライトのビュー座標に変換 lLViewPosition = mul( cfLightViewMatrix, lWorldPosition ) ; // ライトのビュー座標をライトの射影座標に変換 VSOutput.LPPosition = mul( cfLightProjectionMatrix, lLViewPosition ) ; // Z値だけはライトのビュー座標にする VSOutput.LPPosition.z = lLViewPosition.z ; // 深度影用のライトから見た射影座標を算出 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++( 終了 ) // 出力パラメータを返す return VSOutput ; }