I made a simple fragment shader (below) to test with a full screen billboard plane. It essentially does a colour change halfway down the screen, except it doesn’t:
void frag(in float4 pos:VPOS, out float4 col:COLOR)//, out float depth:smile:EPTH)
{
if (pos.y > _ScreenParams.y / 2)
col.rgba = float4(0, 1, 0,0);
else
col.rgba = float4(1,0,0,0);
}
I then run this from the editor with the “Game” tab split off into a window. When changing the window size, Unity scales the size of the screen within it to maintain its aspect ratio. This means, if the window is taller than the screen height, the screen will be centred vertically in the window with a margin at the top and bottom.
The thing is, the fragment Y coordinate I get as input (pos.y) is in window coordinates, not in the coordinate space of the screen centred within it. So, you can resize the window height up and down and as long as it stays taller than the screen, _ScreenParams.y will never change and the shader’s colour change will always be off-centre.
So, how do I get the actual window dimensions so I can compensate for this? I am using DX9 (Shader Model 3.0). I have no choice in the matter.
Actually, pos would be in clip space, so y is between -1 and 1. _ScreenParams.y is in pixels, which you don’t need for this. The clip space position x and y are 0 in the middle of the window, no matter what the aspect or resolution is.
That was the case for DX11 with Shader Model 5.0, but in Shader Model 3.0, the X and Y I’m being given are in actual window coordinates. I discovered this through experimentation. The shader above, under DX9 with SM 3.0, does indeed change color halfway-ish down the screen but it’s off-centered because the window is taller than the screen.
Even if the window is the same height as the screen, the change point is off-centre by precisely the amount of space used at the top of the window for its tab and window gadgets.
I’m not following you. What do you mean with screen and window? In my terminology it’s screen and viewport, but maybe that’s not what you mean. A screenshot would probably also help.
Sorry. When I say "screen " and “window”, I’m talking about physical things on the monitor display. “Window” is the drag-able window in which the game is being rendered. “Screen” is the sub-region within that window where the actual game’s rendered output appears (which is centred in the window).
Here’s a screenshot. Note that the “Game” tab’s view is taller than the rendered scene resulting in grey bars above and below the rendered scene. The entire height of that panel, grey bars and all, is the coordinate system in which I’m receiving fragment positions. I need to know the height of that region. _ScreenParams.y in the shader only provides the height of the rendered scene centred in the “Game” tab window.
Here is the shader being used on that polygon. it should cause a colour change at the midpoint of the rendered scene but the grey bar at the top is displacing it:
Shader "Custom/CustomShader"
{
SubShader
{
Pass
{
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
void vert(in float4 vertex:POSITION, out float4 pos:POSITION)
{
pos = mul(UNITY_MATRIX_MVP, vertex);
}
void frag(in float4 pos:VPOS, out float4 col:COLOR)
{
if (pos.y > _ScreenParams.y / 2)
col.rgba = float4(0, 1, 0,0); // Green
else
col.rgba = float4(1,0,0,0); // Red
}
ENDCG
}
}
FallBack "Diffuse"
}
And what is the “rendered scene” in this case? A render target? If you would start with an empty scene, put in an object and applied that shader, the color split should be at the middle of the viewport.
You are apparently doing some other steps that adds the offset. The answer to the size of the offset can only be found in those steps.
There isn’t a direct connection between VPOS and _ScreenParams. VPOS is the real pixel location, where _ScreenParams is the render resolution, and these might not match up in a window. Basically your _ScreenParams are probably giving you the 1920x1080 resolution you’ve set it to render that window at, but the actual resolution you’re displaying at is going to be smaller than that because you’re displaying a window and that’s what VPOS is going to give you coordinates to. If you measure the green area I bet you’ll find it’s 540 pixels high (1/2 of 1080).
What you want to do is not use VPOS. Instead use “in float4 pos : POSITION”. That should have the pos.xy be -1 to 1 which you can do (pos.xy * 0.5 + 0.5) * _ScreenParams.xy to get the pixel positions.
Alternatively don’t force the editor window to use 1920x1080, but just use 16:9 or free aspect.
I’ll give POSITION a try but I’m not sure if that’s going to work with Shader Model 3.0. Thanks.
Update: I was reasonably sure I’d tried using POSITION before. Now I recall why I am stuck with VPOS with DX9. When Unity tries to compile the shader, I get:
“invalid input semantic ‘POSITION’: Legal indices are in [1,15] invalid ps_3_0 input semantic ‘POSITION’ at line 36 (on d3d9)”
Note that changing POSITION to, say, POSITION1 yields a pos.x,y of 0, 0 for all fragments. According to this Microsoft site, the only input semantics available for pixel shaders under DX9 are COLOR, TEXCOORD, VFACE and VPOS: Semantics - Win32 apps | Microsoft Learn
I should point out that this shader worked fine (with DX11) until I was told I had to support DX9.
I never use VPOS myself, so that explains it. POSITION is an output from the vertex shader, but can’t be used directly as an input to the fragment shader. It’s used for rasterization by the GPU, but not passed along. You can easily make a copy though:
void vert(in float4 vertex:POSITION, out float4 pos:POSITION, out float4 pos2:TEXCOORD0)
{
pos = mul(UNITY_MATRIX_MVP, vertex);
pos2 = pos;
}
void frag(in float4 pos:TEXCOORD0, out float4 col:COLOR)
{
if (pos.y > 0)
col.rgba = float4(0, 1, 0,0); // Green
else
col.rgba = float4(1,0,0,0); // Red
}
That works! The coordinate system passed from TEXCOORD0 appears to be (-32 < y < 32) based on experimentation, with zero at the centre and positive Y going up the screen. Thanks muchly for the assistance. This has been driving me nuts.
That probably is because your plane is rendered at 32 units of distance. To make it always fall in the -1 to 1 range, just divide the x and y by w in the fragment shader: