当前位置:网站首页>Unity URP forward rendering process and obtaining screen space UVs and depth
Unity URP forward rendering process and obtaining screen space UVs and depth
2022-07-22 05:08:00 【It's not too late webgl】
Forward rendering process
cpu Cull and sort on , Reject as , The visual centrum of the camera is removed , Remove the model outside the camera view , Don't render anymore . Occlusion culling , Eliminate the invisible model in the visual vertebra , Don't render anymore . Sorting to ensure performance , Opacity is from near to far , Reduce unnecessary rendering , Translucent objects to ensure correctness , From far to near .
cpu Get the required relevant model data on : Vertex data Map data There are also material properties , Submit the data to gpu, And call gpu Rendering , Render every time , As a drawcall.
stay gpu On , In two parts , Part is vertex shader , The other part is the slice shader .
In vertex shaders , Mainly position change , Position the vertex , In turn, is Model space World space Look space Cutting space The process is MVP Matrix transformation .
There is also a process from vertex shader to fragment shader , In the process , Crop and cull , Clip the elements that are not in the screen to those that are only in the clipping display space , The display range is [-w, w], The four-dimensional variables obtained here xy It is under the cutting space xy coordinate ,z Is depth ,w Namely w, Note that there , Because get the screen uv You also need to operate in this step , And the compatibility of different platforms also needs to appear from this step , Because depth is z From this step, it varies according to the platform . Then proceed NDC( Normalized equipment coordinate system ), Normalization operation , stay unity Inside ,xy Restriction on 0 To w Between , And depth z, Depending on the platform , Then for -w To w perhaps 0-w.NDC After the operation , Remove the back , Some invisible elements ( Mainly triangular surface , Points and lines don't need ) Get rid of . If the three points of the primitive triangle are rendered clockwise, it is the back , Will be eliminated , The face rendered counterclockwise is the front , Will be rendered .
After the back culling , About to switch to screen space ( Or buffer space ), Here, it will be converted according to the rendered width and height , After the conversion , Element assembly , And then rasterize , Rasterization can be understood as obtaining the pixels of the screen occupied by the elements ( Usually called pianyuan ), How many pixels does it occupy , How many times will the slice shader be called to calculate the color . This step is to calculate the results of all slices of the primitive synchronously .
Slice shader operation completed , The final output merging stage is reached . After your calculation , It may not be able to fill pixels with color , We also need to conduct multiple tests , After passing these tests , Then the color can be written to the frame buffer . These steps are :
- Translucency test According to transparency , How many colors below transparency are directly discarded for writing .unity The function inside is clip, Internal if the value passed in is less than 0, No color will be written .
- Template testing According to the template set before , contrast , If you pass, you will continue to the next step .
- Depth testing Compare the depth of the frame buffer , By continuing to the next step .
- Mixed testing This step is mainly for translucent objects , The current color is mixed proportionally with the pixel color of the frame buffer , Get the final color .
Last , After completion , You can write the color to the buffer smoothly . Be careful , The buffer does not just store the colors of pixels , There is also in-depth information , And template information .
Get screen space UV
The previous rendering process has said so much , Then you should know how to get the screen uv Is that right . Must be NDC Spatial xy Just the coordinates , So how to get , stay urp Inside , There are two ways , First, let's talk about the simplest :
as follows , Use it directly urp Built in GetVertexPositionInputs function , Enter the vertex coordinates in model space , You can get the world space , Look space , Cutting space ,NDC All information in space . We can look at the structure :
The second way , That is, we calculate by ourselves through the coordinates in the clipping space ,unity Relevant methods are also built in ,
You can see , In fact, it's just a revision xy Coordinates of ,zw It didn't move at all . take xy from -w To w, Changed to 0 To w The range of .
_ProjectionParams Of x Item is for platform compatibility ,dx Of y coordinate 0 It's the top , and opengl Platform 0 To 1 It's from bottom to top .
I also found the width and height constant of the screen :
There is also time related
List others
These are all in UnityInput.hlsl In file , You can go and have a look for yourself .
Return to the right topic , Now that we have NDC Spatial xy, Their current range is [0, w], Then it's easy , As long as we divide by w, Then you get the screen space UV Coordinates .
Get depth
above , We get the screen UV, It's through NDC Coordinates in space xy Acquired , Actually z The value of is depth , We get the depth , First, limit it to 0-1 Or depending on the platform , stay -1 To 1 Between , In fact, it is in w Just fine .
such , You can get , Objects closer to the camera , The amount of color , The farther away , The darker it gets .
Why do you write this , because unity The values of the near clipping plane and the far clipping plane of each platform have been built in , We just need to judge
Here are a few of them , The normalized value of the far clipping plane and the near clipping plane of the platform , We can use this to calculate a unified depth rendering effect , You can do the rest by yourself unity Search in the Library , The names are listed below .
Get the depth in the depth map
The mood is here , Then write about how to get the depth in the depth map .
First , You have to urp Pipeline Asset Inside , Turn on depth texture rendering .
When it's turned on , You can shader It declares the texture _CameraDepthTexture, You can get the depth map .
Then take the screen obtained above uv, You can get the depth on the depth map .
It's that simple , You get the depth , renders , That's it
Last , There's another problem , Now the depth in the depth map and the depth you get , It's not linear , We need to be linear ,unity There are also built-in methods
Use according to your own needs , One is calculated from the near cutting surface , One is calculated from the camera position .1 Is the far cutting surface .
The first value is passed into the depth you calculated , Or the depth obtained from the depth map .
Second value _ZBufferParams, I also took a screenshot above , You can find it from above .
end .
边栏推荐
- Hcip notes sorting 2022/7/15
- PL-Marker(ACL 2022)——信息抽取(NER+RE)新SOTA,论文浅析与代码浏览
- (字符串+内存)函数的介绍
- [chestnut sugar GIS] how to batch delete empty files with the same name in multiple folders
- Subvert cognition! There are as many as 13 methods to guarantee research?
- Development direction and problems of optaplanner
- 基于MIPI的高性能成像系统
- 【板栗糖GIS】bat—怎么删除子文件夹下的同后缀名的数据
- 柠萌影视通过聆讯:年营收12亿 腾讯与芒果文创是股东
- 基于SqlSugar的开发框架循序渐进介绍(12)-- 拆分页面模块内容为组件,实现分而治之的处理
猜你喜欢
Php7.4 using composer to handle exceptions
Butterknife Library (an efficient tool library, Xiaobai only records one usage - no in-depth study, hee hee)
DRF--JWT2-用户认证-自定义控制simpjwt返回内容
动态内存精讲
Simple use of ormlitedb database (dependent) (Xiaobai growth record)
leetcode:814. 二叉树剪枝【dfs】
牛客刷题篇
深度之眼(十五)——导数
Access denied for user ‘root‘@‘localhost‘ (using password: YES)的解决方法
Yiyang Qianxi was scolded for being hot search...
随机推荐
unity URP前向渲染流程以及获取屏幕空间UV和深度
Example demonstration of "uncover the secrets of asp.net core 6 framework" [04]: Custom dependency injection framework
年中盘点 | 2022年,PaaS 再升级
unity的静态设置以及烘培属性
Zhenhua scenery semiconductor passed the registration: with an annual revenue of 500million, China Electronics is the actual controller
Write bootloader from 0 - bootloader relocates app
QT use qtreeview to login QQ friends list
Anti counterfeiting AI ownerless lamp, who is playing it really?
Detailed explanation of cesium events (mouse events, camera events, keyboard events, scene trigger events)
Xuantai pharmaceutical passed the registration: it plans to raise 600million yuan, and Lianhe investment is the major shareholder
Scratch score query Electronic Society graphical programming scratch grade examination level 4 true questions and answers analysis June 2022
Academia vs industry: can deep learning break the ceiling of video coding and decoding
DRF--跨域问题解决
Codeforces Round #807 (Div. 2)
Cesium loads 3D tiles data
Musk uploaded that his brain was crazy, but neuralink was already a chicken feather
Hcip notes sorting 2022/7/17
Shell基础
网页红警,如何将存入本地的音频转成可播放状态
柠萌影视通过聆讯:年营收12亿 腾讯与芒果文创是股东