|
| 1 | +--- |
| 2 | +title: How to create a Basic Effect |
| 3 | +description: Demonstrates how to create and initialize an instance of the BasicEffect class and use it to draw simple geometry. |
| 4 | +requireMSLicense: true |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +The steps described here apply to effects created with the [BasicEffect](xref:Microsoft.Xna.Framework.Graphics.BasicEffect) class using the [Effect](xref:Microsoft.Xna.Framework.Graphics.Effect) class to write a custom effect. |
| 10 | + |
| 11 | +> [!NOTE] |
| 12 | +> The example draws aliased geometry, to see an example that draws smoother edges because it also applies anti-aliasing, see [Enabling Anti-aliasing (Multi-sampling)](HowTo_Enable_Anti_Aliasing.md). |
| 13 | +
|
| 14 | +### End result |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## To use BasicEffect |
| 19 | + |
| 20 | +Using the basic effect class requires a set of `world`, `view`, and `projection` matrices, a `vertex buffer`, a `vertex declaration`, and an instance of the [BasicEffect](xref:Microsoft.Xna.Framework.Graphics.BasicEffect) class. |
| 21 | + |
| 22 | +1. Declare these properties at the beginning of the game class. |
| 23 | + |
| 24 | + ```csharp |
| 25 | + //Matrices for 3D perspective |
| 26 | + private Matrix worldMatrix, viewMatrix, projectionMatrix; |
| 27 | + |
| 28 | + // Vertex data for rendering |
| 29 | + private VertexPositionColor[] triangleVertices; |
| 30 | + |
| 31 | + // A Vertex format structure that contains position, normal data, and one set of texture coordinates |
| 32 | + private BasicEffect basicEffect; |
| 33 | + ``` |
| 34 | + |
| 35 | +1. Initialize the world, view, and projection matrices in the `Initialize`. |
| 36 | + |
| 37 | + Next, create a world matrix using the default `Matrix.Identity` for simplicity. Set the `view matrix` as a `look-at` matrix with a camera position of `(0, 0, 50)`, pointing at the origin. The `projection matrix` is a `perspective` projection matrix based on a a `45-degree` field of view, an aspect ratio equal to the client window, and a set of `near` and `far` planes to render the geometry within in view of the camera. |
| 38 | + |
| 39 | + ```csharp |
| 40 | + protected override void Initialize() |
| 41 | + { |
| 42 | + // Setup the matrices to look forward |
| 43 | + worldMatrix = Matrix.Identity; |
| 44 | + viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 50), Vector3.Zero, Vector3.Up); |
| 45 | + |
| 46 | + projectionMatrix = Matrix.CreatePerspectiveFieldOfView( |
| 47 | + MathHelper.PiOver4, |
| 48 | + GraphicsDevice.Viewport.AspectRatio, |
| 49 | + 1.0f, 300.0f); |
| 50 | + |
| 51 | + base.Initialize(); |
| 52 | + } |
| 53 | + ``` |
| 54 | + |
| 55 | +1. Initialize a [BasicEffect](xref:Microsoft.Xna.Framework.Graphics.BasicEffect) with the transformation and light values in the `LoadContent` method. |
| 56 | + |
| 57 | + ```csharp |
| 58 | + protected override void LoadContent() |
| 59 | + { |
| 60 | + basicEffect = new BasicEffect(_graphics.GraphicsDevice); |
| 61 | + |
| 62 | + basicEffect.World = worldMatrix; |
| 63 | + basicEffect.View = viewMatrix; |
| 64 | + basicEffect.Projection = projectionMatrix; |
| 65 | + |
| 66 | + // primitive color |
| 67 | + basicEffect.AmbientLightColor = new Vector3(0.1f, 0.1f, 0.1f); |
| 68 | + basicEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f); |
| 69 | + basicEffect.SpecularColor = new Vector3(0.25f, 0.25f, 0.25f); |
| 70 | + basicEffect.SpecularPower = 5.0f; |
| 71 | + basicEffect.Alpha = 1.0f; |
| 72 | + // The following MUST be enabled if you want to color your vertices |
| 73 | + basicEffect.VertexColorEnabled = true; |
| 74 | + |
| 75 | + // Use the built in 3 lighting mode provided with BasicEffect |
| 76 | + basicEffect.EnableDefaultLighting(); |
| 77 | + } |
| 78 | + ``` |
| 79 | + |
| 80 | + > [!NOTE] |
| 81 | + > If you wish, you can set up the lighting manually through code, as follows: |
| 82 | + > [!code-csharp[](./files/basiceffectlighting.cs)] |
| 83 | + |
| 84 | + |
| 85 | +1. Still in `LoadContent`, create the per vertex data using the `VertexPositionColor` format. This example shows the data for the face of a triangle. |
| 86 | + |
| 87 | + ```csharp |
| 88 | + triangleVertices = new VertexPositionColor[3]; |
| 89 | + |
| 90 | + triangleVertices[0].Position = new Vector3(0f, 0f, 0f); |
| 91 | + triangleVertices[0].Color = Color.Red; |
| 92 | + triangleVertices[1].Position = new Vector3(10f, 10f, 0f); |
| 93 | + triangleVertices[1].Color = Color.Yellow; |
| 94 | + triangleVertices[2].Position = new Vector3(10f, 0f, -5f); |
| 95 | + triangleVertices[2].Color = Color.Green; |
| 96 | + ``` |
| 97 | + |
| 98 | +1. Finally, in the `Draw` Method, call [GraphicsDevice.Clear](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice#Microsoft_Xna_Framework_Graphics_GraphicsDevice_Clear_Microsoft_Xna_Framework_Color_) to clear the render target. |
| 99 | + |
| 100 | +1. Set the rasterizer state to turn off culling using the [RasterizerState](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice.RasterizerState) property. |
| 101 | + |
| 102 | +1. Call [EffectPass.Apply](/api/Microsoft.Xna.Framework.Graphics.EffectPass.html#Microsoft_Xna_Framework_Graphics_EffectPass_Apply) to set the effect state in preparation for rendering. |
| 103 | + |
| 104 | +1. Draw the geometry by calling [GraphicsDevice.DrawUserPrimitives](/api/Microsoft.Xna.Framework.Graphics.GraphicsDevice.html#Microsoft_Xna_Framework_Graphics_GraphicsDevice_DrawUserPrimitives__1_Microsoft_Xna_Framework_Graphics_PrimitiveType___0___System_Int32_System_Int32_Microsoft_Xna_Framework_Graphics_VertexDeclaration_). |
| 105 | + |
| 106 | + ```csharp |
| 107 | + protected override void Draw(GameTime gameTime) |
| 108 | + { |
| 109 | + GraphicsDevice.Clear(Color.SteelBlue); |
| 110 | + |
| 111 | + RasterizerState rasterizerState1 = new RasterizerState(); |
| 112 | + rasterizerState1.CullMode = CullMode.None; |
| 113 | + GraphicsDevice.RasterizerState = rasterizerState1; |
| 114 | + foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) |
| 115 | + { |
| 116 | + pass.Apply(); |
| 117 | + |
| 118 | + GraphicsDevice.DrawUserPrimitives( |
| 119 | + PrimitiveType.TriangleList, |
| 120 | + triangleVertices, |
| 121 | + 0, |
| 122 | + 1, |
| 123 | + VertexPositionColor.VertexDeclaration |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + base.Draw(gameTime); |
| 128 | + } |
| 129 | + ``` |
| 130 | + |
| 131 | +When the sample is run, the basic geometry is rendered using the custom [BasicEffect](xref:Microsoft.Xna.Framework.Graphics.BasicEffect), feel free to play with the position, content or rendering order to enhance the effect. |
| 132 | + |
| 133 | +## See Also |
| 134 | + |
| 135 | +- [How to create a State Object](HowTo_Create_a_StateObject.md) |
| 136 | + |
| 137 | +### Concepts |
| 138 | + |
| 139 | +- [What Is a Configurable Effect?](../../whatis/graphics/WhatIs_ConfigurableEffect.md) |
| 140 | + |
| 141 | +### Reference |
| 142 | + |
| 143 | +- [GraphicsDevice](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice) |
| 144 | +- [BasicEffect](xref:Microsoft.Xna.Framework.Graphics.BasicEffect) |
| 145 | +- [RasterizerState](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice.RasterizerState) |
0 commit comments