Silverlight provides perspective transform for creating simple 3D effects. Silverlight also allows using part of the XNA framework functionality with support for 3D models, effects, and creating vertex and pixel shaders, giving the developers a lot of power. Both these methods are described in this chapter.
You're reading from Instant Silverlight 5 Animation
Perspective transform (also sometimes called projection transform) enables the developers to position or move Silverlight (2D) objects within 3D space. It is invaluable for creating simple but effective 3D animations, for example, for Silverlight banners.
Let's explain perspective transform while describing the corresponding sample located under the SAMPLES\CODE\ProjectionSample
folder. This sample allows you to investigate all of the parameters of the perspective transform.
Perspective transform in Silverlight is achieved by setting the Projection
property of a Silverlight object to contain the PlaneProjection
object, whose parameters define the parameters of perspective transform. These parameters include three rotation angles corresponding to rotation around each axis X, Y, and Z, 3D coordinates of the center of rotation, global offset, specifying 3D translation in the viewer's coordinates and local offset, specifying the 3D translation in the coordinates that rotate...
Silverlight 5 introduced real 3D capabilities via access to a simplified version of XNA. Using this functionality one can build real complex 3D models of triangles, and create vertex and pixel shaders utilizing the full power of GPU to display them.
There are some shortcomings, however, to the new functionality provided. They are as follows:
It is not multiplatform – it will only run on Windows (though it is multi-browser).
If run within a browser, the client will be required to allow the blocked display drivers for that website and this might be a nuisance if you want your 3D animations to be widely available.
The 3D code is essentially not part of the Silverlight framework. It is standalone functionality and the developer needs to spend some extra time and effort in order to make it interact properly with the rest of the Silverlight application.
Here we show how to build triangle 3D animation. The code for this sample is located under the SAMPLES\CODE\MovingTriangle
folder.
Try running this sample! During the first run, most likely you'll get the following warning message: Please enable your graphics drivers and reload the application. In order to be able to run the 3D application, you need to do the following:
Right mouse click onto this warning message (or anywhere around it).
Click on Silverlight.
Choose the Permissions tab, find and select the URL corresponding to the application (usually if you view it on the same machine, you'll be able to tell the correct URL by the port number).
Click on the Allow button at the bottom of the dialog box.
Click on the OK button.
Now restart the application if you are running it in the debugger, or restart the browser to refresh the application if you are running the application on a website.
When you run the application successfully, you will see a tricolored...
We can spice up the model a little bit by creating a rotating prism, instead of the rotating triangle. The MovingPrismApplication
project is located under the SAMPLES\CODE\ MovingPrismApplication
folder.
When running MovingPrismApplication
, do not forget to set the driver permissions for that application, just as you did for MovingTriangle
.
The following is a screenshot of the moving prism:
The only differences from the MovingTriangle
project are the following:
The fourth vertex is being created shifted along the Z-axis from the rest of the vertices:
// the 4th vertext to create the prism Vector3 zVertex = new Vector3(0, 0, 1);
The
vertices
array contains 12 vertices instead of 3; each three consecutive vertices corresponding to a side of the prism:// create an array of vertices // corresponding to the triangles // corresponding to the sides of the prism. // the same vertices will be repeated // for different sides if needed VertexPositionColor[] vertices = new...