Share via


Animated Textures

In addition to using dynamic buffers to animate geometry, you can also animate the textures on geometry.  This is accomplished by using BitmapProvider.  A BitmapProvider at its simplest is just a wrapper for a Bitmap, which can be used as the icon for a pushpin or the data for a texture that should be applied to geometry that you create in an Actor.  In fact, if you just have a single image you can use SingleBitmapProvider.  You expose this Bitmap via the CurrentImage property.  You also have to provide the size of the image upfront using ImageSize.  If you want to change the texture, the system polls the NewImageAvailable property.  If you return true, the system re-reads CurrentImage and copies data into the display texture.  When it is done, it calls OperationCompleted.

Animated pin

See it in action!

In this sample, we use a pushpin created with our own BitmapProvider.  This provider reads the previous screen buffer, creating an infinite mirror effect.  We could actually clean this sample up a bit in two ways: by implementing HasFrequentUpdates to return true (causing it to animate continuously) and removing the call to Host.NeedUpdate, and by implementing OperationCompleted to dispose current.

In a more realistic example, we might have a thread that periodically queries the url of a traffic cam.  When the image data arrives, set NewImageAvailable to true, provide the new image, and in OperationCompleted signal the query thread to get a new image.

For a demonstration of how to do this using Actors, copy the ScreenshotProvider class from AnimatedPins to BezierSurface, and change SurfacePlugIn.cs, line 91, to:

   Texture t = Texture.FromBitmapProvider(new ScreenshotProvider(this.Host));

and presto! you get the same effect.

Finally, SingleImageProvider has code built in to deal with animated gifs.  Simply create a bitmap with the animated gif, hand it to SingleImageProvider, that to your pin or Texture, and everything should go on its own from there.

Get the code!

Comments