1

我想制作一个带有 3D 图形的 XNA 游戏,我想知道一件事。假设我Model的场景中有 10 秒,我想用相同的光源来绘制它们,比如定向光。现在我知道Models 有Effects 和Effects 有照明信息等等。我的问题是,如何将相同的光源应用于场景中的所有模型,而不是每个模型都有自己的光源?有人告诉我,如果我离基地很远。

4

1 回答 1

1

如果您使用 XNA 4.0 创建游戏,则需要使用 Effects。幸运的是,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();
    }
}
于 2012-01-04T19:10:12.337 回答