Basic Effect


In this tutorial, we will be taking an initial look at effects in MonoGame, especially the BasicEffect class. In the Using 3D Models tutorial, we looked at how to draw 3D models. In that tutorial, we actually ran into the BasicEffect class. Here, we will pick up where we left off in that tutorial and discuss some of the basics of the BasicEffect class. While you don't need to, I would recommend starting from the code that we made at the end of that tutorial.

Effects in MonoGame

An effect is simply a method of designating how an object should be rendered on the screen. Back in the past, a graphics API would have a list of variables that you could set that would tell the graphics card everything it needed to know, like the position of lights in the scene, the textures to use, and other things. More recently, as graphics programmers wanted more flexibility and graphics cards became more powerful, it became possible for a person to create a small program that could be run for each vertex that you were handling and for each pixel that you draw on the screen. With this, the possibilities became limitless, and you can now do anything you want, as long as your graphics card can keep up with it.

These programs are written in a programming language called HLSL, and there will be tutorials later that will discuss how to use this. There is one small problem, though. It can be quite a bit of work to create a complete effect from scratch, and so the MonoGame people provide an easy-to-use but powerful effect for you, which is utilized with the BasicEffect class. In this tutorial and the next couple of tutorials, we will look at how this class is used.

Some BasicEffect Basics

In the next couple of tutorials, we will take a look at how to do lighting and fog with the BasicEffect class. In this tutorial, we will look at where theBasicEffect class was used earlier in our game and a few simple things that you can do with it. Recall that in the previous tutorial, we created a DrawModel() method that looked like this:

private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
    foreach (ModelMesh mesh in model.Meshes)
        foreach (BasicEffect effect in mesh.Effects)
            effect.World = world;
            effect.View = view;
            effect.Projection = projection;

Notice that the inner foreach loop states that we want to look at all of the BasicEffects that are in each mesh of the model. By default, when you load a model, the Content Pipeline prepares the model with a BasicEffect to handle the rendering. Later, we will see that you can replace the BasicEffect class with something of your own, but for now, we will keep this the same. Before, we set the world, view, and projection matrices of the effect to be what we wanted, and the BasicEffect class took care of it from there.

In addition to what we already have here, there are two things that we want to look at. First, you can set a different texture for the BasicEffect. For instance, continuing on with the code from the previous tutorial, I have loaded another texture called "GreenShipTexture.png", which can be downloaded from the Texture Library. You can then put in the other texture with a line of code like the following:

effect.Texture = otherTexture; // otherTexture is of the type "Texture2D"

This gives us something like the image below when we run it:


The second thing we want to do is to try using no texture at all. This can easily be done with the following line of code:

effect.TextureEnabled = false;

When we do this, we get a solid white drawing, like in the image below, because the effect has no other indication of what color should be used.


Download the completed project

What's Next?

In this tutorial, we got a little bit of an idea of what the BasicEffect class does and how effects work. The next step is to use a couple of other features of the BasicEffect class, including lighting and fog.

Troubleshooting.png Having problems with this tutorial? Try the troubleshooting page!