[英]XNA - 3D game - Apply a light to all models
我想用3D圖形制作XNA游戲,我想知道一件事。 假設我的場景中有10個Model
,我想使用相同的光源(例如定向光)繪制它們。 現在,我了解到Model
擁有Effect
,而Effect
具有照明信息等等。 我的問題是,如何將同一光源應用於場景中的所有模型,而不是每個模型都有自己的光源? 有人告訴我我離基地不遠了。
如果使用XNA 4.0創建游戲,則需要使用“效果”。 幸運的是,XNA團隊包括了一個功能強大而簡單的Effect,稱為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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.