Charles Petzold



A Rotating *Solid* Cube in Silverlight 3

August 6, 2009
Roscoe, N.Y.

Much time has passed since I posted a blog entry describing how to make a rotating wireframe cube in Silverlight 3, which I then proclaimed was about as far as Silverlight could go. I later realized that I could use Canvas.SetZIndex to order elements from foreground to background, and I could calculate shades based on surface normals and hypothetical lighting vectors. I have now persuaded Silverlight 3 to render a rotating solid cube:


RotatingCube.html

On the web page, the cube on the left is animated; the one on the right can be turned horizontally and vertically using the sliders. (I'm using an orthographic camera projection here — i.e., no perspective — and if you think the rear of the cube looks larger than the front, that's an optical illusion where your mind tries to project prospective into the image.)

The source code is in no state to be posted at this time. It needs a considerable amount of refactoring and cleaning up and testing with other types of simple figures. (The dodecahedron is next.) But keep in mind that the techniques I'm developing here cannot be used to simulate full-blown WPF-like 3D in Silverlight. The only clipping that's going on is on the 2D level based on the normal Canvas.ZIndex attached property, and it's easy to come up with examples where this technique will fail.

It's also not clear to me how much interest there is in this stuff. I've only gotten a little feedback from previous blog posts about simulating 3D in Silverlight, so I'm not sure it's really worth my time to get the code in usable shape for public consumption. Let me know.

Also, while working with the 3D transforms and other algorithms to get this program working, I found myself consulting a book on WPF 3D that I wrote two years ago, and which you might also find of use in understanding some of the complex mathematics involved. (Although it seems pretty hopeless at this time, if this book were to sell several thousand more copies, I might be able to persuade my publisher to let me get back into the book-writing business!)

Amazon.com BookSense.com Barnes & Noble
Amazon Canada Amazon UK Amazon Français
Amazon Deutsch Amazon Japan