June 22nd, 2009Silverlight 3 Writeable Bitmap API - Plasma
Crossposted on lab101.be
Inspired by old school MS DOS demos and this Flash Alchemy experiment.
Found out that someone already ported some code from the Alchemy experiment to C#.
So after some tweaking and some help of my colleague Frederik it’s up and running in Silverlight 3 beta.
I’m using two threads to speed up the plasma calculation when moving the dots, since everybody has a duo,triple,quad,octa … core CPU these days,
One thread does even lines and the other odd lines (interlacing).
public static void GeneratePlasmaEvenLines()
{
int index = 0;
for (var x = 0; x < width; x +=2)
{
for (var y = 0; y < height ; y+=1)
{
uint color = CalculatePixel(x, y);
plasma[index++] = color % 255;
}
index += width;
}
}
public static void GeneratePlasmaOddLines()
{
int index = width;
for (var x = 1; x < width; x+=2)
{
for (var y = 0; y < height; y+=1)
{
uint color = CalculatePixel(x, y);
plasma[index++] = color % 255;
}
index += height;
}
}
private static uint CalculatePixel(int x, int y)
{
int w = (int) param6;
int h = (int)param5;
uint color = (uint)
(
128.0 + (param3 * Math.Sin(x / param1))
+ 128.0 + (param4 * Math.Sin(y / param2))
+ 128.0 + ((param5/4) * Math.Sin(Math.Sqrt((double)((x - w)* (x - w) + (y - h ) * (y - h))) / 9.0))
+ 128.0 + ((param6/4)* Math.Sin(Math.Sqrt((double)(x * x + y * y)) / 9.0))
) / 4;
// Add shift position to sync the color
color += LastShift;
return color;
}
Placing the pixels on the canvas with the brand new bitmap API.
private void BuildBitmap(uint[] data)
{
writeableBitmap.Lock();
uint colorValue=0;
for (int x = 0; x < imageWidth; x++)
{
for (int y = 0; y < imageHeight; y++)
{
// set the pixel value
int palletIndex = (int)data[y * imageWidth + x];
colorValue = GeneratePlasma.palette[palletIndex];
writeableBitmap[y * imageWidth + x] = (int) colorValue;
}
}
writeableBitmap.Invalidate();
writeableBitmap.Unlock();
btImage.Source = writeableBitmap;
GC.Collect();
}
To view the full code download the project below.
Silverlight 3 beta is required (fully compatible with previous version)
Silverlight 3 Beta - Windows Developer Runtime
Silverlight 3 Beta - Mac Developer Runtime
No Tags





July 11th, 2009 at 3:49 pm
[...] Bitmap API (Link1, Link2) [...]
July 27th, 2009 at 11:59 am
You can definitely see the old school feel in the graphic. Thanks for the insight.
Regards
Adirec
August 25th, 2009 at 6:03 pm
Thanks for sharing the code. Can I add this effects in my game?
August 30th, 2009 at 9:22 pm
Love the graphics. Thanks for the code.
November 11th, 2009 at 4:42 pm
Hmmm looks interesting! Is Silverlight 3 in full version now (not beta)?
January 17th, 2010 at 4:49 pm
Thanks for sharing the code.
I really like it.
January 20th, 2010 at 6:59 pm
thx for sharing the code mate..
permit me to try
February 23rd, 2010 at 9:30 am
wew,…okey I follow you Kris..