# Automatic shadow for isometric objects

I have like hunderd of isometric objects. One example:

I'm trying to find a way to automate creation of shadow they're casting. I ended up with following transformation:

   matrix.Shear(-0.25f, -0.25f, MatrixOrder.Append);
matrix.Translate(0.5f * source.Width, 0.125f * source.Height, MatrixOrder.Append);


Whole algorithm:

  string sourceFileName = @"c:\in.png";
string targetFileName = @"c:\out.png";
using (var source = (Bitmap)Bitmap.FromFile(sourceFileName)) {
using (Bitmap target = new Bitmap(source.Width * 2, source.Height, PixelFormat.Format32bppArgb)) {
using (var g = Graphics.FromImage(target)) {
GraphicsPath gp = new GraphicsPath();
gp.AddPolygon(new Point[] { new Point(0, 0), new Point(source.Width, 0), new Point(0, source.Height) });
var matrix = new Matrix();
matrix.Reset();
matrix.Shear(-0.25f, -0.25f, MatrixOrder.Append);
matrix.Translate(0.5f * source.Width, 0.125f * source.Height, MatrixOrder.Append);
gp.Transform(matrix);
g.DrawImage(source, gp.PathPoints);
}
var grayscale = new Bitmap(target.Width, target.Height, PixelFormat.Format32bppArgb);
for (var x = 0; x < target.Width; x++) {
for (var y = 0; y < target.Height; y++) {
var pixel = target.GetPixel(x, y);
if (pixel.A != 0) {
pixel = Color.FromArgb(255 / 3, Color.Black);
}
grayscale.SetPixel(x, y, pixel);
}
}
for (var x = 0; x < source.Width / 2; x++) {
for (var y = source.Height - 1; y >= 0; y--) {
var pixel = source.GetPixel(x, y);
if (pixel.A != 0) {
break;
} else {
grayscale.SetPixel(x, y, Color.FromArgb(0, 0, 0, 0));
}
}
}

grayscale.Save(targetFileName, ImageFormat.Png);
}
}


Which produces something like:

It kind of works - but not 100% - the angle of shadow is changed according to proportion of the source object. Is there a way or algorithm that will work 100% or be closer to real ?

(Edited to explain more detaily).

• +1 this is seriously awesome. Try messing around with Photoshop first to see if you can get something useful, and then apply it to code. Aug 7 '13 at 22:20
• If you explain specifically what's "not 100%" with it, you're likely to get answers that are more relevant to you. Since this appears to be a stylistic thing, it's going to be difficult for people to answer you without a detailed technical description of exactly what you're looking for.
– House
Aug 7 '13 at 22:40
• Agreed with Byte56 - we need more explanation of how your current approach is failing / what you're looking for. Some screenshots of bad cases (especially in context in the game, not just isolated tiles) might help. Aug 7 '13 at 22:53
• Why do you translate it? Just a shear without translation gives more realistic result in my opinion when aligned from the bottom of the images. Aug 8 '13 at 7:25