3D グラフィックスを使用した XNA ゲームを作成したいのですが、疑問に思っていることが 1 つあります。シーンに10 個Model
の s があり、それらすべてをディレクショナル ライトのような同じ光源で描画したいとします。これで、 には があり、 には照明情報があることModel
がわかりました。私の質問は、各モデルが独自の光源を持つのではなく、シーン内のすべてのモデルに同じ光源を適用するにはどうすればよいですか? 私がベースから外れているかどうか誰か教えてください。Effect
Effect
5898 次
1 に答える
1
XNA 4.0を使用してゲームを作成している場合は、Effectsを使用する必要があります。幸い、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();
}
}
于 2012-01-04T19:10:12.337 に答える