Warp Effect using pure Pixel Shaders

I am totally blown away by the possibilities of Pixel Shaders and already finding a great variety of uses for it. To get started on Pixel Shaders I would recommend stopping by Greg Schecter’s blog and reading his series of articles on this subject. It was definitely a starting point for me.

As an experiment, I decided to do the Genie Effect as a Pixel Shader. The original implementation involved the use of a 3D mesh to get the distortion effect. There was also quite a bit of setup code involved to initialize a Viewport3D, put a GeometryModel3D in it and also initialize the MeshGeometry3D. With Pixel Shaders it becomes a lot easier and boils down to just applying an Effect !

  <Image Source="img.jpg"
         Grid.Row="1"
         Stretch="Fill">
       <Image.Effect>
         <WpfApplication1:WarpEffect 
              L1="{Binding Value, ElementName=_lp1}"
              L2="{Binding Value, ElementName=_lp2}"
              L3="{Binding Value, ElementName=_lp3}"
              L4="{Binding Value, ElementName=_lp4}"
              R1="{Binding Value, ElementName=_rp1}"
              R2="{Binding Value, ElementName=_rp2}"
              R3="{Binding Value, ElementName=_rp3}"
              R4="{Binding Value, ElementName=_rp4}" />
       </Image.Effect>
  </Image>

The parameters L1, L2, L3, L4, R1, R2, R3 and R4 control the two sides of the Warp mesh. In short they represent the control points of a Bezier curve for the left and right side. Each of these parameters is of type double and is bound to a HLSL constant register (c0 – c7).

By tweaking these parameters I can get some nifty looking Genie curves:

image

 

image

image

image

 

Here is a short video of the demo app in action:

Similar Posts:

14 responses to “Warp Effect using pure Pixel Shaders”

  1. Kris

    I worked on a few effects when the beta was first released. At http://labs.adobe.com/wiki/index.php/Pixel_Bender_Toolkit:Gallery there are some effects that can be converted to WPF easily. I think its a shame that they’re not more integrated with the imaging pipeline (WIC). I haven’t used either CoreImage or Pixel Bender but they seem to have some nice advantages. Maybe the multi-input effects that have been mentioned will enable some more scenarios.

  2. PierreMF

    I would really appreciate source code for the demo app. Do you intend to make it available?
    Thanks,
    Pierre

  3. Simsod

    Hi Pavan, great work as usual. It´s kinda sad that there aren´t that much pixelshader-samples out there. This is probably the best tutorial/samples I´ve found so far: http://blogs.microsoft.co.il/blogs/tamir/archive/2008/06/17/hlsl-pixel-shader-effects-tutorial.aspx
    It´s quite a different way of thinking, at least i´m having some trouble grasping it. But it´s like one of the MS guys said in a interview. the amount of developers using shaders will be many, the amount of developers writing them will probably be quite few.

    At the moment i´m looking for a drop/water-effect to use with a touch-screen app i´m working on, I heard jeremiah morril is working on it but haven´t seen anything from him..

    Aaaanyways. nice to see more samples on pixelshaders, hope it will end up in fluidkit eventually!

    regards, Simon

  4. Adam Hill

    There are two good ShaderX2 books that are now free for all, edited by Wolfgang Engel.

    http://www.realtimerendering.com/blog/shaderx2-books-available-for-free-download/

    Enjoy!

  5. Nigel

    Hi

    Did you ever post the sample code?

  6. anvaka

    Nice one!

    I created this effect for Silverlight 3 beta 1/WPF, using the same approach with Bezier curves. But I’m using a “transition”-kind shader, i.e. I’m passing current transition time to the shader, and it calculates current values of control points as a function of time.

    You can check live demo here (green rectangle – the lamp, is draggable): http://anvaka.googlepages.com/SLGenieDemo.htm

    Source code for the library is here: http://anvaka.googlepages.com/GenieDemo.zip

  7. ashish

    HI Pavan,

    Have you tried implementing image zoom in pixel shaders ?

  8. Tcha-Tcho

    Hy pavan!

    Thankyou for this great work… im trying to get a cylindrical distortion with pixel shaders… the effect im trying to get is that i have the image rolled over a bottle. Like this distortion
    || | | | | | | | ||
    || | | | | | | | ||
    || | | | | | | | ||
    || | | | | | | | ||
    || | | | | | | | ||
    || | | | | | | | ||
    || | | | | | | | ||
    || | | | | | | | ||
    || | | | | | | | ||
    || | | | | | | | ||
    || | | | | | | | ||
    || | | | | | | | ||

    Did you think its possible to build with your warp plugin??

    Thanks in advance

    Best Regards from Brazil and happy new year

Leave a Reply