Graphics in Silverlight 5 - Part V
In this fifth part of this series, we shall continue exploring 3-D graphics in Silverlight 5.
This article continues from the earlier parts of this series: Graphics in
Silverlight 5 - Part I, II, III, IV. In this artice, we will look at 3-D graphics in
Silverlight. Unlike WPF, Silverlight does not include comprehensive support for
3-D graphics. Support for 3-D graphics is limited in Silverlight. So let's now
see what is available and how to use it.
You can apply 3-D effects to any UIElement in Silverlight through a feature
called perspective transforms. Through perspective transforms, you can create an
illusion as though you were rotating or moving your objects in 3-D.
You can apply a perspective transform to a UIElement by setting the Projection
property of the element to a PlaneProjection. The PlaneProjection class
represents a 3-D-like effect on an object. It has around 12 properties that can
be used to control the rotation and positioning of an object.
Visit the MSDN link mentioned at the bottom of this article to see the 12
properties and their descriptions.
The following example renders 3-D like effect to an image. Thus, when you see
the output, you will experience an illusion of a 3-D control.
Note: Both examples in this article make use of an image, mill.jpg. The
image credit goes to Nimish Dalal (http://nimishdalal.posterous.com/).
Here, the RotationY property indicates the degree of rotation. For example, the
RotationY property will specify the rotation around the vertical axis of the
object. The value of RotationY tells you whether the object will be either
towards you or away from you. The LocalOffsetX property translates the object
along the X axis of the plane of the object after it has been rotated.
Therefore, the rotation of the object determines the direction in which you
translate the object.
The figure shows the output:
The real advantage of perspective transforms though is seen with animations.
Consider the following code:
Here, the image is rotated slightly to render a 3-D like effect. However instead
of specifying the rotate coordinates directly through RotationX property, an
animation is used to perform the same. The animation is defined inside a
storyboard which runs when the grid is loaded. The code-behind will include the
following to start the storyboard, which in turn will start the animation.
private void BeginAnimation(Object sender, EventArgs e)
Refer the following link for details of the PlaneProjection class.
Conclusion: The fifth part of this series briefly described 3-D graphics