- Home /
Question by
shaderWriter · May 24, 2015 at 12:42 PM ·
shadercg
How to get view space position of a pixel
Hello, I am need to get the screen space of a pixel or vertex in a shader.
The reason is that I have a renderTexture screen grab, and I want to get the pixel that aligns with the same pixel on the renderTexture.
Here is a test screenshot mapping the x/y coordinates to r/g:
As you can see the y channel aligns at the bottom of the screen, x is nowhere, This changes depending on the distance of the camera from the pixel. Any ideas?
Here is the test shader:
Shader "Tests/screenParams" {
Properties {
}
SubShader {
Pass {
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
struct vertexInput{
float4 vertex : POSITION;
};
struct vertexOutput{
float4 pos : SV_POSITION;
};
struct fragmentInput{
float4 pos : SV_POSITION;
};
vertexOutput vert(vertexInput i)
{
vertexOutput o;
o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
return o;
}
float4 frag(fragmentInput i): COLOR
{
float4 texCoords = ComputeScreenPos(i.pos);
return float4(texCoords.x, texCoords.y, 0.5, 1.0);
}
ENDCG
}
}
}
shaderexample.jpg
(162.1 kB)
Comment