In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use Stencil to optimize local post-processing special effects, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Just an example, don't talk to me about cross films and billboards superimposed. The realization of this effect is to use solid color to redraw the original model on RT and then fold it back to the original image after a Gaussian blur.
With the addition of Stencil, the amount of computation has been reduced by half (for comparison, I cut out the Blit flow and character drawing part of the final image of the post-processing process, showing only the amount of downsample and Gaussian blur)
Principle:
Stencil and ZTest are like two brothers, very similar, but the former is often ignored. As we all know, in modern GPU with Early-Z (Early-ZS, to be exact, Z+Stencil) mechanism, we can make occluded objects from drawing through ZTest through front-to-back drawing, which is the most basic fillrate optimization method.
Stencil itself is a technology for comparing + rewriting a specific Buff, which is actually the same mechanism as the comparison depth + rewrite depth of ZTest, but with more changes. So it can also skip the pixel calculation phase directly by rewriting the Buff and then letting the subsequent objects fail when reading the Buff.
It is not cost-effective to use post-processing effects for local objects, because even if there is only a local need to calculate, GPU must calculate the entire screen. If you first draw a marker area on the screen with Stencil, you can limit the subsequent calculation to a small range.
A simple normal is used to extend the OutLine, which expands the size of the rod. And mark Stencil as 1 in the place where it has been drawn
Pass {CULL OFF ZTest OFF COLORMASK 0 Stencil {Ref 1 Comp NotEqual Pass Replace ReadMask 1 WriteMask 1} CGPROGRAM # pragma vertex vert # pragma fragment frag # include "UnityCG. Cginc "struct appdata {float4 vertex: POSITION Float3 normal: NORMAL;}; struct v2f {float4 vertex: SV_POSITION;}; float _ Outline; v2f vert (appdata v) {v2f o; o.vertex = UnityObjectToClipPos (v.vertex) Float3 norm = mul ((float3x3) UNITY_MATRIX_IT_MV, v.normal); float2 offset = TransformViewToProjection (norm.xy); o.vertex.xy + = normalize (offset) * o.vertex.z * _ Outline; UNITY_TRANSFER_FOG (o.vertex); return o } fixed4 frag (v2f I): SV_Target {return 1;} ENDCG}
Then, in the later post-processing process, add the required Pass
Stencil {Ref 1 Comp Equal Pass Keep ReadMask 1 WriteMask 1}
That's it.
But...
1. Stencil can only be written by drawing entities, and cannot be copied between multiple Buff. So once downsample is passed, it will be lost, and downsample is very common in post-processing.
two。 For this reason, the order in which RT is drawn is also very important, and only RT, as a drawing target, has a chance to get Stencil detection.
The use of 3.Stencil in other RT can only be done through Graphics.SetRenderTarget (source.colorBuffer, stencil.depthBuffer). It points to the color Buffer of one RT and the depthBuff of another RT at the same time, and the resolution of the two RT must be exactly the same.
Specific code (normal Blit cannot be used, RenderTarget will be forced to be switched):
Private void DepthBlit (RenderTexture source, RenderTexture destination, Material mat, int pass, RenderTexture depth) {if (depth = = null) {Graphics.Blit (source, destination, mat, pass); return;} Graphics.SetRenderTarget (destination.colorBuffer, depth.depthBuffer); GL.PushMatrix (); GL.LoadOrtho (); mat.mainTexture = source Mat.SetPass (pass); GL.Begin (GL.QUADS); GL.TexCoord2 (0.0f, 1.0f); GL.Vertex3 (0.0f, 1.0f, 0.1f); GL.TexCoord2 (1.0f, 1.0f); GL.Vertex3 (1.0f, 1.0f, 0.1f); GL.TexCoord2 (1.0f, 0.0f) GL.Vertex3 (1.0f, 0.0f, 0.1f); GL.TexCoord2 (0.0f, 0.0f); GL.Vertex3 (0.0f, 0.0f, 0.1f); GL.End (); GL.PopMatrix ();}
The texture size limit is the most troublesome, and everything else is fine. The actual use is to keep the RT that holds the Stencil and then attach it to the target RT when needed.
Drawing marks comes at a cost, but you can also draw them incidentally when you draw a normal picture, which is much less expensive.
Remember to use it when you can use it.
Well, I know you might ask Bloom exactly how he did it. Bloom itself Unity has a built-in post-processing, but it is full-screen. If you want to be local, you can only open a single RT to draw the object to be flooded.
But this involves the problem of retaining the depth buffer, otherwise the object must be displayed at the front and cannot be obscured by the obstacle. Because you have to switch RT drawing at the same time, the only way is to Graphics.SetRenderTarget (source.colorBuffer, stencil.depthBuffer) and draw a new RT while using the previous depth buffer.
For the sake of simplicity, I switch RenderTarget directly in the post-processing stage, and then use Graphics.DrawMeshNow to draw the object to be flooded, which will cause Mesh to be unable to Batch. If you want to Batch, you have to let the camera draw these objects, and you have to use CommandBuffer.
On how to use Stencil to optimize local post-processing special effects to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.