Shader - change colour per position on screen

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
kubatp
Gnome
Posts: 368
Joined: Tue Jan 06, 2009 1:12 pm
x 43

Shader - change colour per position on screen

Post by kubatp »

Hello everyone,
I might have a very simple question but I am having problems with getting it to work.
In short, I want to be able to change output colour of fragment shader per its position on screen - the more in the center of the screen it is, the more transparent the result colour should be.

Use case is also quite simple - I want fog to be less dense in the middle of the screen and get more dense towards the edges.
I thought I will just use the calculation of output position in vertex shader, but it simply doesnt work well for whatever reason.
Anyone did something similar?
Thank you for your help!
kubatp
Gnome
Posts: 368
Joined: Tue Jan 06, 2009 1:12 pm
x 43

Re: Shader - change colour per position on screen

Post by kubatp »

Noone knows? I thought that it must be something easy for guys working with graphics but maybe it is not?
kubatp
Gnome
Posts: 368
Joined: Tue Jan 06, 2009 1:12 pm
x 43

Re: Shader - change colour per position on screen

Post by kubatp »

Hello paroj,
thank you for the reply. The link you sent did not help me directly because I use cg, but thanks to it I was able to find a chain or pages from which I got the solution.
I am not sure how good the solution is but here it is

Vertex shader:

Code: Select all

output.screenPosition = output.position; //standard output position from vs
Fragment shader:

Code: Select all

vertex_output.screenPosition.xy /= vertex_output.screenPosition.w;
float2 texCoord = 0.5f * (float2(vertex_output.screenPosition.x, -vertex_output.screenPosition.y) + 1);
float distanceFromCenterOfScreen = 1 - max(abs(texCoord[0] - 0.5), abs(texCoord[1] - 0.5));
or probably even better

Code: Select all

float distanceFromCenterOfScreen = distance(texCoord.xy, float2(0.5, 0.5));
I am not sure how good or efficient the solution is but it works. If anyone has ideas how to make it better or different way, please let me know.
Post Reply