【问题标题】:XNA - 3D game - Apply a light to all modelsXNA - 3D 游戏 - 为所有模型应用灯光
【发布时间】:2012-01-05 03:10:12
【问题描述】:

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

【问题讨论】:

标签: c# 3d xna lighting 3d-modelling


【解决方案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();
    }
}

【讨论】:

  • 那么,基本上修改Model的Effect来覆盖模型中的光照?我很好。
猜你喜欢
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多