- Home /
Shader: get size of a pixel for orthographic camera
I've been working on a shader to create object outlines, and I can't seem to figure out what the width/height of a single pixel is in projection space, i.e. after you've multiplied everything by MVP. What I would like to do is move each vertex out along a normal by some number of pixels.
So let's say I have a simple 1x1 square quad. At each corner I have a normal, pointing out from the center of the quad. All the points and the normals are in 2D space relative to the camera - they quad is facing the camera straight on. I then take each normal and get it into projection space:
float3 norm = mul((float3x3)UNITY_MATRIX_MVP, v.normal);
float2 offset = norm.xy;
Then, I want to take that offset direction and multiply it by, say, 2 pixels. So something like this, though this code isn't working:
output.pos.xy += (offset * _NumberOfPixels) / _ScreenParams.y ;
(note: output.pos is the vector pos after it's converted to projection space)
A bit more information: this camera's orthographic size changes over time. I'm rendering the output of the ortho camera to a RenderTexture, which has a known size in pixels.
I'm happy to write code to keep track of the camera size with a script, and pass it to a material variable, if someone can explain to me how to use them to determine pixel size in projection space.
As a side note, is there any way to get a multiplier based on the size of a pixel that will work for both orthographic and perspective cameras? I would like to be able to write shaders that can move verts around a set number of pixels regardless of the camera rendering them.
Answer by goal2112 · Jul 09, 2013 at 05:02 PM
I think that there has to be a global value for a pixel size, but otherwise you can always pass it in to your shader.
pixel.width = 1.0f / Screen.width;
pixel.height = 1.0f / Screen.height;
simple enough.
EDIT: Just found this: http://docs.unity3d.com/Documentation/Components/SL-BuiltinValues.html
float4 _ScreenParams :
x is current render target width in pixels
y is current render target height in pixels
z is 1.0 + 1.0/width
w is 1.0 + 1.0/height
Answer by nicloay · Oct 14, 2018 at 06:29 AM
use unity_OrthoParams.xy
You can download shader sources from unity download pages Then at UnityShaderVariables.cginc find this
// x = orthographic camera's width
// y = orthographic camera's height
// z = unused
// w = 1.0 if camera is ortho, 0.0 if perspective
float4 unity_OrthoParams;
And it's also available at this page https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html
Your answer
Follow this Question
Related Questions
Unity Grabpass Feature 1 Answer
Easy way to render a texture, pixel perfectly, with a shader, and get the result? 0 Answers
Use camera depth texture to find distance from camera 0 Answers
,Can camera output negative values to render texture? 0 Answers
Check if mouse is over GUI element using Rendertexture? 1 Answer