Image effect on specific part of objects
In this article I'm going to show you how to apply an Image effect only to specific area
this is a screenshot from CyberPunk 2077 that censored some parts of the bodies
Solution 1
Using GrabPass
GrabPass is a special pass type - it grabs the contents of the screen where the object is about to be drawn into a texture. This texture can be used in subsequent passes to do advanced image based effects.
- Just GrabPass { } grabs the current screen contents into a texture. The texture can be accessed in further passes by _GrabTexture name. Note: this form of grab pass will do the time-consuming screen grabbing operation for each object that uses it.
- GrabPass { "TextureName" } grabs the current screen contents into a texture, but will only do that once per frame for the first object that uses the given texture name. The texture can be accessed in further passes by the given texture name. This is a more performant method when you have multiple objects using GrabPass in the scene
Initializing position and grab position in shader:
pixelating results:
now attach this shader to a material and use it
Solution 2
Using two cameras that I explained before
1.Make secondary camera
2.On second camera, in Clear Flags set it to Don't clear
Solution 3
Using Command Buffer
we know, that the GPU can work on stuff in parallel. But what’s about the communication between CPU and GPU? Does the CPU has to wait until the GPU finished the job before it can receive new commands?
No! because this communication would create bottlenecks (e.g. when the CPU can’t deliver commands fast enough) and would make parallel working impossible.bottleneck, as for how it is named, occurs when there is a limit on how much data is being sent for processing or how much data can be processed at the same time.
The solution is a list where commands can be added by the CPU and read by the GPU – independent from each other! This list is called: Command Buffer.
Command buffers hold list of rendering commands ("set render target, draw mesh, ..."). They can be set to execute at various points during camera rendering (see Camera.AddCommandBuffer), light rendering (see Light.AddCommandBuffer) or be executed immediately (see Graphics.ExecuteCommandBuffer).
The command buffer makes it possible that CPU and GPU can work independent from each other. When the CPU wants something to be rendered, it can push that command into the queue and when the GPU has free resources, it can take the command out of the list and execute it (but the list works as a FIFO – so the GPU can only take the oldest item in the list (which was first/earlier added than all others) and work on that).
By the way: there are different commands possible. One example is a draw call, another would be to change the render state.
this is a vulkan command buffer as you can see there are bunch of commands in command buffer:
https://vulkan-tutorial.com/Drawing_a_triangle/Drawing/Command_buffers
https://developer.nvidia.com/engaging-voyage-vulkan
Let's Start to use the command buffer
Masking part of the character with the sphere
create a command
Temporarily copy the rendering result to the Render Texture
Apply the rendering result to the material that you want to apply image effects
Release temporary render texture
Register where you want to add the command buffer
Below is a high-level overview of how Cameras use the Forward or Deferred pipeline to render a Scene in Unity.
- Black boxes represent an internal Unity process.
- Blue boxes represent a CameraEvent where you can add Command Buffers.
add Image effects to where Ref is equal to 1
adding color to pixelated area
More Examples!
pixelating specific objects
grayscale where that is not equal to Ref 1
References:
Co-Founder, Technical Lead at Funtory
3yبسیار عالی. استفاده کردیم
Product Manager, Game Designer at True Dream
3yAhsantom !