我想制作一个带有3D图形的XNA游戏,有一件事我想知道。假设我的场景中有10 Model
个,我想用相同的光源绘制它们,就像定向光源一样。现在我了解Model
有Effect
个,Effect
有照明信息等等。我的问题是,如何将相同的光源应用于场景中的所有模型,而不是每个模型都有自己的光源?有人告诉我,如果我离开基地。
答案 0 :(得分: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();
}
}