Pixel-perfect shader in Unity ShaderLab

喜欢而已 提交于 2019-11-28 12:03:13

There is a ShaderLab built-in value called _ScreenParams.

_ScreenParams.x is the screen width in pixels. _ScreenParams.y is the screen height in pixels.

Here's the documentation page: http://docs.unity3d.com/462/Documentation/Manual/SL-BuiltinValues.html

I don't think this is going to happen. Your rendering is tied to current selected video mode and it doesn't even have to match your physical screen size (if that is what you mean by pixel-perfect).

The closest you are going to get with this is if you render at recommended resolution for your display device and use pixel shader to shade an entire screen. This way, one 'physical pixel' is going to be roughly equal to one actual rendered pixel. Other than that, it is impossible to associate physical (that is your display's) pixels to rendered ones.

This is unless, of course, I somehow misunderstood your intentions.

is it possible for the shader itself to "know" what the screen resolution is

I don't think so.

and indeed for the shader to control single physical pixels?

Yes. Pixel shaders know what pixel they are drawing and can also sample other pixels.

Mokona Modoki

First of all, please define 'Pixel perfect' and 'Physical pixel'.

If by physical pixel you mean your display's pixel (monitor, laptop display, any other hardware you might use) then you are out of luck. Shaders don't operate on those, they operate on their own 'abstract pixels'.

You can think about it in this way: Your graphics are rendered in a picture with some configurable resolution (say 800x600 pixels). You can still display this picture on a 1920x1080 display in full screen no problem, it would look crappy though. This is what's happening with actual display and video card rendering. What determines the actual amount of rendered pixels is your video mode (picture's resolution in the above example). And physical pixels are your display's pixels. When rendering you can only operate on the first kind.

This leads us to a conclusion that when you render the graphics at the exact same resolution as your display's native resolution, you can safely say that you endeed render it as 'Physical Pixels'.

In unity, you can pass the renderer some external data (this might include your current screen resolution (for example as a Vector2, see this). However you most likely don't need any of this, since pixel shaders already operate on pixels (rendered pixels, determined by your current video mode). That means that if you use some resolution which is lesser than your native one, you most likely will not be able to render a single pixel.

Hope it helped.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!