Determining every other pixel line?
by Dave Calabrese · in Torque Game Engine Advanced · 09/17/2007 (5:59 am) · 5 replies
I'm attempting to make every other line in my shader have a different alpha value (like a television monitor). To do this, I am determining if the line is even or odd. The end result I'm trying to achieve is the following:
1 = Visible
0 = Invisible
111111111111111111111111111
000000000000000000000000000
111111111111111111111111111
000000000000000000000000000
111111111111111111111111111
000000000000000000000000000
My block of HLSL for this, in the fragment program, is as follows:
This works, however is not accurate, as instead of making every odd valued line have an alpha value of zero, it instead seems to just make random pixels all over the place of an alpha value of 0. Any thoughts?
1 = Visible
0 = Invisible
111111111111111111111111111
000000000000000000000000000
111111111111111111111111111
000000000000000000000000000
111111111111111111111111111
000000000000000000000000000
My block of HLSL for this, in the fragment program, is as follows:
float modVal = ((IN.texCoord.y) * 1000);
modVal = modVal * modVal;
if( fmod( modVal , 2) > 0.0 )
OUT.col.a = 1;
else
OUT.col.a = 0;This works, however is not accurate, as instead of making every odd valued line have an alpha value of zero, it instead seems to just make random pixels all over the place of an alpha value of 0. Any thoughts?
About the author
Recent Threads
#2
The MAIN of the fragment program is:
Which causes the error:
I'm gonna be picking up a book on shaders here shortly (specifically: www.amazon.com/COMPLETE-Effect-HLSL-Guide/dp/0976613212/ref=si3_rdr_bb_product/1... ), so hopefully my questions will become a bit more exceptional after that rather than rudimentary. ;)
09/17/2007 (3:35 pm)
That was some very helpful information, actually! Those ddx and ddy functions seem quite useful. Unfortunetly (big surprise) I still can't get this to work. I now get an error message upon the compiling of the fragment shader.The MAIN of the fragment program is:
{
Fragout OUT;
float2 texturePos;
texturePos.x = IN.texCoord.x;
texturePos.y = IN.texCoord.y;
texturePos.y += sin( (IN.texCoord.x * 3800) * ((time) * 0.08)) * 0.004;
texturePos.x += sin( (IN.texCoord.y * 3800) * ((time) * 0.08)) * 0.004;
OUT.col = IN.shading;
OUT.col *= tex2D(diffuseMap, texturePos);
OUT.col.w *= visibility;
OUT.col.a = 0.8;
float modVal = (abs(ddy(IN.texCoord.y)) * 128);
modVal = modVal * modVal;
if( fmod( modVal , 2) > 0 )
OUT.col.a = 1;
else
OUT.col.a = 0;
return OUT;
}Which causes the error:
error X4532: cannot map expression to pixel shader instruction set
I'm gonna be picking up a book on shaders here shortly (specifically: www.amazon.com/COMPLETE-Effect-HLSL-Guide/dp/0976613212/ref=si3_rdr_bb_product/1... ), so hopefully my questions will become a bit more exceptional after that rather than rudimentary. ;)
#3
09/18/2007 (1:08 pm)
I think you need to set pixel shader 2.0 to get the ddx/ddy instructions. Hopefully that will help! ;)
#4
Is that what you meant?
09/18/2007 (10:07 pm)
I've already got it set to Pixel Shader 2.0... unless I'm thinking something else. I defined my 'ShaderData' as...new ShaderData( holoTelevisionShader )
{
DXVertexShaderFile = "shaders/holoTelevisionV.hlsl";
DXPixelShaderFile = "shaders/holoTelevisionP.hlsl";
pixVersion = 2.0;
};Is that what you meant?
#5
09/19/2007 (11:50 am)
Hrmm, I'd start commenting out stuff in your shader then. If I sneak some time away tonight, maybe I'll take a crack at it (just to get familiar with ddx/ddy).
Torque Owner Brian Richardson
If you square the value, it'll always be even. You may also want to add a little fudge value in your comparision, because of floating point errors.
Also, instead of multiplying by 1000, you might want to set the texture size as a shader constant and multiply by that (or always assume texture size x in your shader). That should give you better results. One thing to note is that if you allow mipmapping, the texture that you're sampling from could be a different size then the original.
I just did a really fast google search and found that the ddy function will probably do what you want. Check out this link:
http://forums.xna.com/thread/19899.aspx
and this example code:
http://www.shadertech.com/shaders/Checker.fx
Hope that helps!