Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #36 – How to Create Reflections and Shadows for Images and Text.

Reflections are a cool effect you can apply to images and text elements through the use of RenderTransforms. For example, take a look at this screen shot below of reflections which were rendered using Silverlight. The first reflection is done on a TextBlock, the second on an Image.

  image

                           Figure 1. TextBlock and Image reflection

For game programming you can take this a step further and use it for object shadows and water reflections in your game. For example, take a look at this screen shot that shows a tree’s reflections on two different ground surfaces:

   image image

                                        Figure 2. Tree with shadow

You can even apply a SkewTranform to the shadow to skew it the direction of the sun.

   image

Figure 3. Shadow skewed in Sun Direction

And how about a little reflection on water:

   image image

                               Figure 4. Water reflections.

So how is all this done? Let’s start with the reflection of a TextBlock as show in Figure 1 above.

<TextBlock Text="REFLECTION EXAMPLE" Canvas.Left="145" Canvas.Top="12" Foreground="White" FontSize="30"></TextBlock>
<TextBlock Text="REFLECTION EXAMPLE" Canvas.Top="80" Canvas.Left="145" FontSize="30" RenderTransformOrigin="0.5,0.5" Opacity="0.6">
    <TextBlock.RenderTransform>
        <ScaleTransform  ScaleY="-1"/>
    </TextBlock.RenderTransform>
    <TextBlock.Foreground>
        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" >
            <GradientStop Color="#FF000000" Offset="0.0"/>
            <GradientStop Color="#FFFFFFFF" Offset="1.0"/>
        </LinearGradientBrush>
    </TextBlock.Foreground>
</TextBlock>

Few things to notice:

  1. We use two TextBlocks, one for the main TextBlock , one for the reflection.
  2. For the TextBlock that is reflecting we create a ScaleTransform and set the ScaleY = –1. This will make the TextBlock invert upside down.
  3. We create an OpacityMask for the purpose of fading the TextBlock out. We use a LinearGradientBrush for this OpacityMask which allows us to fade vertically by going from StartPoint “0.5,0” to EndPoint “0.5,1”

Now for the Image in Figure 1 we do the exact same thing except we are using the Image object:

<Image Source="Reflect.png"  Canvas.Left="225" Canvas.Top="100" ></Image>
<Image Source="Reflect.png"  Canvas.Left="225" Canvas.Top="385" RenderTransformOrigin="0.0,0.0">
    <Image.RenderTransform>
            <ScaleTransform ScaleY="-1"></ScaleTransform>
    </Image.RenderTransform>
    <Image.OpacityMask>
        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientStop Color="#00000000" Offset="0.0"/>
            <GradientStop Color="#FFFFFFFF" Offset="1.0"/>
        </LinearGradientBrush>
    </Image.OpacityMask>
</Image>

The trees use the same effect except that the tree used to reflect on the ground is a black & white mask of the color image (see Figure 5 below). For the water reflection, we keep the color image since water reflects color.

image image

      Figure 5. Color Image + B/W Mask.

Run a demo of this Tip here: http://silverlight.services.live.com/invoke/66033/Reflection%20and%20Shadows/iframe.html

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Reflections are a cool affect you can apply to images and text elements through the use of RenderTransforms

# September 5, 2008 12:24 AM

Silverlight news for September 5, 2008 said:

Pingback from  Silverlight news for September 5, 2008

# September 5, 2008 4:59 AM

Dew Drop - September 5, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - September 5, 2008 | Alvin Ashcraft's Morning Dew

# September 5, 2008 8:12 AM

Amin Mahpour » Blog Archive » Silverlight post collections said:

Pingback from  Amin Mahpour  &raquo; Blog Archive   &raquo; Silverlight post collections

# September 5, 2008 11:13 AM

How to Create Reflections and Shadows for Images and Text. « Rich Internet Applications said:

Pingback from  How to Create Reflections and Shadows for Images and Text. &laquo; Rich Internet Applications

# September 5, 2008 4:03 PM

Mirrored Blogs said:

Post: Approved at: Sep-6-2008 Tip: How to Create Reflections and Shadows Good little article on Reflections

# September 6, 2008 4:34 AM

Community Blogs said:

Michael Washington on Silverlight Desktop for DNN, Corey Schuman with Play/Paus button, Shawn Wildermuth

# September 6, 2008 12:18 PM

WiredPrairie - How to create a reflection in silverlight without a VisualBrush said:

Pingback from  WiredPrairie -   How to create a reflection in silverlight without a VisualBrush

# September 7, 2008 7:33 AM

2008 September 08 - Links for today « My (almost) Daily Links said:

Pingback from  2008 September 08 - Links for today &laquo; My (almost) Daily Links

# September 8, 2008 2:42 AM

Visual Web Developer Team Blog said:

&#160; Silverlight Tip of the&#160; Day #39 Title : How to Create a Zoom Toolbar Demo: silverlight.services.live.com/.../iframe.html

# September 11, 2008 1:21 AM

veritassword said:

How do you do the reflection without XAML (like using the code-behind)?

I have an issue descriped <a href="silverlight.net/.../43842.aspx">here on the Silverlight Forums</a> where the opacity mask or the linear brush will only make the reflected image black instead of fading from dark to light. Any ideas?

# October 30, 2008 3:42 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:57 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:57 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:26 AM