2012-01-04 7 views
1

3DグラフィックスでXNAゲームを作りたいと思っています。私のシーンに10 Modelがあるとしましょう。私は指向性のライトのように、すべて同じ光源でそれらを描きたいと思います。今私はModelEffectを持っていて、Effectはとりわけ照明情報を持っていることを理解しています。私の質問は、自分の光源を持つ各モデルではなく、私のシーンのすべてのモデルにどのように同じ光源を適用するのでしょうか?誰かが、私が拠点を離れているかどうか教えてくれます。XNA - 3Dゲーム - すべてのモデルに光を当てよう

+0

したがって、http://gamedev.stackexchange.com –

答えて

1

XNA 4.0を使用してゲームを作成する場合は、エフェクトを使用する必要があります。幸いにも、XNAチームには、BasicEffectという強力で簡単なエフェクトが含まれていました。特に指定しない限り、BasicEffectはモデルのレンダリング時に使用されるデフォルトのエフェクトです。 BasicEffectは最大3つの指向性ライトをサポートしています。下のサンプルコードでは、BasicEffectインスタンスを操作して指向性ライトを使用してレンダリングする方法について説明します。

public void DrawModel(Model myModel, float modelRotation, Vector3 modelPosition, 
         Vector3 cameraPosition 
    ) { 
    // Copy any parent transforms. 
    Matrix[] transforms = new Matrix[myModel.Bones.Count]; 
    myModel.CopyAbsoluteBoneTransformsTo(transforms); 

    // Draw the model. A model can have multiple meshes, so loop. 
    foreach (ModelMesh mesh in myModel.Meshes) 
    { 
     // This is where the mesh orientation is set, as well 
     // as our camera and projection. 
     foreach (BasicEffect effect in mesh.Effects) 
     { 
      effect.EnableDefaultLighting(); 
      effect.World = transforms[mesh.ParentBone.Index] * 
          Matrix.CreateRotationY(modelRotation) * 
          Matrix.CreateTranslation(modelPosition); 
      effect.View = Matrix.CreateLookAt(cameraPosition, 
          Vector3.Zero, Vector3.Up); 
      effect.Projection = Matrix.CreatePerspectiveFieldOfView(
            MathHelper.ToRadians(45.0f), 1.333f, 
            1.0f, 10000.0f); 
      effect.LightingEnabled = true; // turn on the lighting subsystem. 
      effect.DirectionalLight0.DiffuseColor = new Vector3(0.5f, 0, 0); // a red light 
      effect.DirectionalLight0.Direction = new Vector3(1, 0, 0); // coming along the x-axis 
      effect.DirectionalLight0.SpecularColor = new Vector3(0, 1, 0); // with green highlights 
     } 
     // Draw the mesh, using the effects set above. 
     mesh.Draw(); 
    } 
} 
+0

でより良い応答を得ることができます。基本的にモデルの効果を変更して、モデルのライティングをオーバーライドしますか?私はそれで大丈夫です。 –

関連する問題