あなたの好きなようにエフェクトを修正することができます。法線マップ、ライティングアルゴリズムなどを追加して下さい。

  • エフェクトファイルに、最初にこれから使用する変数を宣言します。
  1. // These are required by the Animation library
  2. float4x4 MatrixPalette[56];
  3. float4x4 World;
  4.  
  5. // These are not
  6. float4x4 View;
  7. float4x4 Projection;
  8. texture BasicTexture;
  9.  
  10. // Stores sampler state info for the texture
  11. sampler TextureSampler = sampler_state
  12. {
  13. Texture = (BasicTexture);
  14. };
  15.  

頂点シェーダの作成
  • エフェクトの頂点シェーダを作成します。最初に XNA から頂点シェーダに渡す構造体を宣言します。
  1. // This is passed into our vertex shader from Xna
  2. struct VS_INPUT
  3. {
  4. // This is the position of the vertex in the model file
  5. float4 position : POSITION;
  6. // The vertex normal
  7. float3 normal : NORMAL0;
  8. // This is the texture coordinate for the vertex in the model file
  9. float2 texcoord : TEXCOORD0;
  10. // These are the indices (4 of them) that index the bones that affect
  11. // this vertex. The indices refer to the MatrixPalette.
  12. half4 indices : BLENDINDICES0;
  13. // These are the weights (4 of them) that determine how much each bone
  14. // affects this vertex.
  15. float4 weights : BLENDWEIGHT0;
  16. };
  17.  
  • 次に頂点シェーダからハードウェアに渡す構造体を作成して下さい。
  1. // This is passed out from our vertex shader once we have processed the input
  2. struct VS_OUTPUT
  3. {
  4. // The final position of the vertex in world space
  5. float4 position : POSITION;
  6. // The texture coordinate associated with the vertex
  7. float2 texcoord : TEXCOORD0;
  8. };
  9.  
  • 後でモデルのボーンによって座標変換させる頂点位置を保存する構造体を作成します。
  1. // This is the output from our skinning method
  2. struct SKIN_OUTPUT
  3. {
  4. float4 position;
  5. float4 normal;
  6. };
  7.  
  • 次にボーンによって頂点位置を座標変換するメソッドを作成して下さい。
  1. // This method takes in a vertex and applies the bone transforms to it.
  2. SKIN_OUTPUT Skin4( const VS_INPUT input)
  3. {
  4. SKIN_OUTPUT output = (SKIN_OUTPUT)0;
  5. // Since the weights need to add up to one, store 1.0 - (sum of the weights)
  6. float lastWeight = 1.0;
  7. float weight = 0;
  8. // Apply the transforms for the first 3 weights
  9. for (int i = 0; i < 3; ++i)
  10. {
  11. weight = input.weights[i];
  12. lastWeight -= weight;
  13. output.position += mul( input.position, MatrixPalette[input.indices[i]]) * weight;
  14. output.normal += mul( input.normal, MatrixPalette[input.indices[i]]) * weight;
  15. }
  16. // Apply the transform for the last weight
  17. output.position += mul( input.position, MatrixPalette[input.indices[3]])*lastWeight;
  18. output.normal += mul( input.normal, MatrixPalette[input.indices[3]])*lastWeight;
  19. return output;
  20. };
  21.  
  • 最後に頂点シェーダメソッドを作成することができます。
  1. void TransformVertex (in VS_INPUT input, out VS_OUTPUT output)
  2. {
  3. // Calculate the skinned position
  4. SKIN_OUTPUT skin = Skin4(input);
  5. // This is the final position of the vertex, and where it will be drawn on the screen
  6. float4x4 WorldViewProjection = mul(World,mul(View,Projection));
  7. output.position = mul(skin.position, WorldViewProjection);
  8. // This is not used by is included to demonstrate how to get the normal in world space
  9. float4 transformedNormal = mul(skin.normal, WorldViewProjection);
  10. output.texcoord = input.texcoord;
  11. }
  12.  

ピクセルシェーダの作成
  • 頂点シェーダと同じように、新しい構造体を宣言する必要があります。最初にピクセルシェーダからハードウェアに渡す構造体(頂点シェーダによって処理された後に生成されます)を宣言します。
  1. // This is passed into our pixel shader
  2. struct PS_INPUT
  3. {
  4. float2 texcoord : TEXCOORD0;
  5. };
  6.  
  • ピクセルシェーダから画面スクリーンに渡す構造体を宣言します。
  1. // This is the final color rendered on the screen
  2. struct PS_OUTPUT
  3. {
  4. float4 color : COLOR;
  5. };
  6.  
  • ピクセルシェーダはシンプルに特別なライティング処理は行わないようにしています。
  1. void TransformPixel (in PS_INPUT input, out PS_OUTPUT output)
  2. {
  3. output.color = tex2D(TextureSampler,input.texcoord);
  4. }
  5.  
  • 最後にテクニックを作成する必要があります。これはエフェクト(今回のケースでは適用されません)で使用されるレンダリングステートの情報を宣言し、頂点シェーダとピクセルシェーダの機能が使われます。
  1. technique TransformTechnique
  2. {
  3. pass P0
  4. {
  5. VertexShader = compile vs_2_0 TransformVertex();
  6. PixelShader = compile ps_2_0 TransformPixel();
  7. }
  8. }
  9.  

最終更新:2008年06月01日 04:50