Introduction

This sample shows the creation of a triangle as a polygon in 3D space using Silverlight 5 beta. The three vertices that comprise the triangle are assigned a position and color.

Building the Sample

If changing the shader files (*.hlsl), run the CompileShaders.bat file (with the DirectX SDK installed) to recompile the shaders in order to see the changes in your Silverlight application.

Description

This section shows the creation of the triangle's vertices and the assigning of colors:

 

C#
Edit|Remove
/// <summary> 
/// Creates a vertex buffer containing a single triangle primitive 
/// </summary> 
/// <returns>A vertex buffer containing a single triangle primitive</returns> 
VertexBuffer CreateTriangle() 
{ 
   // create vertices 
   var vertices = new VertexPositionColor[3]; 
   vertices[0].Position = new Vector3(-1, -10); // left 
   vertices[1].Position = new Vector3(010);   // top 
   vertices[2].Position = new Vector3(1, -10);  // right 
   vertices[0].Color = new Color(25500255); // red 
   vertices[1].Color = new Color(02550255); // green 
   vertices[2].Color = new Color(00255255); // blue 
             
   // create graphics device managed buffer 
   var vb = new VertexBuffer(resourceDevice, VertexPositionColor.VertexDeclaration, 
                vertices.Length, BufferUsage.WriteOnly); 
 
   // copy vertex data to graphics device buffer 
   vb.SetData(0, vertices, 0, vertices.Length, 0); 
 
   return vb; 
} 
 

 

Silverlight 5 introduces the ability to use hardware accelerated 3D graphics in your Silverlight applications. This opens up a whole new set of scenarios that are possible in Silverlight, such as 3D drawn controls, data visualizers, 3D charts, scatter points, geographic overlays, and 3D games and simulations. 

Developers familiar with XNA will be able to use their existing knowledge to help get them quickly up to speed with Silverlight 3D graphics programming.

3D graphics are an illusion that creates the perception of depth on a two-dimensional surface. It is possible to use standard 2D graphics already in Silverlight to create the illusion of 3D, but this would quickly become very complex. The core XNA graphics libraries are tailored specifically for 3D graphics and make the problem much easier to solve.

Silverlight introduces a number of new classes to support 3D graphics. The DrawingSurface control is the main Silverlight control used to render 3D graphics. Silverlight also includes the core of the XNA Graphics library from XNA Game Studio 4.0. 

Source Code Files

For more information on X, see http://blogs.msdn.com/b/silverlight_sdk/archive/2011/04/13/what-s-new-in-silverlight-5-beta-3d-graphics-support.aspx