Game Development Community

Average: Refraction Materials on Animated Sprites bug out. (video)

by Matthew Hoesterey · in Torque X 2D · 05/25/2009 (10:34 am) · 20 replies

(I created a big refraction material center screen so the problem is easy to see.)
http://www.mhoesterey.com/demo/RefractionProblem.wmv

This issue existed in the last release and has not been fixed in the newest version of the engine.

If you have any object in a layer above a refraction material and that object is anywhere on the screen the refraction bugs out as seen in the video.

The material also bugs out randomly even when no objects are assigned to be above it. Once it has bugged out as seen in the video it will never play right again.


Thanks.:)

#1
09/15/2009 (3:16 am)
Has there been any fix to this?
#2
09/15/2009 (10:02 am)
Nope
#3
03/07/2010 (2:19 pm)
Anyone have an idea of what would cause the above behavior. If I knew what was causing this I could take a stab at fixing it. I'd really like to use this effect.
#4
03/07/2010 (7:27 pm)
RefractionManager.cs seems like a good place to start. The class summary suggests some possible issues with refraction in 2D games and some possible ways to address.

(not sure what yr precise bug is as couldn't play the vid - maybe you can put it on youtube)
#5
03/08/2010 (7:14 am)
Thanks Man! I'll check out the refraction manager. I'll also post the vid on youtube when I get home. Basically the refraction material will start refracting the entire sprite (you can see the poly outline even when you shouldn't).
#6
03/08/2010 (11:15 am)
there's another refraction bug which may be related to your issue. possibli.
if you have a sprite using a screen overlay...er... "Additive" and another doing "Refraction", the refraction one will blow out and clip.

Hope it helps with the detective work.
#7
03/08/2010 (9:27 pm)
Here is an image from the video I made last may of the problem.
www.mhoesterey.com/demo/RefractionProblem.jpg
You can see how the big ugly sprite in the center of the screen shows its edges even though they are alphad out.

Steve this very well could be a problem caused by additive sprites as other sprites in my scene are additive.

I'll let you guys know if I figure anything out.
#8
03/09/2010 (10:53 am)
where abouts is the refraction going on in that screenshot?
#9
03/09/2010 (12:06 pm)
I was not able to see the video but for me this looks like the sprite is not correctly masked/alpha values are not correct.
#10
03/09/2010 (1:09 pm)
The big white scribble is set to refract. Though you will notice the edge of the sprite. If you see the video different edges flicker into view.

I know its not the alpha as I could at one time reproduce the cause of this and the problem can be made at times to go away. I know one cause has to do with the layering of objects. Having an object on a layer in front of the refraction sprite will sometimes make it start to flicker. Sadly the problem seams to be caused by other things as well. (unless I'm creating an object at layer 0 in code that I don't know about)

I'll try to get a converter for the video so more people can see it. I took it with fraps. Apparently its a strange format.

(as a side note I've actually tried copying the bomb refract material from the plane game that ships with torque. It bugs out too when put in a complex scene with a ton of objects.)

My next step will be to put the refract material in a simpler scene and try to break it. Once I'm exactly sure what breaks it hopefully it will be easier to fix.
#11
03/09/2010 (4:41 pm)
I updated the picture above with a newer one that better illustrates the problem.

The refraction sprite in the pic is taken from the plane game that ships with torque.

Here is a link to the you tube video

www.youtube.com/watch?v=bWOCXFh_08w
#12
03/09/2010 (5:01 pm)
Ok so Steve was correct. If an additive sprite occupies the screen the refraction sprite seams to stop using its alpha. This was reproduced in the tankbuster demo.
#13
03/09/2010 (7:10 pm)
Is the IsTranslucent flag set properly on all the relevant materials? (the class summary of the RefractionManager refers to an issue there).

The refraction shader uses rgb for the refraction and alpha is used for alpha. So there shouldn't really be any issue with the alpha unless additive materials have set the alpha blend mode differently and it isn't getting reset when it should.

The pic of the big white texture you posted earlier is clearly not fully transparent at the edges though. Was that because of the glitching or is the texture really like that?
#14
03/09/2010 (9:05 pm)
That was the glitching. :)

As far as I can tell the Translucent flag is set correctly on all my materials. Looking at the Refraction Manager I think the translucent problem they mention is that all translucent materials will be refracted regardless of the layer they are on. I don't think its related but could be wrong.

You mentioned that:

"there shouldn't really be any issue with the alpha unless additive materials have set the alpha blend mode differently and it isn't getting reset when it should."

Could the problem be in the actual additive materials translucency? I could try forcing an additive material to be non translucent.

I delved into the Refraction manager. From what I can tell it is just using the back buffer to create a texture of the background and then passes that into the refraction material. Perhaps the problem is deeper and in the render manager. :(

I thought it might be useful to have a before after comparison. I made a vid of the tankbuster demo before changing one of the hit fx to additive and then after changing the hit fx to additive. (by just clicking the additive box on the material in TX2D builder)
www.youtube.com/watch?v=nq4PZodLwEs

It seams that the refracted materials alpha is being ignored when an additive material is present. :( I'm going to Dive into the BaseRenderManager.cs as well and see if I can find anything in there. :)
#15
03/10/2010 (8:03 am)
"there shouldn't really be any issue with the alpha unless additive materials have set the alpha blend mode differently and it isn't getting reset when it should."

That was based on my reading of the refraction shader. When the shader uses your refraction texture it uses the rgb components to workout the refraction at each pixel and then it applies the alpha component directly to the final colour. So the alpha being used is the one specified in your refraction texture, and so far as I can see is not being modified or ignored.

Additive materials do set the src/dest blend stuff, however, and this influences how alpha is applied when compositing the output from the pixel shader with the pixels already in the backbuffer/rendertarget (it's what makes the additive materials additive). So it struck me that maybe this has something to do with your glitch.

Looking at the refraction material, however, it's actually setting the src/dest blend stuff anyway, so I guess that's my hypothesis out the window :)

#16
03/10/2010 (10:00 am)
I'm wondering if its a render order issue. Perhaps the additive materials are getting rendered after the refracted material.

I have a feeling your on the right track with what you mentioned before though Duncan. Talking to a graphic coder friend he figured that a render state is getting blown away when the additive material renders. Perhaps their is a problem where the src blend isn't actually getting set at the right time.

#17
03/10/2010 (7:08 pm)
Good News.

I found the bugs. Well there are two and I've fixed 1. The other might be avoidable (maybe).


The first bug is related to the additive problem.

in RenderMaterial.cs Edit SetupEffect()


Change This:
if (IsTranslucent)
            {
                srs.Gfx.Device.RenderState.SourceBlend = _sourceBlend;
                srs.Gfx.Device.RenderState.DestinationBlend = _destinationBlend;
            }

Too This:
srs.Gfx.Device.RenderState.SourceBlend = _sourceBlend;
srs.Gfx.Device.RenderState.DestinationBlend = _destinationBlend;


Refracted materials are not translucent so I think the additive mat was setting the destination blend to "one" and then it wasn't getting set back in the renderer.

I dont think this will have any negative effects on performance.



The 2nd bug is related to the number of translucent scene objects on screen at once. It seams that a large number or translucent scene objects will also bug out the refract material and cause it to appear too opaque. The number of scene objects needed to do this is pretty high though. (I was using scene objects to create air whisps to get around a particle bug)

I'll be trying a large number of particles and will let you guys know if translucent particles have the same result.

#18
03/11/2010 (8:38 am)
Perhaps instead of removing the IsTranslucent check - just set the IsTranslucent flag to true on yr refraction material/s for the same result?
#19
03/11/2010 (8:50 am)
Sounds like a good idea.

#20
03/20/2010 (3:10 pm)
Turns out that wont fix the problem.

So I Set the refraction material to translucent and it still bugs out with additive materials.

I did some tests

I tried this:
if (IsTranslucent || IsRefractive)
            {
                if (IsRefractive)
                {
//break point here
                }
                srs.Gfx.Device.RenderState.SourceBlend = _sourceBlend;
                srs.Gfx.Device.RenderState.DestinationBlend = _destinationBlend;
            }

So the refraction material does go through this block of code but still bugs out. It looks like the problem isn't caused by the refraction materials settings but some other material that isn't getting its blend set.

I tried setting the additive materials blend in this block of code as well but that did not work either.

Not sure what material is bugging the refraction out. I only know the problem only shows up when additive and refraction materials are together.


Removing the translucent check fixes the problem though. I'm at a loss to what the cause of the problem is but the fix seams to work at least.