Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Terrain Tutorial Part 3 - Creating Smooth Tile Transitions using Opacity Masks

This tutorial will look into using the Opacity property on the tiles to create smooth, natural looking transitions between tiles. For example, blending a dirt tile into a grass tile, a grass tile into a rock tile, etc.

Run the demo here: http://silverlight.services.live.com/invoke/66033/Terrain%20Transititions/iframe.html

Below, on the left, is a screen shot of a grass and dirt tile without the use of an opacity mask. As you can see, the straight line does not make for a very real looking transition! On the right is the result with an opacity mask applied, making for a much more realistic transition.

No Opacity Mask                                           With Opacity Mask

image image

So how does this work? What we do is we put down a grass layer followed by a layer of dirt on the same tile location. We then apply an Opacity mask to the dirt image which tells Silverlight what level of rendering to do on each pixel within the dirt image.

Example:

Layer 1                                    Layer 2                                  Opacity Mask                          Result

image + image+image =image

The checker boxes in the opacity image (image #3) above represent the areas that are transparent in the PNG opacity mask. In this area, the image will not be drawn allowing any image below it show through.

Here’s how it looks in our XAML code:

<Image Source="grass.png"></Image>
<Image Source="dirt.png" >
    <Image.OpacityMask >
        <ImageBrush ImageSource="opacityMask.png"></ImageBrush>
    </Image.OpacityMask>
</Image>

Using a PNG image file, transparency is represented by the alpha value only. All the other colors in this image have no impact. Each color is represented by “#AARRGGBB” where AA=alpha hex value, RR=red hex value, GG=green hex value and BB= blue hex value.  Transparency in a pixel increases starting at #00000000 (completely non-transparent) to #FF000000 (completly transparent.

If you do not want to use an image for your opacity mask, you can also use a Brush. For example, this tile shows a transition using a LinearImageBrush:

Layer 1                               Layer 2                                 LinearGradientBrush            Result

image+image+ image=image 

Here is the XAML code:

<Image Source="grass.png"></Image>
<Image Source="dirt.png">
    <Image.OpacityMask >
        <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
            <GradientStop Color="#FF000000" Offset="0.0" />
            <GradientStop Color="#00000000" Offset="1.0" />
        </LinearGradientBrush>
    </Image.OpacityMask>
</Image>

Using other brush types, such as the RadialGradientBrush, you can do a lot of other cool effects.

Example:

image+image+image= image

XAML Code:

<Image Source="grass.png"></Image>
<Image Source="dirt.png">
    <Image.OpacityMask >
        <RadialGradientBrush GradientOrigin="1,0">
            <GradientStop Color="#FF000000" Offset="0.0" />
            <GradientStop Color="#FF000000" Offset="0.25" />
            <GradientStop Color="#AE000000" Offset="0.75" />
            <GradientStop Color="#00000000" Offset="1.0" />
        </RadialGradientBrush>
    </Image.OpacityMask>
</Image>

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

This tutorial will look into using the Opacity property on the tiles to create smooth, natural looking

# August 25, 2008 8:44 PM

juergen79 said:

It looks easy and is good illustrated.

After my bachelor thesis about silverlight I would try to use your tutorials.

# August 26, 2008 2:07 AM

ProgrammerFish | Silverlight: Creating Smooth Tile Transitions using Opacity Masks said:

Pingback from  ProgrammerFish | Silverlight: Creating Smooth Tile Transitions using Opacity Masks

# August 26, 2008 2:21 AM

Silverlight news for August 26, 2008 said:

Pingback from  Silverlight news for August 26, 2008

# August 26, 2008 3:08 AM

Dew Drop - August 26, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - August 26, 2008 | Alvin Ashcraft's Morning Dew

# August 26, 2008 8:32 AM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #32 &#160; Title : How to Declare a Custom User Control from a XAML Page.

# August 26, 2008 1:13 PM

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

While exploring techniques for game programming with Silverlight I came across some hurdles and discovered

# August 26, 2008 1:57 PM

News said:

Silverlight Tip of the Day #32 Title : How to Declare a Custom User Control from a XAML Page. Demo :

# August 26, 2008 2:56 PM

Community Blogs said:

Scott Barnes on Submitting SL Bugs, Shawn Wildermuth on SL Firestarter in NYC, Jason Cooke Templatinging

# August 26, 2008 8:35 PM

Mirrored Blogs said:

Post: Approved at: Aug-27-2008 Microsoft Invests in Move Networks Microsoft made an undisclosed investment

# August 28, 2008 12:34 AM

GuptaAtul said:

good examples and explained well with pictures. working with Silverlight is really becoming easy

# October 3, 2008 7:24 AM

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

While exploring techniques for game programming with Silverlight I came across some hurdles and discovered

# November 18, 2008 1:27 PM

Silverlight Tips of the Day said:

While exploring techniques for game programming with Silverlight I came across some hurdles and discovered

# November 18, 2008 1:28 PM