Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I need to initialize a rendertarget ('SurfaceFormat.Single' format) drawing sprites into it. I would like the alpha channel of the sprite to be written into the rendertarget, so that regardless of the sprite texture, i end up with sprites silhouettes in the rendertarget. But XNA doesn't support alpha blending when using SurfaceFormat.Single, and from the pixel shader in which i handle the rendertarget for further calculations, i have all 0 readings. I find documentation of this features to be sparse and partial, so, could anyone suggest some source where i can check the sanity of my code?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

After some experimentation I wasn't able to directly use alpha when writing with spriteBatch to a SurfaceFormat.Single format RenderTarget.

So I had to render

  • from spriteBatch into SurfaceFormat.Color rendertarget "A"
  • from "A" into SurfaceFormat.Color rendertarget "B", drawing a full.screen quad while using a pixel shader that stores alpha channel into red channel
  • from "B" into SurfaceFormat.Single rendertarget "C"


There are two problems addressed here:

  1. the application of a pixel shader to a sprite
  2. usage of alpha channel in context where alpha is not supported

For first problem, it is not enough to specify an effect while calling spriteBatch.Begin(...), probably because using batch.Draw(...) isn't the same as drawing textured geometry; it is required to draw sprites to a rendertarget and then use that as a texture while drawing again to screen, applying the pixel shader, as suggested in Microsoft article http://msdn.microsoft.com/en-us/library/bb313868(v=xnagamestudio.31).aspx (outdated since it's referring to XNA 3.0 way of doing so, but the underlying theory still holds)

For second problem, i still suspect there's a better way, but the only way i managed to use alpha while in context not supporting it was to do an additional pass before, writing alpha into some other channel (ok for me since for that technique i only cared about alpha)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.