Jump to content
  • Advertisement
Sign in to follow this  
Yann ALET

[FIXED] HLSL Scale shader

This topic is 3664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm new to HLSL, and I'm trying to write a simple scale shader, it works fines but the scale origin point is not centered and I have no idea what's wrong :( Vertex shader code : QuadVertexOutput DesaturateVS(float4 Position : POSITION, float2 TexCoord : TEXCOORD0) { QuadVertexOutput OUT; OUT.Position = Position; float2 texelSize = 1.0 / QuadScreenSize; OUT.UV = TexCoord + texelSize*0.5 - Center; return OUT; } Pixel shader code : half4 DesaturatePS(QuadVertexOutput IN) : COLOR { half4 c = 0; float scale = 5; c += tex2D(implicitSampler, IN.UV.xy*scale); return c; } To help you understand my problem here is a screenshot with shader off : http://adysoft5.free.fr/shader/without_shader.jpg and now with shader on : http://adysoft5.free.fr/shader/with_shader.jpg As you can see, the scaling works, but the output is not centered :s Can anyone help me on this ? :) [Edited by - Yann ALET on June 13, 2008 6:47:58 AM]

Share this post


Link to post
Share on other sites
Advertisement

Are you perhaps working with pretransformed (screenspace) vertex coordinates? If so, this is probably expected behavior, since these coordinates range from 0;0 to width;height, with the origin at the top left corner. There are a few ways around this, but it depends on what you want to do.

For a typical game scenario, the most useful way seems to me to change your shader to just texture the full quad (you'll probably need to give the quad vertices some texture coordinates for that) and change the quad size (ie the vertex positions) if you want to scale it. This way you'll have more control over how big and where your quad should be displayed, without having to muck around with shader parameters for scaling.

Share this post


Link to post
Share on other sites
Thanks for the answer !

Actually I'm not working on a game, but on a .NET C# WPF application. The component I'm applying the shader on is a simple Image. All I want is to scale up/down this image when the user move the mouse over it for instance.
So there are no vertices involved, just a texture (the image) that I'm sending to the shader.
Like I said, the scale effect works fine except the scaling origin point seems to be the top left corner of the application, and not the center of the image.
I think it comes form this :
c += tex2D(implicitSampler, IN.UV.xy*scale);

I tried to offset output pixels like this :
float2 center = 0.5; //UV map are 0 to 1 right ? So 0.5 should be the middle ?!
c += tex2D(implicitSampler, IN.UV.xy*scale + center);

but it didn't work ...

I don't know if this can help you (or anyone else) figure what's wrong. Any help would be greatly appreciated :)

Share this post


Link to post
Share on other sites
I managed to fix the problem :)
If anyone has the same issue, here is the fix :

Replace :
c += tex2D(implicitSampler, IN.UV.xy*scale);

by

c += tex2D(implicitSampler, (IN.UV.xy + float2(0.5,0.5))*scale);

Works great !

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!