Silverlight 5 RC–What’s new in the 3D world?

Silverlight 5 RC is now available and you can grab it here.

If you want to know more about non 3D features, here are some interesting posts:

This release also introduces a lot of new features for XNA support in Silverlight 5:

Assemblies

The first thing you will note is the rename of the assemblies:

  • Microsoft.Xna.Framework.Silverlight became System.Windows.Xna.dll.
  • Audio moved from System.Windows.dll to Microsoft.Xna.Framework.dll

GraphicsDevice

The device is no longer handled by the DrawEventArgs class. It is now available on GraphicsDeviceManager.Current.GraphicsDevice. So it is far easier to initialize your code because you don’t have to wait for the first draw to get the device:






  1. private void myDrawingSurface_Draw(object sender, DrawEventArgs e)


  2. {


  3. // Render scene


  4. scene.Draw();


  5.  


  6. // Let’s go for another turn!


  7. e.InvalidateSurface();


  8. }


  9.  


  10. private void UserControl_Loaded(object sender, RoutedEventArgs e)


  11. {


  12. // Check if GPU is on


  13. if (GraphicsDeviceManager.Current.RenderMode != RenderMode.Hardware)


  14. {


  15. MessageBox.Show(“Please activate enableGPUAcceleration=true on your Silverlight plugin page.”, “Warning”, MessageBoxButton.OK);


  16. }


  17.  


  18. // Create the scene


  19. scene = new Scene(GraphicsDeviceManager.Current.GraphicsDevice);


  20. }




Effect classes

A new base class for effect was introduced. It has the same features as the Effect class in XNA.

Obviously the 5 children of this class are also supported:

So from now, you can write the following code:






  1. // Clearing screen


  2. GraphicsDeviceManager.Current.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, new Color(0.2f, 0.2f, 0.2f, 1.0f), 1.0f, 0);


  3.  


  4. basicEffect = new BasicEffect(GraphicsDeviceManager.Current.GraphicsDevice);


  5. basicEffect.EnableDefaultLighting();


  6. basicEffect.VertexColorEnabled = true;


  7.  


  8. // Compute matrices


  9. Matrix world = Matrix.CreateRotationX(rotationAngle) * Matrix.CreateRotationY(rotationAngle);


  10. Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, -5.0f), Vector3.Zero, Vector3.UnitY);


  11. Matrix projection = Matrix.CreatePerspectiveFieldOfView(0.85f, aspectRatio, 0.01f, 1000.0f);


  12.  


  13. // Affect parameters values


  14. basicEffect.World = world;


  15. basicEffect.View = view;


  16. basicEffect.Projection = projection;


  17.  


  18. foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)


  19. {


  20. // Apply pass


  21. pass.Apply();


  22.  


  23. // Drawing the cube


  24. cube.Draw();


  25. }


  26.  


  27. // Animate rotation


  28. rotationAngle += 0.05f;




Actually, this is really simple now to port a XNA 4.0 code to Silverlight 5.

RenderTargets

Silverlight 5 RC now supports render target (2d as well as cube). So you can produce stellar effects such as mirrors, real-time environment maps, shadows, etc.

Like in XNA 4.0, you can easily create and affect a render target:






  1. RenderTarget2D renderTarget2D = new RenderTarget2D(GraphicsDeviceManager.Current.GraphicsDevice, 200, 200, false, SurfaceFormat.Color, DepthFormat.Depth24, 2, RenderTargetUsage.DiscardContents);


  2.  


  3. GraphicsDeviceManager.Current.GraphicsDevice.SetRenderTarget(renderTarget2D);




Multisample

You can activate multisample on render targets and on the main DrawingSurface. For the DrawingSurface, you must define it directly in the XAML code:






  1. <DrawingSurface Draw=”OnDraw” x:Name=”renderSurface”>


  2. <DrawingSurface.CompositionMode>


  3. <OffscreenCompositionMode PreferredMultiSampleCount=”2” />


  4. </DrawingSurface.CompositionMode>


  5. </DrawingSurface>




You can note the soft edges on the roof for example.

Using the same Xaml tag (CompositionMode), you can also define the depth buffer and stencil buffer format.






  1. <DrawingSurface.CompositionMode>


  2. <OffscreenCompositionMode PreferredMultiSampleCount=”2” PreferredDepthStencilFormat=”Depth24Stencil8” RenderTargetUsage=”DiscardContents”/>


  3. </DrawingSurface.CompositionMode>




Math library

And last but not least, the Microsoft.Xna.Framework.Math is now included in the SDK! Vectors, Matrices and other painful mathematical concepts are now handled for you Sourire.

Conclusion

Finally Silverlight 5 offers a really well integrated XNA experience. You will be able to reuse directly a large part of your XNA 4.0 code. The only point which is missing is the support for the content pipeline….but…wait…. ^^